如何在Java中获取字符串中双引号之间的字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22789293/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 17:52:48  来源:igfitidea点击:

How to get the string between double quotes in a string in Java

javastringquotes

提问by Batuhan Yaman

For example, input will be like:

例如,输入将类似于:

AddItem rt456 4  12 BOOK “File Structures” “Addison-Wesley” “Michael Folk”

and I want to read all by using scanner and put it in a array.

我想使用扫描仪读取所有内容并将其放入数组中。

like:

喜欢:

   info[0] = rt456
   info[1] = 4
   ..
   ..
   info[4] = File Structures
   info[5] = Addison-Wesley

So how can I get the string between quotes?

那么我怎样才能得到引号之间的字符串呢?

EDIT: a part of my code->

编辑:我的代码的一部分->

public static void main(String[] args) {
            String command;
        String[] line = new String[6];
        Scanner read = new Scanner(System.in);
        Library library = new Library();

        command = read.next();

        if(command.matches("AddItem"))
        {
            line[0] = read.next(); // Serial Number
            line[1] = read.next(); // Shelf Number
            line[2] = read.next(); // Shelf Index
            command = read.next(); // Type of the item. "Book" - "CD" - "Magazine"

            if(command.matches("BOOK"))
            {
                line[3] = read.next(); // Name
                line[4] = read.next(); // Publisher
                line[5] = read.next(); // Author

                Book yeni = new Book(line[0],Integer.parseInt(line[1]),Integer.parseInt(line[2]),line[3],line[4],line[5]);


    }
    }
}

so I use read.next to read String without quotes.

所以我使用 read.next 来读取没有引号的字符串。

SOLVED BY USING REGEX AS

使用正则表达式解决

read.next("([^\"]\S*|\".+?\")\s*");

采纳答案by Raul Guiu

An alternative using a messy regular expression:

使用凌乱正则表达式的替代方法:

public static void main(String[] args) throws Exception {
    Pattern p = Pattern.compile("^(\w*)[\s]+(\w*)[\s]+(\w*)[\s]+(\w*)[\s]+(\w*)[\s]+[“](.*)[”][\s]+[“](.*)[”][\s]+[“](.*)[”]");
    Matcher m = p.matcher("AddItem rt456 4  12 BOOK “File Structures” “Addison-Wesley” “Michael Folk”");

    if (m.find()) {
        for (int i=1;i<=m.groupCount();i++) {
            System.out.println(m.group(i));
        }
    }
}

That prints:

那打印:

AddItem
rt456
4
12
BOOK
File Structures
Addison-Wesley
Michael Folk

I assumed quotes are as you typed them in the question “” and not "", so they dont need to be escaped.

我假设引号是你在问题“”而不是“”中输入的,所以它们不需要被转义。

回答by JManish

You can try this. I have prepared the demo for your requirement

你可以试试这个。我已经根据您的要求准备了演示

  public static void main(String args[]) {
      String str = "\"ABC DEF\"";
      System.out.println(str);
      String str1 =  str.replaceAll("\"", "");
      System.out.println(str1);
  }

After reading just replace the double quotes with empty string

阅读后只需用空字符串替换双引号

回答by Steve Sarcinella

As a reference, take a look at this: Scanner Docs

作为参考,看看这个:扫描仪文档

How you read from the scanner is determined by how you will present the data to your user.

您如何从扫描仪读取数据取决于您将如何向用户呈现数据。

If they are typing it all on one line:

如果他们在一行中输入所有内容:

Scanner scanner = new Scanner(System.in);
String result = "";
System.out.println("Enter Data:");
result = scanner.nextLine();

Otherwise if you split it up into input fields you could do:

否则,如果将其拆分为输入字段,则可以执行以下操作:

Scanner scanner = new Scanner(System.in);
System.out.println("Enter Identifier:");
info[0] = scanner.nextLine();
System.out.println("Enter Num:");
info[1] = scanner.nextLine();
...

If you want to validate anything before assigning the data to a variable, try using scanner.next("");where the quotes contain a regex pattern to match

如果您想在将数据分配给变量之前验证任何内容,请尝试使用scanner.next("");其中引号包含正则表达式模式来匹配

EDIT:

编辑:

Check herefor regex info.

检查这里的正则表达式的信息。

As an example, say I have a string

例如,假设我有一个字符串

String foo = "The cat in the hat";

regex (Regular Expressions) can be used to manipulate this string in a very quick and efficient manner. If I take that string and do foo = foo.replace("\\s+", "");, this will replace any whitespace with nothing, therefore eliminating whitespace.

regex(正则表达式)可用于以非常快速有效的方式操作此字符串。如果我使用该字符串并执行foo = foo.replace("\\s+", "");,这将用空替换任何空格,从而消除空格。

Breaking down the argument \\s+, we have \swhich means match any character that is whitespace.

分解参数\\s+,我们有\s这意味着匹配任何空白字符。

The extra \before \sis a an escape character that allows the \sto be read properly.

额外的\before\s是一个允许\s正确读取的转义字符。

The +means match the previous expression 0 or more times. (Match all).

+装置0次或多次匹配以前的表达。(匹配所有)。

So foo, after running replace, would be "TheCatInTheHat"

所以 foo 在运行 replace 后将是“TheCatInTheHat”

Same this regex logic can apply to scanner.next(String regex);

同样这个正则表达式逻辑可以适用于 scanner.next(String regex);

Hopefully this helps a bit more, I'm not the best at explanation :)

希望这会有所帮助,我不是最擅长解释的:)

回答by Jason C

You can use StreamTokenizerfor this in a pinch. If operating on a String, wrap it with a StringReader. If operating on a file just pass your Readerto it.

您可以StreamTokenizer在紧要关头使用它。如果在 a 上操作,用 aString包裹它StringReader。如果对文件进行操作,只需将您的文件传递Reader给它。

// Replace “ and ” with " to make parsing easier; do this only if you truly are
// using pretty quotes (as you are in your post).
inputString = inputString.replaceAll("[“”]", "\"");

StreamTokenizer tokenizer = new StreamTokenizer(new StringReader(inputString));
tokenizer.resetSyntax();
tokenizer.whitespaceChars(0, 32);
tokenizer.wordChars(33, 255);
tokenizer.quoteChar('\"');

while (tokenizer.nextToken() != StreamTokenizer.TT_EOF) {
    // tokenizer.sval will contain the token
    System.out.println(tokenizer.sval);
}

You will have to use an appropriate configuration for non-ASCII text, the above is just an example.

您必须对非 ASCII 文本使用适当的配置,以上只是一个示例。

If you want to pull numbers out separately, then the default StreamTokenizerconfiguration is fine, although it uses doubleand provides no intnumeric tokens. Annoyingly, it is not possible to simply disable number parsing without resetting the syntax from scratch.

如果您想单独拉出数字,那么默认StreamTokenizer配置就可以了,尽管它使用double并且不提供int数字标记。令人讨厌的是,不可能在不从头开始重置语法的情况下简单地禁用数字解析。

If you don't want to mess with all this, you could also consider changing the input format to something more convenient, as in Steve Sarcinella's good suggestion, if it is appropriate.

如果您不想弄乱这一切,您还可以考虑将输入格式更改为更方便的格式,如Steve Sarcinella 的好建议(如果合适)。