如何将连接的字符串拆分为多个浮点值?

时间:2022-07-07 21:37:15

I'm a begginer in java I have

我是爪哇的乞丐

packet=090209153038020734.0090209153039020734.0 

like this I want to split this string and store into an array like two strings:

像这样,我想把这个字符串分割成一个数组,像两个字符串:

1) 090209153038020734.0
2) 090209153039020734.0

I have done like this:

我这样做过:

String packetArray[] = packets.split(packets,Constants.SF); 

Where: Constants.SF=0x01.

地点:Constants.SF = 0 x01。

But it won't work.

但它不会工作。

Please help me.

请帮助我。

6 个解决方案

#1


3  

I'd think twice about using split since those are obviously fixed width fields.

对于使用split,我要再三考虑,因为这些字段显然是固定宽度的。

I've seen them before on another question here (several in fact so I'm guessing this may be homework (or a popular data collection device :-)) and it's plain that the protocol is:

我之前在这里看到过他们的另一个问题(事实上,我猜这可能是家庭作业(或者是一个流行的数据收集设备:-)),很明显,协议是:

  • STX (0x01).
  • STX(0 x01)。
  • 0x0f.
  • 0 x0f。
  • date (YYMMDD or DDMMYY).
  • 日期(YYMMDD或DDMMYY)。
  • time (HHMMSS).
  • 时间(HHMMSS)。
  • 0x02.
  • 0 x02。
  • value (XXXXXX.X).
  • 值(XXXXXX.X)。
  • 0x03.
  • 0 x03。
  • 0x04.
  • 0 x04。

And, given that they're fixed width, you should probably just use substrings to get the information out.

由于它们的宽度是固定的,你应该使用子字符串来获取信息。

#2


2  

The JavaDoc of String is helpful here: http://java.sun.com/j2se/1.4.2/docs/api/java/lang/String.html

String的JavaDoc在这里很有用:http://java.sun.com/j2se/1.4.2/docs/api/java/lang/String.html

You have your String packet;

你有你的字符串包;

String.indexOf(String) gives you a position of a special substring. your interested in the "." sign. So you write

String. indexof (String)提供一个特殊子字符串的位置。你对"。"标志感兴趣。所以你写

int position = packet.indexOf(".")+1

+1 becuase you want the trailing decimal too. It will return something 20-ish and will be the last pos of the first number.

+1因为后面的小数也是+1。它将返回大约20的内容,并将是第一个数字的最后一个po。

Then we use substring

然后我们使用子串

String first = packet.substring(0,position) will give you everything up to the ".0" String second = packet.substring(position-1) should give you everything starting after the ".0" and up to the end of the string.

String first = packet.substring(0,position)将把所有东西都交给你。0" String second = packet.substring(position-1)应该会给你在"后面开始的所有信息。0"一直到弦的末端。

Now if you want them explicitely into an array you can just put them there. The code as a whole - I may have some "off by one" -bugs.

如果你想把它们显式地放到数组中你可以把它们放在那里。整个代码——我可能有一些“一刀切”的bug。

int position = packet.indexOf(".")+1 
String first = packet.substring(0,position)
String second = packet.substring(position-1)
String[] packetArray = new String[2];
packetArray[0] = first;
packetArray[1] = second;

#3


2  

String packetArray[] = packets.split("\u0001");

should work. You are using

应该工作。您正在使用

public String[] split(String regex, int limit)

which is doing something else: It makes sure that split() returns an array with at most limit members (1 in this case, so you get what you ask for).

它还做了其他的事情:它确保split()返回一个数组,该数组的限制成员最多为1(在本例中为1,因此您将得到所需的)。

#4


1  

You need to read the Javadocs for the String.split() methods...you are calling the version of String.split() that takes a regular expression and a limit, but you are passing the string itself as the first parameter, which doesn't really make sense.

您需要为String.split()方法读取javadoc。您正在调用string .split()的版本,它接受一个正则表达式和一个限制,但是您将字符串本身作为第一个参数传递,这实际上是没有意义的。

As Aaron Digulla mentioned, use the other version.

正如Aaron Digulla提到的,使用另一个版本。

#5


0  

You don't say how you want to do the split. It could be based on a fixed length (number of characters) or you want one decimal place.

你不会说你想要怎样分开。它可以是基于固定长度(字符数),也可以是需要一个小数点。

If the former you could do packetArray = new String[]{packet.substring(0, 20), packet.substring(21)};

如果是前者,可以使用packetArray = new String[]{packet。substring(0,20),packet.substring(21)};

int dotIndex = packets.indexOf('.');
packetArray = new String[]{packet.substring(0, dotIndex+2), packet.substring(dotIndex+2)};

Your solution confuses the regexp with the string.

您的解决方案将regexp与字符串混淆。

#6


0  

split uses regular expressions as documented here. Your code seems to be trying to match the whole string Constants.SF = 0x01 times, which doesn't make much sense. If you know what char the boxes are then you can use something like {[^c]+cc} where c is the character of the box (i guess this is 0x01), to match each "packet".

split使用这里记录的正则表达式。您的代码似乎试图匹配整个字符串常量。SF = 0x01乘以,这没什么意义。如果你知道字符的箱子是什么的话,那么你可以使用诸如{[^ c]+ cc },c是盒子的角色(我想这是0 x01),与每一个“包”。

I think you are trying to use it like the .net String.Split(...) function?

我觉得你是在用。net字符串。split(…)函数?

#1


3  

I'd think twice about using split since those are obviously fixed width fields.

对于使用split,我要再三考虑,因为这些字段显然是固定宽度的。

I've seen them before on another question here (several in fact so I'm guessing this may be homework (or a popular data collection device :-)) and it's plain that the protocol is:

我之前在这里看到过他们的另一个问题(事实上,我猜这可能是家庭作业(或者是一个流行的数据收集设备:-)),很明显,协议是:

  • STX (0x01).
  • STX(0 x01)。
  • 0x0f.
  • 0 x0f。
  • date (YYMMDD or DDMMYY).
  • 日期(YYMMDD或DDMMYY)。
  • time (HHMMSS).
  • 时间(HHMMSS)。
  • 0x02.
  • 0 x02。
  • value (XXXXXX.X).
  • 值(XXXXXX.X)。
  • 0x03.
  • 0 x03。
  • 0x04.
  • 0 x04。

And, given that they're fixed width, you should probably just use substrings to get the information out.

由于它们的宽度是固定的,你应该使用子字符串来获取信息。

#2


2  

The JavaDoc of String is helpful here: http://java.sun.com/j2se/1.4.2/docs/api/java/lang/String.html

String的JavaDoc在这里很有用:http://java.sun.com/j2se/1.4.2/docs/api/java/lang/String.html

You have your String packet;

你有你的字符串包;

String.indexOf(String) gives you a position of a special substring. your interested in the "." sign. So you write

String. indexof (String)提供一个特殊子字符串的位置。你对"。"标志感兴趣。所以你写

int position = packet.indexOf(".")+1

+1 becuase you want the trailing decimal too. It will return something 20-ish and will be the last pos of the first number.

+1因为后面的小数也是+1。它将返回大约20的内容,并将是第一个数字的最后一个po。

Then we use substring

然后我们使用子串

String first = packet.substring(0,position) will give you everything up to the ".0" String second = packet.substring(position-1) should give you everything starting after the ".0" and up to the end of the string.

String first = packet.substring(0,position)将把所有东西都交给你。0" String second = packet.substring(position-1)应该会给你在"后面开始的所有信息。0"一直到弦的末端。

Now if you want them explicitely into an array you can just put them there. The code as a whole - I may have some "off by one" -bugs.

如果你想把它们显式地放到数组中你可以把它们放在那里。整个代码——我可能有一些“一刀切”的bug。

int position = packet.indexOf(".")+1 
String first = packet.substring(0,position)
String second = packet.substring(position-1)
String[] packetArray = new String[2];
packetArray[0] = first;
packetArray[1] = second;

#3


2  

String packetArray[] = packets.split("\u0001");

should work. You are using

应该工作。您正在使用

public String[] split(String regex, int limit)

which is doing something else: It makes sure that split() returns an array with at most limit members (1 in this case, so you get what you ask for).

它还做了其他的事情:它确保split()返回一个数组,该数组的限制成员最多为1(在本例中为1,因此您将得到所需的)。

#4


1  

You need to read the Javadocs for the String.split() methods...you are calling the version of String.split() that takes a regular expression and a limit, but you are passing the string itself as the first parameter, which doesn't really make sense.

您需要为String.split()方法读取javadoc。您正在调用string .split()的版本,它接受一个正则表达式和一个限制,但是您将字符串本身作为第一个参数传递,这实际上是没有意义的。

As Aaron Digulla mentioned, use the other version.

正如Aaron Digulla提到的,使用另一个版本。

#5


0  

You don't say how you want to do the split. It could be based on a fixed length (number of characters) or you want one decimal place.

你不会说你想要怎样分开。它可以是基于固定长度(字符数),也可以是需要一个小数点。

If the former you could do packetArray = new String[]{packet.substring(0, 20), packet.substring(21)};

如果是前者,可以使用packetArray = new String[]{packet。substring(0,20),packet.substring(21)};

int dotIndex = packets.indexOf('.');
packetArray = new String[]{packet.substring(0, dotIndex+2), packet.substring(dotIndex+2)};

Your solution confuses the regexp with the string.

您的解决方案将regexp与字符串混淆。

#6


0  

split uses regular expressions as documented here. Your code seems to be trying to match the whole string Constants.SF = 0x01 times, which doesn't make much sense. If you know what char the boxes are then you can use something like {[^c]+cc} where c is the character of the box (i guess this is 0x01), to match each "packet".

split使用这里记录的正则表达式。您的代码似乎试图匹配整个字符串常量。SF = 0x01乘以,这没什么意义。如果你知道字符的箱子是什么的话,那么你可以使用诸如{[^ c]+ cc },c是盒子的角色(我想这是0 x01),与每一个“包”。

I think you are trying to use it like the .net String.Split(...) function?

我觉得你是在用。net字符串。split(…)函数?