aspose file tools
The moose likes Beginning Java and the fly likes Spliting a string on a second space and not all spaces Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "Spliting a string on a second space and not all spaces" Watch "Spliting a string on a second space and not all spaces" New topic
Author

Spliting a string on a second space and not all spaces

Mike Bates
Ranch Hand

Joined: Sep 19, 2009
Posts: 81
I have a series of stings that I want to get values for but there are extra spaces that cause extra gyrations I am hoping someone would be able to help me around.

The string has embedded newlines and returns which I split on first to get to the specific lines and now need to get the number on the line and associate it with a variable.

I tried the following but it drops a number off.



Giving me:
Fred is
00
Willma

Barney is
99

Any thoughts?

Mike
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19216

The number is part of the splitting regex and will therefore not show up in the non-matched parts. Take a look at the Javadoc of Pattern for how (positive) lookahead works.


SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
Mike Bates
Ranch Hand

Joined: Sep 19, 2009
Posts: 81
Rob,

Thanks that was the guide point I needed.

This worked:


Mike
Jelle Klap
Bartender

Joined: Mar 10, 2008
Posts: 1409

Seems to me like the line terminator pattern doesn't quite cut it either.

Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life.
Mike Bates
Ranch Hand

Joined: Sep 19, 2009
Posts: 81
Jelle,


It seemed to work for me, did I miss something as to why does not quite work?

Mike
Jelle Klap
Bartender

Joined: Mar 10, 2008
Posts: 1409

For the hard-coded String it'll work like a charm, unfortunately it won't successfully match line terminators for text files created on Windows, Unix (based) and older Mac OS operating systems. See http://en.wikipedia.org/wiki/Newline.
Mike Bates
Ranch Hand

Joined: Sep 19, 2009
Posts: 81
Okay, thanks. I was not worried about OSs. (though I did not say that upfront.) And I did not catch that as an issue or even think about it, which I need to remember.

I am pulling data from a serial device. The coded strings are surrounded by newlines and returns.

Thanks
Mike
Ulrika Tingle
Ranch Hand

Joined: Nov 24, 2009
Posts: 92
Mike Bates wrote:Any thoughts?


Sometimes it's easier and faster to do some old-fasioned parsing.

 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Spliting a string on a second space and not all spaces
 
Similar Threads
convert pseudo code to java
modifying file data
UNIX line break problem
Swap sublist of an arraylist with the sublist of another arraylist
Pattern & Matcher - how to get any combination?