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

Java 获取两个字符之间的字符串(两个引号),java,string,double-quotes,Java,String,Double Quotes,我试图用这段代码获取两个引号之间的文本。但我只收到了第一篇“美好的一天”。请帮忙 文本输入: Today is a „nice day“ and i „like“ it because because of the „sun and flowers“ . 代码: public静态字符串getStringBetweenTwoChars(字符串输入、字符串起始字符、字符串结束字符){ 试一试{ 对于(int i=0;i可以在各种模式下获得。重要的是方法发送正确的字符串!)。另外请检查,可能会

我试图用这段代码获取两个引号之间的文本。但我只收到了第一篇“美好的一天”。请帮忙

文本输入:

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

public静态字符串getStringBetweenTwoChars(字符串输入、字符串起始字符、字符串结束字符){
试一试{
对于(int i=0;i
一种解决方案是拆分输入和只读奇数索引

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]);
    }
}
更新已发布代码:

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;
    }
}
阵列的更新

//let method signature as it is
String sout = Split.getStringBetweenTwoChars(sin, "\"", "\"");
//remove comma and get result into array
String[] s=sout.split(",");

一种解决方案是拆分输入和只读奇数索引

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]);
    }
}
更新已发布代码:

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;
    }
}
阵列的更新

//let method signature as it is
String sout = Split.getStringBetweenTwoChars(sin, "\"", "\"");
//remove comma and get result into array
String[] s=sout.split(",");
String res=”“;
试一试{
对于(int i=0;i
String res=“”;
试一试{
对于(int i=0;i
您可以使用正则表达式以引号内的短语为目标:

publicstaticvoidmain(字符串[]args){
字符串startChar=“\”;
字符串endChar=“\”;
String input=“今天是一个“美好的一天”,我“喜欢”它是因为“阳光和鲜花”;
字符串模式=startChar+“([\\s\\w]*)”+endChar;
Matcher m=Pattern.compile(Pattern.Matcher)(输入);
while(m.find()){
系统输出println(m.group(1));
}
}

您可以使用正则表达式以引号内的短语为目标:

publicstaticvoidmain(字符串[]args){
字符串startChar=“\”;
字符串endChar=“\”;
String input=“今天是一个“美好的一天”,我“喜欢”它是因为“阳光和鲜花”;
字符串模式=startChar+“([\\s\\w]*)”+endChar;
Matcher m=Pattern.compile(Pattern.Matcher)(输入);
while(m.find()){
系统输出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;
String str=“今天是“美好的一天”,我“喜欢”它是因为“阳光和鲜花”;
String[]parts=str.split(\”),res=newstring[parts.length/2];
for(int i=0;i

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

我将只使用引号将其拆分,并且只在结果数组中的奇数元素之后执行以下操作:

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;
String str=“今天是“美好的一天”,我“喜欢”它是因为“阳光和鲜花”;
String[]parts=str.split(\”),res=newstring[parts.length/2];
for(int i=0;i

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

@Traian:谢谢您的反馈。但是你知道吗:为什么我的代码不工作?是的,使用递归性并得到所有结果。。。UpdatedOk,但对于您的代码,我们必须使用“/”更改原始输入文本。如果我们必须输入一个包含1000个双引号的很长的文本,那么您的代码是否无效。让我们看看上面Krustibat的解决方案更好。不。方法签名与您添加的相同!只是增加了递归。本地字符串仅用于测试目的!从您想要的文本文件的位置添加输入字符串,因为保留原始签名在本地并不重要。试试看<代码>输入长度:79
->数一数!因为
今天是“美好的一天”,我“喜欢”它是因为“阳光和鲜花”。
(这就是所处理的->可以在各种模式下获得。重要的是方法发送正确的字符串!)。另外请检查,可能会有帮助:@Traian:谢谢您的反馈。但是你知道吗:为什么我的代码不工作?是的,使用递归性并得到所有结果。。。UpdatedOk,但对于您的代码,我们必须使用“/”更改原始输入文本。如果我们必须输入一个包含1000个双引号的很长的文本,那么您的代码是否无效。让我们看看上面Krustibat的解决方案更好。不。方法签名与您添加的相同!只是增加了递归。本地字符串仅用于测试目的!添加