0

我想检索某人作为字符串输入的引号中的任何内容,我假设它是我需要的子字符串,但我不确定如何。

当用户输入一个由一个空格分隔的单词和数字混合的字符串时:嘿 110 说“我不太擅长 Java”但是“我可以钓鱼”

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

不幸的是,我不能使用 split、trim、tokenizer、regex 或任何使这变得非常容易的东西。

这一切都在这个方法中,我尝试识别字符串中的某些内容是单词、数字还是引号:

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="";
            }


        } 

    }


}

谢谢!

4

6 回答 6

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
于 2013-09-06T18:21:20.740 回答
0

遍历字符串并使用临时 int 变量来存储引用的字符串何时开始。当您看到它结束时,您可以提取该子字符串并使用它做您想做的事情。

于 2013-09-06T03:07:26.927 回答
0

使用StringUtils.subStringBetween

于 2013-09-06T03:15:46.017 回答
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));
    }
}

}

于 2013-09-06T03:24:29.160 回答
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结果并将它们添加到 a的基本类,List这样我就可以维护顺序,也许可以使用某种标志来确定它们是什么类型......

于 2013-09-06T03:29:00.677 回答
0
public String getNextQuote(int index, String sentence){
 return sentence.substring(sentence.indexOf("\"", index + 1), sentence.indexOf("\"", index + 2));
}

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

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

于 2013-09-06T18:20:43.097 回答