如何在用户输入的字符串中获取“?”之间的内容? Java的

我想检索有人以字符串forms输入的内容,我假设它是我需要的子字符串,但我不确定如何。

当用户输入一个混合了单词和数字的字符串时,所有字母和数字都被一个空格隔开:嘿110说“我不是很擅长Java”但是“我能钓得很好”

然后我想能够采取“我不是很擅长Java”和“我可以很好地钓鱼”并打印出引号内的内容,以便字符串中可以有多个引号。 现在我有if(userInput ==’“’)然后我用子串做一些事情,但我不确定是什么。

我不能使用分割,修剪,标记器,正则表达式或任何会使这个非常容易不幸的事情。

在这个方法中我尝试识别字符串中的某些东西是单词,数字还是引用:

public void set(String userInput)// method set returns void { num=0;// reset each variable so new input can be passed String empty=""; String wordBuilder=""; userInput+=" "; for(int index=0; index<userInput.length(); index++)// goes through each character in string { if(Character.isDigit(userInput.charAt(index)))// checks if character in the string is a digit { empty+=userInput.charAt(index); } else { if (Character.isLetter(userInput.charAt(index))) { wordBuilder+=userInput.charAt(index); } else { if(userInput.charAt(index)=='"') { String quote=(userInput.substring(index,); } } //if it is then parse that character into an integer and assign it to num num=Integer.parseInt(empty); word=wordBuilder; empty=""; wordBuilder=""; } } } } 

谢谢!

我不确定你是否正在寻找这个,但它会逐步删除所引用的部分……

 String quote = "I say: \"I have something to say, \"It's better to burn out then fade away\"\" outloud..."; if (quote.contains("\"")) { while (quote.contains("\"")) { int startIndex = quote.indexOf("\""); int endIndex = quote.lastIndexOf("\""); quote = quote.substring(startIndex + 1, endIndex); System.out.println(quote); } } 

哪个输出……

 I have something to say, "It's better to burn out then fade away" It's better to burn out then fade away 

更新

我不知道这是不是在作弊……

 String quote = "I say: \"I have something to say, \"It's better to burn out then fade away\"\" outloud...\"Just in case you don't believe me\""; String[] split = quote.split("\""); for (String value : split) { System.out.println(value); } 

哪个输出……

 I say: I have something to say, It's better to burn out then fade away outloud... Just in case you don't believe me 

更新

好的,假String#split

 StringBuilder sb = new StringBuilder(quote.length()); for (int index = 0; index < quote.length(); index++) { if (quote.charAt(index) == '"') { System.out.println(sb); sb.delete(0, sb.length()); } else { sb.append(quote.charAt(index)); } } 

更新

好的,这基本上是假的split选项......

 String quote = "blah blah 123 \"hello\" 234 \"world\""; boolean quoteOpen = false; StringBuilder sb = new StringBuilder(quote.length()); for (int index = 0; index < quote.length(); index++) { if (quote.charAt(index) == '"') { if (quoteOpen) { System.out.println("Quote: [" + sb.toString() + "]"); quoteOpen = false; sb.delete(0, sb.length()); } else { System.out.println("Text: [" + sb.toString() + "]"); sb.delete(0, sb.length()); quoteOpen = true; } } else { sb.append(quote.charAt(index)); } } if (sb.length() > 0) { if (quoteOpen) { System.out.println("Quote: [" + sb.toString() + "]"); } else { System.out.println("Text: [" + sb.toString() + "]"); } } 

哪个产生......

 Text: [blah blah 123 ] Quote: [hello] Text: [ 234 ] Quote: [world] 

知道,我不知道你是如何存储结果的。 我很想创建一些基本类,它们能够存储String结果并将它们添加到List这样我就可以保持顺序并可能使用某种标志来确定它们的类型......

尝试下一个:

 public static void main(String[] args) { String input = "\"123\" hey 110 say \"I am not very good at Java\" but \" I can fish pretty well\""; int indexQuote = -1; boolean number = true; String data = ""; for (int i = 0; i < input.length(); i++) { char ch = input.charAt(i); if (Character.isWhitespace(ch)) { if (data.length() > 0 && indexQuote == -1) { if (number) { System.out.println("It's a number: " + data); } else { System.out.println("It's a word: " + data); } // reset vars number = true; data = ""; } else if (indexQuote != -1) { data += ch; } } else if (ch == '"') { if (indexQuote == -1) { number = false; indexQuote = i; } else { System.out.println("It's a quote: " + data); // reset vars number = true; data = ""; indexQuote = -1; } } else { if (!Character.isDigit(ch)) { number = false; } data += ch; if (data.length() > 0 && i == input.length() - 1) { if (number) { System.out.println("It's a number: " + data); } else { System.out.println("It's a word: " + data); } } } } } 

输出:

 It's a word: hey It's a number: 110 It's a word: say It's a quote: I am not very good at Java It's a word: but It's a quote: I can fish pretty well 

迭代字符串并使用临时int变量来存储引用的字符串。 当您看到它结束时,您可以提取该子字符串并使用它执行您想要的操作。

使用StringUtils.subStringBetween

 public class MyTestSecond { public static void main(String...args){ String a = "hey 110 say \"I am not very good at Java\""; // Method 1 if(a.contains("\"")) System.out.println(a.substring(a.indexOf("\""),a.lastIndexOf("\"")+1)); //Method 2 String[] array = a.split(" "); for(int i=0;i 

}

 public String getNextQuote(int index, String sentence){ return sentence.substring(sentence.indexOf("\"", index + 1), sentence.indexOf("\"", index + 2)); } 

用法:使用索引作为参数调用方法。 此索引类似于您遇到的最后一个"索引"

之后,它将返回下两个引号之间的所有内容。