• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

remove carriage return from string

 
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

I need to remove all "ENTERs" present from a string, may that be \n or \r or anything. I did search this forum but could not find a suitable answer. I even googled but to no avail.

In the code, I have used System.getParameter("line.separator") to find the line separator and remove the same from the string. The problem is that the java web application when deployed in a linux box does not understand ENTERs pressed from a Windows client box.
So, if Server is Windows and Client is also Windows, it works fine. If server is Linux and Client is Windows, it does not work.

I do not want to hard code every possible line separator. Is there a way to find an ENTER in a string.

Regards,
Vijay.
 
author
Posts: 23958
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I do not want to hard code every possible line separator. Is there a way to find an ENTER in a string.



Well, there isn't many "every possible line separators"...

Unix uses the line feed, aka LF, with the ascii value of 10. Windows uses the carriage return / line feed combo (two characters). The carriage return, aka CR, has an ascii value of 13. And some systems uses just the CR.

If you want to get rid of the line separators. Get rid of all LFs and CRs, and you'll do it for all systems.

Henry
 
Marshal
Posts: 80214
423
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You need to go through the regular expressions classes, eg Matcher, Pattern, and find out what is used for line ends. Then you can use the replaceAll method of String, particularly if you have worked out how to get these line end characters into a regular expression.
 
Vijay Raj
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Henry.

So, there is no other way of removing line separators other than doing -



Which one would be better - solving with regular expression or the one above? Won't the regular expression solution un-necessary load the classes?

Regards,
Vijay.
 
Henry Wong
author
Posts: 23958
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Which one would be better - solving with regular expression or the one above? Won't the regular expression solution un-necessary load the classes?



The one above IS a regex based solution. The replaceAll() method is just a convenience method that calls the regex replace methods.... And you can combine the two statements like so...



Also, note that strings are not mutable. So, you need to assign the result back to the "str" reference, if you actually want to change it.

Henry
 
Vijay Raj
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Henry.
 
Ranch Hand
Posts: 266
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henry Wong wrote:

Which one would be better - solving with regular expression or the one above? Won't the regular expression solution un-necessary load the classes?



The one above IS a regex based solution. The replaceAll() method is just a convenience method that calls the regex replace methods.... And you can combine the two statements like so...


...



Note that although this works, you don't need to "double escape" them: the regex "\n" matches "\n". So, you could/should just do:

 
Henry Wong
author
Posts: 23958
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Note that although this works, you don't need to "double escape" them: the regex "\n" matches "\n". So, you could/should just do:



I am not too sure if I completely agree with this. I agree that it will work. But in the first case, I am sending a "\n" string to the regex engine. And in the second case, I am sending an ASCII 10 character to the regex engine. It should work as the regex engine will replace the "\n" with the LF character anyway.

But I would feel more comfortable if I send readible text to the regex engine than control characters -- whether it works or not.

Henry

 
Piet Verdriet
Ranch Hand
Posts: 266
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henry Wong wrote:

Note that although this works, you don't need to "double escape" them: the regex "\n" matches "\n". So, you could/should just do:



I am not too sure if I completely agree with this. I agree that it will work. But in the first case, I am sending a "\n" string to the regex engine. And in the second case, I am sending an ASCII 10 character to the regex engine. It should work as the regex engine will replace the "\n" with the LF character anyway.

But I would feel more comfortable if I send readible text to the regex engine than control characters -- whether it works or not.

Henry



Hmmm... You got me thinking there.



<cricket sounds />



Well, I can't really figure out why one should be prefered over the other. Let's hope our true regex-monk, Alan Moore, has something to contribute about this subject.
; )
 
Ranch Hand
Posts: 262
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
AFAIK, it only matters when the regex is coming from something outside the program, like a file or the console, where it can be difficult or impossible to input control characters like linefeeds. Then you have to use the escape sequence.
 
Master Rancher
Posts: 5116
82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There are also a variety of regex editors, testers, and other plugins available for various IDEs. These usually are able to convert between Java string literals and the regex as seen by the regex engine. For people who use such plugins, it's nice to see the escape sequences rather than actual newlines, returns, tabs, etc. Otherwise you can't easily tell whether a line break is a \r, \n, or some combination. Or whether other whitespace is really a space, or perhaps a tab. So if you use such plugins, or if your code will be seen and used by people who use these plugins, it's probably a good idea to use the extra escapes. In my opinion, of course.
 
Piet Verdriet
Ranch Hand
Posts: 266
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Alan, Mike, thank you both!
 
Ranch Hand
Posts: 275
jQuery Eclipse IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Vijay,

What did you finally use ?
I am facing exactly the same problem.
 
Campbell Ritchie
Marshal
Posts: 80214
423
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have you tried what was suggested? If you need more help, please start a new thread; you are unlikely to get a useful reply from such an old thread.
 
I'm full of tinier men! And a tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic