简体   繁体   English

如何将串联字符串拆分为多个浮点值?

[英]How do I split a concatenated string into multiple floating point values?

I'm a begginer in java I have 我是Java的初学者

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 = 0x01。

But it won't work. 但这是行不通的。

Please help me. 请帮我。

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(0x01)。
  • 0x0f. 0x0f。
  • date (YYMMDD or DDMMYY). 日期(YYMMDD或DDMMYY)。
  • time (HHMMSS). 时间(HHMMSS)。
  • 0x02. 0x02。
  • value (XXXXXX.X). 值(XXXXXX.X)。
  • 0x03. 0x03。
  • 0x04. 0x04。

And, given that they're fixed width, you should probably just use substrings to get the information out. 并且,由于它们的宽度是固定的,您可能应该仅使用子字符串来获取信息。

The JavaDoc of String is helpful here: http://java.sun.com/j2se/1.4.2/docs/api/java/lang/String.html 字符串的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. String.indexOf(String)为您提供特殊子字符串的位置。 your interested in the "." 您对“。”感兴趣 sign. 标志。 So you write 所以你写

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

+1 becuase you want the trailing decimal too. +1是因为您也想要尾随小数。 It will return something 20-ish and will be the last pos of the first number. 它将返回20-ish,并将是第一个数字的最后一个位置。

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;
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()返回一个数组,该数组最多具有limit成员(在这种情况下为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. 您需要阅读Javadocs中的String.split()方法 ...您正在调用带有正则表达式和限制的String.split()版本,但是您将字符串本身作为第一个参数传递 ,没有任何意义。

As Aaron Digulla mentioned, use the other version. 正如Aaron Digulla所述,请使用其他版本。

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. 您的解决方案使正则表达式与字符串混淆。

split uses regular expressions as documented here . split使用正则表达式 ,如此处所述 Your code seems to be trying to match the whole string Constants.SF = 0x01 times, which doesn't make much sense. 您的代码似乎正在尝试匹配整个字符串Constants.SF = 0x01次,这没有多大意义。 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". 如果您知道盒子是什么字符,则可以使用类似{[^ c] + cc}的字符来匹配每个“数据包”,其中c是盒子的字符(我想这是0x01)。

I think you are trying to use it like the .net String.Split(...) function? 我认为您正在尝试像.net String.Split(...)函数一样使用它?

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

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