繁体   English   中英

使用split()方法时利用字符串的不同部分

[英]Utilizing the Different Parts of a String when Using the split() Method JAVA

我正在使用while循环遍历一个名为s的字符串的ArrayList。

int i = 0;
while (i < s.length()) {

  i++;
  s.get(i).split(",");

}

我试图split()的每一行s用分隔符"," 我想将每行的每个部分放入这样的新Product对象中: new Product(s.get(i) first part, s.get(i) second part)

我找不到一种方法来捕获和利用我要分割的字符串的每个部分。

String[] result = s.get(i).split(",");

result包含字符串的各个拆分部分。

并在您的while循环中将length方法从s.length()纠正为s.length

split方法返回一个字符串数组。

另外,使用for循环:

for (int i=0; i<s.length(); i++) {
    String[] parts = s.get(i).split(",");
    Product product = new Product(parts[0], parts[1]);
}

暂无
暂无

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

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