cancel
Showing results for 
Search instead for 
Did you mean: 

Determining the number of new lines in a string

bjorn-henrik_zink
Active Participant
0 Kudos

Hi,

is there a way to determine the number of line breaks in a string. For example, I receive the content of a textarea and want to find out how many lines there is in the text.

Thanks.

Accepted Solutions (0)

Answers (3)

Answers (3)

Former Member
0 Kudos

Java is generally designed to support non-hardcoding alternatives to this kind of approach.

Instead of hardcoding (hardcoding baaad ) the carriage values into the code please at least try to use the following System property.

System.getProperty("line.separator");

This should work for most JVM supporting platforms and you shouldn't need to worry if someone decides one day to change the \r or \n to something different.

If it doesn't work and you are sure that it is due to this then hardcode it instead.

Btw. didn't the Point rewarding program already stop? I remember receiving a related e-mail about it that it deteriorated the quality of these forums.

Cheers,

Kalle

detlev_beutner
Active Contributor
0 Kudos

Hi Kalle,

Stefan has already mentioned this, and I have already answered to this - while in general I am one of the heaviest fans of the No-Hardcoded-Strings-Within-Algorithmic-Code-Party, in this case, to follow the Manifest of this imaginary party, you can define somewhere as a constant LINE_BREAKER = "\r\n".

Just to use the system line separator is something between bad and wrong. If you parse a text stream, you must be absolutely sure that this stream is created with line separators of the system the software is running on. Then it would be OK.

Generally, you cannot know from where the stream comes from. If you receive it via internet, via file-system, not knowing from where it was imported etc pp - you have to make sure to catch the different line breaking possibilities, and, once again, this is done by using \r\n as delimiter string.

About the points: No, not stopped, and no, most people have found out that for the forums the quality has not been deteriorated but improved (this doesn't hold for the weblogs).

Best regards

Detlev

Former Member
0 Kudos

Heh, funny. I completely missed that middle, Stefans, response with this dynamic parameter. I must've been too concentrated on your answer. Early mornings, late nights...

Still, by using the hardcoded method you suggest with a valid, but limited point, you do bind the solution to assume that the linefeed is one of these two (or their combination).

Practically it is surely valid at the moment in most cases, but it is still not correct from design perspective, especially in Java that runs on a lot of platforms.

Would you accept that it could be valid to add also the checking of the dynamic line-break in addition to your checks? This way you would catch it both in the cases you mention as well as in any obscure format an operating system might want to use (now or in the future)?

O.k. must've also missunderstood the mail regarding the end of point gathering. Personally I would rather have a community based on something else than points, but I guess it does serve as a motivator in some cases.

Cheers,

Kalle

detlev_beutner
Active Contributor
0 Kudos

Hi Kalle,

> by using the hardcoded method you suggest with a

> valid, but limited point, you do bind the solution to

> assume that the linefeed is one of these two (or their

> combination).

Yes - as Java does, and as it works - at least at the moment - for the whole world, and as it will probably do forever. Just compare BufferedReader.readLine() or LineNumberReader.

Sorry, your arguments just do not work.

If you are happy, you can define


public static final String LINE_BREAKERS = "rn" + System.getProperty("line.separator");

But <i>if</i> you really expect changes about line breaking conventions in the future, also this is not a solution. The changes may appear on a third system, and the Java code may be confronted with such a source without the system Java runs on behaving this way.

So I insist that my proposed solution is the most straight one, is Java-conform, is real-world-orientated and just - right.

Best regards

Detlev

bjorn-henrik_zink
Active Participant
0 Kudos

Here is how you do it:

StringTokenizer st = new StringTokenizer("string", "\n");

int rows = st.countTokens();

detlev_beutner
Active Contributor
0 Kudos

Hi Elvez,

almost right, see my msg (if a line break is /n or /r or a combination of both) is system dependent.

Please consider awarding points for helpful answers like this just by pressing the yellow star and choosing the corresponding amount of points.

Thanks in advance

Detlev

Former Member
0 Kudos

Hi Elvez,

here is how you do it regardless of the OS:

StringTokenizer st = new StringTokenizer(yourStringHere, System.getProperty("line.separator"));
int rows = st.countTokens();

Regards

Stefan

PS: You will miss the last line, if it doesn't end with line feed.

detlev_beutner
Active Contributor
0 Kudos

Hi Stefan,

sorry, but somehow "wrong" again:

1.) Setting in the system line separator won't do it if the source doesn't come from the system! For line breaks in all systems are \n, \r or \r\n, with a delimiter string "\r\n" you'll catch them all.

2.) It doesn't matter if the last line ends with a line break or not: A\rB\r returns two tokens, A\rB will do it too. The only question is the opposite: Will you miss the last line if it ends with line feed?! For you could await A\rB\r to return three instead of two: First line is "A", second line is "B", third line is empty (but existent).

Hope it helps

Detlev

Former Member
0 Kudos

Hi Detlev,

of course you're right and i'm ashamed. Must be the (attention! 1:1 translation) "glowing wine" in the "preXMas time".

Thanks and best regards,

Stefan

PS: BTW, congratulations for being 2nd (and ahead of me) in the forum's top scorer list.

detlev_beutner
Active Contributor
0 Kudos

Hi Elvez,

for line breaks can be Carriage Return, Line Feed, and a combination of both, the most simple way should be to create a StringTokenizer and count the tokens (ie lines):


StringTokenizer strTok = new StringTokenizer(yourString, "/r/n");
int nrOfLines = str.countTokens();

Hope it helps & have a nice week

Detlev