Skip to Content

Determining the number of new lines in a string

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.

Add a comment
10|10000 characters needed characters exceeded

Related questions

3 Answers

  • Posted on Dec 13, 2004 at 12:26 PM

    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

    Add a comment
    10|10000 characters needed characters exceeded

  • Posted on Dec 13, 2004 at 12:27 PM

    Here is how you do it:

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

    int rows = st.countTokens();

    Add a comment
    10|10000 characters needed characters exceeded

    • Former Member Detlev Beutner

      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.

  • author's profile photo Former Member
    Former Member
    Posted on Dec 14, 2004 at 09:13 AM

    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

    Add a comment
    10|10000 characters needed characters exceeded

    • 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

Before answering

You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
You must be Logged in to submit an answer.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.