File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Identifying a line feed in a String Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Identifying a line feed in a String" Watch "Identifying a line feed in a String" New topic
Author

Identifying a line feed in a String

Manju Krishna
Ranch Hand

Joined: Apr 09, 2009
Posts: 48
    
    1
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
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

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.


[LEARNING bLOG] | [Freelance Web Designer] | [and "Rohan" is part of my surname]
Manju Krishna
Ranch Hand

Joined: Apr 09, 2009
Posts: 48
    
    1
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.
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

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.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41867
    
  63
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.


Ping & DNS - my free Android networking tools app
Manju Krishna
Ranch Hand

Joined: Apr 09, 2009
Posts: 48
    
    1
The reg exp which I had used in String split inside the if loop worked. i.e



match.matches() was returning false anyway
Manju Krishna
Ranch Hand

Joined: Apr 09, 2009
Posts: 48
    
    1
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
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19696
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19696
    
  20

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.
Manju Krishna
Ranch Hand

Joined: Apr 09, 2009
Posts: 48
    
    1


This piece of code works..
Thanks everybody for your inputs.
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Identifying a line feed in a String