【问题标题】:Getting incorrect output when using the Java method indexof in combination with substring to create a new string使用 Java 方法 indexof 结合 substring 创建新字符串时得到不正确的输出
【发布时间】:2015-02-26 17:01:53
【问题描述】:

在我的程序中,我试图在“,”字符处分割一个字符串。拆分字符串后,我需要在“=”字符之后创建一个新字符串。然后我需要将字符串重建回原来的状态。目前我能够拆分字符串并将其重建为原始状态。但是,当我尝试使用 indexof 和 substring 方法创建一个新字符串时,我没有得到正确的字符串。我在下面列出了我的代码以及我当前的输出和我不想要的输出。提前感谢您的帮助。

public class StringTestProgram {

public static void main(String[] args) {

String relativeDN = "cn=abc,dn=xyz,ou=abc/def";
System.out.println(relativeDN);

//Split String
String[] stringData = relativeDN.split(",");


{
StringBuilder sb = new StringBuilder();
CharSequence charAdded = ",";

// loop thru each element of the array
for (int place = 0; place < stringData.length; place++) {
System.out.println(stringData[place]);

{

int eq = relativeDN.indexOf('=');
String sub = relativeDN.substring(0, eq);           
System.out.println(sub);

}


// append element to the StringBuilder
sb.append(stringData[place]);

// avoids adding an extra ',' at the end
if (place < stringData.length - 1)

// if not at the last element, add the ',' character
sb.append(charAdded);

}

System.out.print(sb.toString());

}
}
}

我原来的字符串"cn=abc,dn=xyz,ou=abc/def"

我目前的输出:

cn=abc (split string)
cn (create new String)
dn=xyz (split string)
cn (create new String)
ou=abc/def (split string)
cn (create new String)
cn=abc,dn=xyz,ou=abc/def (rebuild String to its original form)

我想要的输出:

cn=abc (split string)
abc (create new string)
dn=xyz (split string)
xyz (create new String)
ou=abc/def (split string)
abc/def (create new String)
cn=abc,dn=xyz,ou=abc/def (rebuild String to its original form)

【问题讨论】:

    标签: java string split substring indexof


    【解决方案1】:

    更改以下行:

    int eq = relativeDN.indexOf('='); String sub = relativeDN.substring(0, eq); 在你的for 循环中

    int eq = stringData[place].indexOf('='); String sub = stringData[place].substring(eq+1, stringData[place].length());

    您需要在每次迭代中使用分隔字符串,因此您需要使用stringData[place]。当您使用 relativeDN 时,它会在每次迭代中获取您的原始字符串,并为每次迭代返回 cn。

    同样对于打印=之后的字符串,你需要指定起始位置为=之后的位置,结束位置为end of string(length)到substring函数。

    【讨论】:

    • Prera​​k 感谢您的帮助和详细解释,这行代码解决了我的问题。
    【解决方案2】:

    使用 Pattern 和 Matcher 类。

    String s = "cn=abc,dn=xyz,ou=abc/def";
    String parts[] = s.split(",");
    for(String i: parts)
    {
        System.out.println(i);
        Matcher m = Pattern.compile("(?<==).+").matcher(i);
        while(m.find())
        {
            System.out.println(m.group());
        }
    }
    System.out.println(s);
    

    输出:

    cn=abc
    abc
    dn=xyz
    xyz
    ou=abc/def
    abc/def
    cn=abc,dn=xyz,ou=abc/def
    

    或

    使用 StringBuilder 类。

    String relativeDN = "cn=abc,dn=xyz,ou=abc/def";
    String[] stringData = relativeDN.split(",");
    StringBuilder sb = new StringBuilder();
    for(int i =0;i<stringData.length;i++)
        {
            if(i!=0)
            {
                sb.append(",");
            }
    System.out.println(stringData[i]);
    int eq = stringData[i].indexOf('=');
    String sub = stringData[i].substring(eq+1,stringData[i].length());           
    System.out.println(sub);
    sb.append(stringData[i]);
    }
    System.out.print(sb.toString());
    

    输出:

    cn=abc
    abc
    dn=xyz
    xyz
    ou=abc/def
    abc/def
    cn=abc,dn=xyz,ou=abc/def
    

    【讨论】:

    • 感谢@Avinash 的帮助,但是我想使用方法 indexof 和 substring 来获得我想要的输出。
    • 你需要使用这个substring(eq+1,stringData[i].length());作为子字符串。
    • @Papitulo 为什么仅限于 indexof 和 substring ?是作业还是练习?
    • @niceman 这是一个练习,我知道可以使用这些方法来完成,我想学习如何而不是放弃它以寻找替代方法。
    • @Papitulo 的主要部分是 substring(eq+1,stringData[i].length()); 这就是我在你接受答案之前所说的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-11-04
    • 2021-01-09
    • 2018-07-22
    • 1970-01-01
    • 2022-06-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多