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

问题描述 投票:0回答:1

我想用这段代码来获取两个引号之间的文字,但我只得到了第一个文字 "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;
    }



java string double-quotes
1个回答
1
投票

一个解决方案可能是分割输入,只读奇数索引。

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(",");

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



1
投票

你可以使用正则表达式来锁定引号内的那些短语。

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

0
投票

我只需要用引号将其分割开来,然后只在结果数组中的奇数元素之后进行处理。

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;

请注意,这将返回一个字符串数组,而不仅仅是一个字符串, 所以你可能需要调整你的返回类型。

© www.soinside.com 2019 - 2024. All rights reserved.