Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to find a CRLF in a String

Status
Not open for further replies.

PeteG

Programmer
Feb 23, 2001
144
GB
Hi,
I have some occurrences of carriage line return feed characters in certain strings within my application. When debugging, I can see them as "\r\n" when evaluating the particular String object. however, I can't 'find' "\r\n" using any String search methods; replace, subString, startsWith, that sort of thing. Presumably, the CRLF character is getting transalated into "\r\n" just for my benefit when viewing the string. How can I perform a search for a CRLF character wihin a string and, ultimately, remove it.
Thx for any help.
 
Have you tried "indexOf(...)", as in: myString.indexOf("\r\n")? It seemed to work for me in a little test case I did.

--Dave


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
O Time, Strength, Cash, and Patience! [infinity]
 
Yes, I have. That works if you literally key in "\r\n" as part of a string and test it. It is when the string has been constructed by user input and they have pressed the carriage return key so the CRLF character appears in the string that I can't find it in a search. At the moment, I'm trying to break down the string into a number of char's and then test each one - still having trouble though... :)
 
What happens if you look for them separately instead of both?

Cheers,
Dian
 
String data = ...
data = data.replaceAll("\r", "").replaceAll("\n", "");

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
I've found a solution using Character.isISOControl(char)- ignoring all chars where that returns true. Does the trick quite well but I'll also look into the data.replaceAll("\r", "").replaceAll("\n", "") approach.
Thanks
 
Hi, data.replaceAll("\r", "").replaceAll("\n", "") works as well! Does anyone have an opinion on which is the better approach? I suppose one factor is whether I'm likely to need to ignore other ISO control characters - looking at a list of control characters, I can't say I'm likely to come across any others imbedded in a string....
Thanks again
 
It depends on your needs - whatever works, and is quickest. Personally I'd go with my solution as it is less work, and gives you more control over what you replace.

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top