How do I get what's inbetween “ ” in a user inputted String? Java

时光怂恿深爱的人放手 提交于 2019-12-20 07:37:07

问题


I'd like to retrieve whatever is in quotes that someone enters as a string, i'm assuming it's substring that I need but i'm not sure how.

When the user inputs a string mixed with words and numbers all separated by one space: hey 110 say "I am not very good at Java" but " I can fish pretty well"

Then I want to be able to take the "I am not very good at Java" and the "I can fish pretty well" and print out what's inside the quotes so there can be multiple quotes in the string. right now I have if( userInput=='"') then I do something with substring but i'm not sure what.

I can't use split, trim, tokenizer, regex or anything that would make this really easy unfortunatley.

it's all in this method where I try to identify if something in the string is a word, number or a quote:

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


        } 

    }


}

Thanks!


回答1:


I'm not sure if this quite what you are looking for, but it will strip down the quoted parts in steps...

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);
    }

}

Which outputs...

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

Updated

I don't know if this is cheating or not...

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);
}

Which outputs...

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

Updated

Okay, fake 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));
    }
}

Updated

Okay, this is basically fake split with options...

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() + "]");
    }
}

Which generates...

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

Know, I don't know how you are storing the results. I would be tempted to create some basic classes which were capable of storing the String results and add them to a List so I could maintain the order and maybe use a flag of some kind to determine what type they are...




回答2:


Try the next:

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);
                }
            }
        }
    }
}

Output:

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



回答3:


Iterate over the string and use a temporary int variable to store when the quoted string started. When you see that it ends, you can extract that substring and do what you want with it.




回答4:


Use StringUtils.subStringBetween




回答5:


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));
    }
}

}




回答6:


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

usage: call the method with an index as parameter. This index resembles the index of the last " that you've encountered.

Afterwards, it will return everything between the next two quotes.



来源:https://stackoverflow.com/questions/18649204/how-do-i-get-whats-inbetween-in-a-user-inputted-string-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!