繁体   English   中英

复制一个字符串拆分为另一个字符串

[英]Copy a string splitted to another string

在类的某个地方, 我声明了一个时间字符串变量

String name;

我将用它来存储文本中的数据 文本具有许多具有以下两种格式的字段

Type: text/html

name=foo

对于这种情况,我对name=foo 类型的字段特别感兴趣

因此, 我之前使用split 破坏了文本的行

String lines[] = text.split("\n"); 

而且,我将再次使用split来标识所提及类型的字段。 在下面的代码中,while循环在检测到name=foo字段的地方停止,并在控制台中打印该字段的值。

int i = 0;  // Counter for the while cycle

while (!(lines[i].split("=")[0].equals("name"))) {
    i++;                      

    if (lines[i].split("=")[0].equals("name")) // If the field is name...
    System.out.println(lines[i].split("=")[1]); // Prints the value of the field

    name = lines[i].split("=")[1]; // <-- My problem is here
}

当我想将字段的值复制到前面提到的String变量时,我的问题就开始了,给了我java.lang.ArrayIndexOutOfBoundsException

我需要String稍后再对其进行处理。 有什么想法可以安全地将该字段的值复制到String变量吗?

向您的if中添加括号,可以避免两个问题:

  • 如果一行不包含=整个字符串都在[0]中,则访问[1]将导致上述异常
  • 无论条件如何,您都在更改(覆盖)变量名

为了使编译器满意,您可能还想将name初始化为null

int i = 0;  // Counter for the while cycle

while (!(lines[i].split("=")[0].equals("name"))) {
    i++;                      

    if (lines[i].split("=")[0].equals("name")){ // If the field is name...
        System.out.println(lines[i].split("=")[1]); // Prints the value of the field

        name = lines[i].split("=")[1]; // <-- My problem is here
    }
}

在您的代码中:

String name;
name = lines[i].split("=")[1];

这里的名字每次都会被覆盖。

我认为您正在寻找这样的东西:

String names[];

String lines[] = text.split("\n");
names[] = new String[lines.length];

在您的while循环内部,您可以像这样:

names[i] = lines[i].split("=")[1];

关于您的代码,有很多事情要注意:

  • 您可能在if -statement之后错过{} ,因此每次while -loop运行时都会更新名称
  • 您访问[1]而不检查split("=")产生了多少个元素
  • 您实际上几乎在每一行上都调用split("=") 4次。 通过引入临时变量来节省CPU时间!
  • 你可以取代你while由-loop for -loop也发现name=value在第一行,不“吐”如果name=value不是内部的任何线路的(你不检查是否i是少比lines.length

我把你的评论留在我的答案里; 随时删除它们。

变体a(使用索引):

for (int i = 0; i < lines.length; i++) {
    // Only split once and keep X=Y together in name=X=Y by specifying , 2
    final String[] split = lines[i].split("=", 2);

    if (split.length == 2 && split[0].equals("name")){ // If the field is name...
        System.out.println(split[1]); // Prints the value of the field

        name = split[1]; // <-- My problem is here
        break; // no need to look any further
    }
}

变体b(使用“ for-each”):

for (String line : lines) {
    // Only split once and keep X=Y together in name=X=Y by specifying , 2
    final String[] split = line.split("=", 2);

    if (split.length == 2 && split[0].equals("name")) { // If the field is name...
        System.out.println(split[1]); // Prints the value of the field

        name = split[1]; // <-- My problem is here
        break; // no need to look any further
    }
}

我想您的问题是,当您到达最后一行或不包含“ =”符号的行时。 您正在检查

!(lines[i].split("=")[0].equals("name"))

但随后您将i加1,所以现在这种情况可能是假的

if (lines[i].split("=")[0].equals("name"))

你会在这里得到java.lang.ArrayIndexOutOfBoundsException

name = lines[i].split("=")[1];

如果该行不包含“ =“。

尝试

if (lines[i].split("=")[0].equals("name")) { // If the field is name...
    System.out.println(lines[i].split("=")[1]); // Prints the value of the field

    name = lines[i].split("=")[1];
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM