拆分从char数组构建的String

时间:2020-12-28 15:44:45

I'm trying to split a specific String of the format

我正在尝试拆分格式的特定字符串

0001,0004,dd/mm/yy hh:mm:ss,01,value01,value02;

in order to extract and save value01 and value02. How can I do this?

为了提取和保存value01和value02。我怎样才能做到这一点?

Here is what i tried so far:

这是我到目前为止所尝试的:

//buffer contains a String like: "0001,0004,dd/mm/yy hh:mm:ss,01,18,750"
String string = new String(buffer,0,len);

String[] parts = string.split(",");

String temp = parts[5];
String lum = parts[6];
System.out.print(temp);
System.out.print(lum);

With this code I get ArrayIndexOutofBounds when running the program in Netbeans. Image of error description

使用此代码,我在Netbeans中运行程序时获得ArrayIndexOutofBounds。错误描述的图像

Also tried this method:

也试过这个方法:

//buffer contains a String like: "0001,0004,dd/mm/yy hh:mm:ss,01,18,750"
String s= new String(buffer,0,len);

String aux = s + ",";
String[] dados = aux.split(",");          
float valor1 = Float.parseFloat(dados[5]);
float valor2 = Float.parseFloat(dados[6]);
System.out.print(aux);

3 个解决方案

#1


1  

This:

String temp = parts[5];
String lum = parts[6];

Should be this:

应该是这样的:

String temp = parts[4];
String lum = parts[5];

Remember that arrays in Java are zero-based. So if you do this:

请记住,Java中的数组是从零开始的。所以,如果你这样做:

String[] parts = "0001,0004,dd/mm/yy hh:mm:ss,01,value01,value02".split(",");

Then "0001" would be in parts[0], "0004" in parts[1], "value01" in parts[4] etc.

那么“0001”将在部分[0]中,“0004”在部分[1]中,“value01”在部分[4]等中。

#2


0  

Achieving what you're trying to do is acutally pretty easy.

实现你想要做的事情非常简单。

Assuming that your string really always looks like:
0001,0004,dd/mm/yy hh:mm:ss,01,value01,value02;

假设你的字符串总是如下:0001,0004,dd / mm / yy hh:mm:ss,01,value01,value02;

Just cut off the start of the string which you don't need and extract the values afterwards:

只需切断不需要的字符串的开头,然后提取值:

// This can be done in hundreds of ways, for the sake of simplicity i'll use substring
String s ="0001,0004,dd/mm/yy hh:mm:ss,01,value01,value02;";
String cutOff = s.substring(31, s.length()-1);
String[] results = cutOff.split(",");

#3


0  

please find below code. It has just a bit modification in Andrew's code. Since we are storing the string after splitting into a String array and it's obvious that array index starts with 0. That's why in order to get value01 and value02, we should use index 4 and 5.

请找到以下代码。它在安德鲁的代码中只有一点修改。因为我们在分割成String数组后存储字符串,并且显然数组索引从0开始。这就是为什么为了获得value01和value02,我们应该使用索引4和5。

public class JavaApplication1
 {
  public static void main(String[] args)
  {
    String str="0001,0004,dd/mm/yy hh:mm:ss,01,value01,value02";
    String [] strArr=str.split(",");
    String temp=strArr[4];
    String lum=strArr[5];
    System.out.println(temp);
    System.out.println(lum);
 }   
}

拆分从char数组构建的String

#1


1  

This:

String temp = parts[5];
String lum = parts[6];

Should be this:

应该是这样的:

String temp = parts[4];
String lum = parts[5];

Remember that arrays in Java are zero-based. So if you do this:

请记住,Java中的数组是从零开始的。所以,如果你这样做:

String[] parts = "0001,0004,dd/mm/yy hh:mm:ss,01,value01,value02".split(",");

Then "0001" would be in parts[0], "0004" in parts[1], "value01" in parts[4] etc.

那么“0001”将在部分[0]中,“0004”在部分[1]中,“value01”在部分[4]等中。

#2


0  

Achieving what you're trying to do is acutally pretty easy.

实现你想要做的事情非常简单。

Assuming that your string really always looks like:
0001,0004,dd/mm/yy hh:mm:ss,01,value01,value02;

假设你的字符串总是如下:0001,0004,dd / mm / yy hh:mm:ss,01,value01,value02;

Just cut off the start of the string which you don't need and extract the values afterwards:

只需切断不需要的字符串的开头,然后提取值:

// This can be done in hundreds of ways, for the sake of simplicity i'll use substring
String s ="0001,0004,dd/mm/yy hh:mm:ss,01,value01,value02;";
String cutOff = s.substring(31, s.length()-1);
String[] results = cutOff.split(",");

#3


0  

please find below code. It has just a bit modification in Andrew's code. Since we are storing the string after splitting into a String array and it's obvious that array index starts with 0. That's why in order to get value01 and value02, we should use index 4 and 5.

请找到以下代码。它在安德鲁的代码中只有一点修改。因为我们在分割成String数组后存储字符串,并且显然数组索引从0开始。这就是为什么为了获得value01和value02,我们应该使用索引4和5。

public class JavaApplication1
 {
  public static void main(String[] args)
  {
    String str="0001,0004,dd/mm/yy hh:mm:ss,01,value01,value02";
    String [] strArr=str.split(",");
    String temp=strArr[4];
    String lum=strArr[5];
    System.out.println(temp);
    System.out.println(lum);
 }   
}

拆分从char数组构建的String