• Post Reply Bookmark Topic Watch Topic
  • New Topic

Convert the multi line string into single line?  RSS feed

 
Ganesan Ramakrishnan
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I need to convert this below multi line string into single line


Trouver ici les meilleures affaires avec un choix de nombreuses r�f�rences � des prix tr�s avantageux.

L'assurance d'un suivi constant est garanti, y compris apr�s la livraison.

Vous trouverez ais�ment un DVD correspondant � vos attentes dans notre gamme constamment renouvel�e!


any help is appreciated.

regards
Ganesan
 
Carl Pettersson
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Supposing you need to use Java to do it... A line break is represented as the character combination '\n', so if you were to remove these from the text it'd be a single line, right?
Have you written any code that attempts to do this? Can we see it?
 
Ganesan Ramakrishnan
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes i tried with

replaceAll("\n", " ");

but this is not working.

thanks


regards
Ganesan
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Carl Pettersson:
A line break is represented as the character combination '\n'. . .
Not quite. That is operating-system dependent. '\n' is a char literal for the linefeed character (0x000a) commonly called newline.
There is also the carriage return character (0x000d, I think) which can be represented by the '\r' char literal.

As far as I remember, Windows uses "\r\n" for line ends, *nix (Unix, Linux, etc) uses '\n' and OS/X (Mac) uses '\r'.

Also what appears as the 1st argument to String#replaceAll is a regular expression as a String. Go through the Java Tutorial about regular expressions, and you should find it quite easy to work out how to create a regular expression which matches line-end characters. Find the JFlex manual; it has a comprehensive list of potential line-end characters in.
There does not (unfortunately) appear to be a Character#isLineEnd method.

You can find the line-end combination for your computer with the System#getProperty method; I think it is called "line.separator", but you can get an array or Properties with the System#getProperties method and iterate through the array to get the names and check whether that is correct. You will have to cast each char to an int to be able to see their values, otherwise the line end looks like this:







Good luck with it
 
Bhaskar Rao
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are these multiple lines in a single String object? If so, use a StringTokenizer with '\n' as the delimiter to break the string into
three lines and then concatinate them together. (String s1 += strToken.nextToken())
 
Ganesan Ramakrishnan
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks Campbell and others.

I got it. In windows "\n\r" , its working fine.

Thanks and Regards
Ganesan
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bhaskar Rao: StringTokenizer is legacy code: don't use it. Use String#split instead. Use a StringBuilder not String + String for multiple concatenation.
And you obviously didn't read what I said about line terminators.

Ganesan did read it, which is why he got the program to work. Well done. It was '\n\r' however, was it? Good thing you checked.
 
Alan Moore
Ranch Hand
Posts: 262
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Campbell Ritchie:
It was '\n\r' however, was it? Good thing you checked. ;)

Actually, the Windows standard line separator is "\r\n"--a carriage-return followed by a linefeed. However, there's nothing to prevent a file on a Windows machine from having Unix- or Mac-style line separators in it. In fact, you should always assume, no matter what platform you're running on, that the file you're reading can have any style of line separator, or even a mixture of all three styles. A regex like "\r\n?|\n" will match a platform-neutral line separator. But for this task, since you're just getting rid of the line separators anyway, all you need to do is match any number of either of the two characters:

By the way, Campbell, Mac OSX is based on Unix, so its standard line separator is '\n'; it's older MacOS's that use '\r'.
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Alan Moore:
By the way, Campbell, Mac OSX is based on Unix, so its standard line separator is '\n'; it's older MacOS's that use '\r'.


Thank you. I never knew that.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!