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

后端 未结 6 1636
清酒与你
清酒与你 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条回答
  •  萌比男神i
    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...

提交回复
热议问题