Alan2
Alan2

Reputation: 24562

How can I check the number of line feeds in a string?

I have a field that I use to hold text. I am going to hold the text in a HTML textarea but I need to set the number of lines.

How can I count the number of line feeds in a string so I can set the textarea rows?

Upvotes: 0

Views: 169

Answers (7)

Guffa
Guffa

Reputation: 700232

A line break is one or two characters, depending on the system. On a windows system it's the two character combination \r\n, but you can look for only one of them when counting them.

As a string is enumerable, you can use the Count extension method:

int cnt = str.Count(c => c == '\n');

Upvotes: 1

Richard Ev
Richard Ev

Reputation: 54087

You can use String.Split:

int lines = stringVariable.Split('\n').Length + 1;

Or, you could use Enumerable.Count:

int lines = stringVariable.Count(s => s == '\n') + 1;

(both of these examples assume that your string doesn't contain a trailing newline.)

However, this won't necessarily give you what you need if any of your rows of text are wider than your textarea, since their text will wrap.

Upvotes: 0

Phillip Schmidt
Phillip Schmidt

Reputation: 8818

It will depend on the number of cols in the text area. Try something like this:

StringBuilder mystring = new StringBuilder(@"Hey this is a fairly long string which I used in this /
example to show how long strings might be broken up into different lines based on how  /
wide your text area is. What is going to happen is we are about to insert a newline  /
after however many characters the textarea is wide. We'll also count the number of   /
newlines that we put in, and that number plus one will be the number we need for the  /
textarea!");

int columnCounter = 0;
int lineCounter= 1; //1 for the first line
const int COLUMNS_IN_TEXT_AREA = howeverManyColsYouHave;
for(int i = 0; i<mystring.Length;i++) //set to less than mystring.length, just in case the string were really short.
{
    if(columnCounter >= COLUMNS_IN_TEXTAREA)
    {
        mystring.Insert(i,"\n");
        lineCounter ++;
    }
}

now send the stringbuilder and line count as JSon or something to your view and voila!

Upvotes: -1

Alex K.
Alex K.

Reputation: 175766

Another way;

 int lines = str.Length - str.Replace("\n", "").Length;

Upvotes: 0

Patrick McDonald
Patrick McDonald

Reputation: 65421

Using LINQ:

int lineCount = text.Count(c => c == '\n');

Upvotes: 1

parapura rajkumar
parapura rajkumar

Reputation: 24403

You can also use regex

int count =  new Regex(@"\n").Matches(inputstr).Count

Upvotes: 0

Aghilas Yakoub
Aghilas Yakoub

Reputation: 28970

try this code extension method.

public class static Extension
{
    public static int CountStringOccurrences(this string text, string pattern)
        {
        // Loop through all instances of the string 'text'.
        int count = 0;
        int i = 0;
        while ((i = text.IndexOf(pattern, i)) != -1)
        {
            i += pattern.Length;
            count++;
        }
        return count;
        }
}

Use

var text = "Sam dfdfgdf Sam.";
text.CountStringOccurrences("Sam"));

Upvotes: 0

Related Questions