Forums Register Login

Identifying a line feed in a String

+Pie Number of slices to send: Send
Hello,

I have a XML file which I parse and write the value of the node to a String. The XML tag is like,

<description>Search - Query Tab
Hide the Query Tab</description>

When I write this to a String I need to identify if there is any line feed in the given String and replace it with hard coded "\n" in it. (NOTE : Only if there is a line feed in it)
I tried to Pattern match to identify if there are any \r or \n.

descriptionBaseTerm is the <description> tag value



The if loop where I split the term using the same reg exp works fine returns the String as
"Search - Query Tab\n Hide the Query Tab". Which is the desired output.

Is there any other way to acheieve this?

Please help.
Thanks,
Manju
+Pie Number of slices to send: Send
Well, the Pattern & Matcher is the best way, but still, How about this approach ?

Use String#split("\r|\n|\r\n") and if returned array size is greater than one, you can iterate and append the each array element.
+Pie Number of slices to send: Send
I have done it the same way as you have quoted.
But I was wondering why Pattern Matcher didnt work.Also wanted to know if there are any other syntax to find line feed using Patterns.
+Pie Number of slices to send: Send
 

Manju Krishna wrote: But I was wondering why Pattern Matcher didnt work.

But in your original post you say, it(P&M) works

Manju Krishna wrote: Also wanted to know if there are any other syntax to find line feed using Patterns.

I don't think so.
+Pie Number of slices to send: Send
Matcher.matches() checks whether the entire region is matched by the regexp, not whether the pattern matches anywhere in the region; Matcher.find() does that.
+Pie Number of slices to send: Send
The reg exp which I had used in String split inside the if loop worked. i.e



match.matches() was returning false anyway
+Pie Number of slices to send: Send
Hello Ulf,
i tried Matcher find.


This is not appending the character "\n" to end of each finds. It just returns the given String in single line
+Pie Number of slices to send: Send
Why not use String.replaceAll: This uses Pattern and Matcher (with find(), not matches()) internally.

Hmm, not even find(), but Matcher.replaceAll. That ones does use find() internally though, in a similar way as Manju's code.
+Pie Number of slices to send: Send
 

Manju Krishna wrote:

This is not appending the character "\n" to end of each finds. It just returns the given String in single line


Pattern.MULTILINE is not the flag you are looking for. That will make sure that ^ and $ are found on each line. You want Pattern.DOTALL.
+Pie Number of slices to send: Send


This piece of code works..
Thanks everybody for your inputs.
+Pie Number of slices to send: Send
 

Ulf Dittmer wrote:Matcher.matches() checks whether the entire region is matched by the regexp, not whether the pattern matches anywhere in the region; Matcher.find() does that.


Hmm, I missed that .. Thanks Ulf.
Your mother is a hamster and your father smells of tiny ads!
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 5097 times.
Similar Threads
Problem writing a string into a file
Java string object value to javascript
Mock exams for JSP
Sun Certified Web Component Developer
New forum for Sun Certified Web Component Developer
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 19, 2024 00:28:49.