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

时间:2013-09-06 03:02:20

标签: java string substring double-quotes

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

当用户输入混合了单词和数字的字符串时,所有字符串都用一个空格分隔:  嘿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="";
            }


        } 

    }


}

谢谢!

6 个答案:

答案 0 :(得分:2)

尝试下一个:

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

答案 1 :(得分:0)

迭代字符串并使用临时int变量来存储引用的字符串。当你看到它结束时,你可以提取那个子串并用它做你想做的事。

答案 2 :(得分:0)

答案 3 :(得分:0)

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<array.length;i++){
        if(array[i].startsWith("\""))
            System.out.println(a.substring(a.indexOf("\""),a.lastIndexOf("\"")+1));
    }
}

}

答案 4 :(得分:0)

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

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的基本类,这样我就可以保持顺序并可能使用某种标志来确定它们的类型是...

答案 5 :(得分:0)

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

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

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