1

i'm trying to get the Text between two quotes with this code. But i have got only the first text "nice day". Please help ...

Text input:

 Today is a „nice day“  and i „like“  it because because of the „sun and flowers“ .

Code:

public static String getStringBetweenTwoChars(String input, String startChar, String endChar) {

        try {
            for (int i = 0; i < input.length(); i++) {
                int start = input.indexOf(startChar);
                if (start != -1) {
                    int end = input.indexOf(endChar, start + startChar.length());
                    if (end != -1) {
                        String x = input.substring(start + startChar.length(), end);
                        input = input.substring(end + 1);
                        return  x;
                    }


                }

            }


        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }



  • There are so many problems. Some of them are: (1) You need to change the return type from `String` to `void`; because a function can return only one value (2) You do not need `try...catch` block. (3) The parameters `startChar` and `endChar` are unnecessary/ambiguous etc. – Arvind Kumar Avinash Jun 04 '20 at 16:10

4 Answers4

2
        String res = "";
        try {
            for (int i = 0; i < input.length(); i++) {
                System.out.println("Input-Length: " + input.length());
                int start = input.indexOf(startChar);
                if (start != -1) {
                    int end = input.indexOf(endChar, start + startChar.length());

                    if (end != -1) {
                        String x = input.substring(start + startChar.length(), end);
                        input = input.substring(end + 1);
                        res += x;
                    }


                }

            }
            return res;


        } catch (Exception e) {
            e.printStackTrace();
        }
        return null; 
    }```


Krustibat
  • 81
  • 5
  • Hi Krustibat. I have new Idea: I want me to get an array at the output. So i've changed the code below: But I don't become a Array. Could you tell me, what i do wrong? (Please see the Code below) – MusterTester Jun 04 '20 at 23:00
1

One solution could be to split input and read only odd index.

public class Split {
    public static void main(String[] args)
    {
        String sin = "Today is a \"nice day\" and i \"like\" it because because of the \"sun and flowers\".";
        System.out.println(sin);
        String[] s=sin.split("\"");
        for(int i=0;i<s.length;i++)
            if(i%2==1) System.out.println(s[i]);
    }
}

Output:

Today is a "nice day" and i "like" it because because of the "sun and flowers".
nice day
like
sun and flowers

Update on posted code:

public class Split {
    public static void main(String[] args)
    {
        String sin = "Today is a \"nice day\" and i \"like\" it because because of the \"sun and flowers\".";
        String sout = Split.getStringBetweenTwoChars(sin, "\"", "\"");
        System.out.println(sout);
    }

    public static String getStringBetweenTwoChars(String input, String startChar, String endChar) {

        try {
            for (int i = 0; i < input.length(); i++) {
                System.out.println("Input-Length: " + input.length());
                int start = input.indexOf(startChar);
                if (start != -1) {
                    int end = input.indexOf(endChar, start + startChar.length());

                    System.out.println("Das sind: " + end);
                    if (end != -1) {
                        String x = input.substring(start + startChar.length(), end);
                        input = input.substring(end + 1);

                        System.out.println("RestText: "+input);
                        System.out.println("QuoteText: "+x);
                        String snext=getStringBetweenTwoChars(input, "\"", "\"");
                        if(snext!=null)
                            x=x+","+snext;
                        return  x;
                    }
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}

Output

Today is a "nice day" and i "like" it because because of the "sun and flowers".
Input-Length: 79
Das sind: 20
RestText:  and i "like" it because because of the "sun and flowers".
QuoteText: nice day
Input-Length: 58
Das sind: 12
RestText:  it because because of the "sun and flowers".
QuoteText: like
Input-Length: 45
Das sind: 43
RestText: .
QuoteText: sun and flowers
Input-Length: 1
nice day,like,sun and flowers

Update for array

//let method signature as it is
String sout = Split.getStringBetweenTwoChars(sin, "\"", "\"");
//remove comma and get result into array
String[] s=sout.split(",");
Traian GEICU
  • 1,750
  • 3
  • 14
  • 26
  • @ Traian: Thank you for your feedback. But do you know: why is my code not work? – MusterTester Jun 04 '20 at 15:54
  • Yes, use recursivity and get all results ... Updated – Traian GEICU Jun 04 '20 at 16:12
  • Ok, but with your code we have to change the original input-text with "/". If we have to input a very long text with 1000 dopple-quotes then is your code not effective. Let's see the solution of Krustibat from above is better. – MusterTester Jun 04 '20 at 21:27
  • NO. Method signature it's the same as you added ! Just added recursion. Local string is just for test purpose ! Add input string from where you wanted Text File, Locally have no importance since it's keep original signature. Try it ! – Traian GEICU Jun 04 '20 at 21:48
  • `Input-Length: 79` -> count it ! for `Today is a "nice day" and i "like" it because because of the "sun and flowers".` (this is what is processed-> can be obtain in various mode.Important is that is method is sending the right string !). Check also, maybe helpful: https://stackoverflow.com/questions/3559063/how-to-enter-quotes-in-a-java-string – Traian GEICU Jun 04 '20 at 21:51
  • `But do you know: why is my code not work` : Yes, use recursivity and get all results ... (didn't say that is the Only fix ... it's a solution). What's the down-side of recursivity ... another story ! ... any-way look again on first version and others and choose what think it's best fit ! – Traian GEICU Jun 04 '20 at 22:03
  • Ok, special thanks. It work very well. But with this code --> x=x+","+snext; i will get at output a String: "nice day,like,sun and flowers". How can i become a array like: ["nice day", "like", "sun and flowers"] ? – MusterTester Jun 04 '20 at 23:09
  • Yes, but have to modify method signature from `String` to `String []`. My advice is to return a `List` (and then do whatever ... place into array elements, etc) ... Another option very easy without any changes is `String[] arr = returnStringWithComma.split(",");` ... Good Luck ! – Traian GEICU Jun 04 '20 at 23:16
  • Hi, i have changed it with modify method signature from String to String [] (please see below) but it not work. – MusterTester Jun 04 '20 at 23:19
  • Yes, recursivity means auto-invoke and can cause some issues (can be fixed also). Easiest way is let metdod signature and code as it is and just process String with coma ... check update – Traian GEICU Jun 04 '20 at 23:25
  • Hi. Now i've got that thing! So i'v used this code: String[] s=sout.split(","); and then: System.out.println(Arrays.toString(s)); DONE! – MusterTester Jun 05 '20 at 11:43
1

You can use regular expression to target those phrases inside the quotes:

public static void main(String[] args) {
    String startChar = "\"";
    String endChar = "\"";

    String input = "Today is a \"nice day\" and i \"like\" it because because of the \"sun and flowers\".";
    String pattern = startChar + "([\\s\\w]*)" + endChar;
    Matcher m = Pattern.compile(pattern).matcher(input);

    while (m.find()) {
        System.out.println(m.group(1));
    }
}
libanbn
  • 272
  • 2
  • 8
0

I would just split it by quotation marks, and the only go after the odd numbered elements in the resulting array:

String str = "Today is a \"nice day\" and i \"like\" it because because of the \"sun and flowers\".";
String[] parts = str.split("\""), res = new String[parts.length / 2];

for (int i = 0; i < res.length; i++) {
    res[i] = parts[i * 2 + 1];
}

return res;

Note that this returns an array of strings, not just one string, so you may have to adjust your return type.

Charlie Armstrong
  • 2,332
  • 3
  • 13
  • 25