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

后端 未结 6 1574
清酒与你
清酒与你 2021-01-26 13:58

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 stri

相关标签:
6条回答
  • 2021-01-26 14:31

    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
    
    0 讨论(0)
  • 2021-01-26 14:43

    Use StringUtils.subStringBetween

    0 讨论(0)
  • 2021-01-26 14:47
    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));
        }
    }
    

    }

    0 讨论(0)
  • 2021-01-26 14:47

    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...

    0 讨论(0)
  • 2021-01-26 14:52
    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.

    0 讨论(0)
  • 2021-01-26 14:52

    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.

    0 讨论(0)
提交回复
热议问题