Jump to content

  • Log In with Google      Sign In   
  • Create Account

FREE SOFTWARE GIVEAWAY

We have 4 x Pro Licences (valued at $59 each) for 2d modular animation software Spriter to give away in this Thursday's GDNet Direct email newsletter.


Read more in this forum topic or make sure you're signed up (from the right-hand sidebar on the homepage) and read Thursday's newsletter to get in the running!


Parsing String sometimes doesn't work.


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
2 replies to this topic

#1 Belgium   Members   -  Reputation: 123

Like
0Likes
Like

Posted 22 March 2012 - 12:57 PM

Hello,

I am using the following to parse a String which has spaces as a delimiter.

List<String> elements = Arrays.asList(text.trim().split("\\s+"));

I've been using that line a lot for String parsing and have never had a problem until now.

It works as I desire for the String " DEVICE: CPM PROC UNIT: 3 PAIR: B ".
The result is "DEVICE:", "CPM", "PROC", etc.

It does not work for this String " DEVICE: SPAN UNIT: 2370 ".
The results are "DEVICE:", "", "SPAN". There's an empty string where I expect to see "SPAN".

This also does not work for this string, with similar results, " DEVICE: ALMCRD UNIT: 56"

I've also tried String temp[] = text.trim().split("[ ]+"). Same thing.

I'm lost. I don't know what I am missing here.


Edit: My original post ate a number of spaces in my examples between "DEVICE: " and the next element. There are more than 1 spaces between this texts. Let's see if changing the font helps.

Edit2: Loading the source file for the text in a hex editor, I see there is a 0x00 character in the place of one of the spaces. So, the three "spaces" between "DEVICE:" and "SPAN" is actually 0x20 0x00 0x20. That 0x00 character doesn't show up in the Strings that parse correctly. Isn't 0x00 considered whitespace and shouldn't "\\s+" include it?

Sponsor:

#2 serioso   Members   -  Reputation: 414

Like
1Likes
Like

Posted 22 March 2012 - 04:43 PM

The null character is not considered whitespace in Java. You'll either have to adjust your regex to "[\0\\s]+", or clean up your input data files (if you have that option).

#3 Belgium   Members   -  Reputation: 123

Like
0Likes
Like

Posted 23 March 2012 - 08:48 AM

The null character is not considered whitespace in Java. You'll either have to adjust your regex to "[\0\\s]+", or clean up your input data files (if you have that option).


Thanks! I changed it to "[\\s\\x00]+" and that seems to work.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS