获取两个字符之间的字符串(两个引号)

get String between two Chars (two Quotes)

我正在尝试使用此代码获取两个引号之间的文本。但是我只有第一个文本"nice day"。请帮忙...

文本输入:

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

代码:

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



一种解决方案可能是拆分输入并只读取奇数索引。

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

输出:

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

更新发布的代码:

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

输出

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

阵列更新

//let method signature as it is
String sout = Split.getStringBetweenTwoChars(sin, "\"", "\"");
//remove comma and get result into array
String[] s=sout.split(",");
        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; 
    }```


您可以使用正则表达式来定位引号内的那些短语:

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

我只是用引号将它分开,并且只在结果数组中的奇数元素之后:

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;

请注意,此 return 是一组字符串,而不仅仅是一个字符串,因此您可能需要调整 return 类型。