SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

From an answer from earlier post to get the first two lines in a rich textbox in sharepoint 2010 in a visual webpart i m using

var firstTwoLines = String.Join("\n", MyRichTextBox.Text.Split('\n').Take(2).ToArray());

..what if there is an image starting from the 2nd line..what condition should i use to check if there is an image present and then skip it?

share|improve this question
Do you need to skip only the image tag itself (<img ... />), or you need to skip the whole line which contains it? – Andrey Markeev Feb 1 '12 at 8:30
hi omlin, i would like to skip the imgae tag itself – spStacker Feb 1 '12 at 12:05
Ok, I added the answer, but can't resist mentioning that the question actually doesn't relate to SharePoint. Please, post pure C# questions like this to – Andrey Markeev Feb 1 '12 at 12:21

There may be a less manual way but you could split and then check each lines until you reach the number of lines you need.

        int linesInArray = 0;
        int linesNeeded = 2;
        string[] linesOfText = MyRichTextBox.Text.Split('\n');
        string[] parsedLines = new string[linesNeeded];

        for (int lineCount = 0; lineCount < linesOfText.Length || linesInArray == linesNeeded; lineCount++)
            //This is assuming rich text box images are html which I believe they are
            if (linesOfText[lineCount].Contains("<img"))
                parsedLines[linesInArray] = linesOfText[lineCount];

The code implementation itself is a little crude but you could see how you could adjust it to fit your needs and be of more "production quality."

share|improve this answer

Just add the following line after your original code:

firstTwoLines = Regex.Replace(firstTwoLines, "<img.*?>", string.Empty);

(you will need to add using System.Text.RegularExpressions; to the top of the file)

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.