Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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 StackOverflow.com –  Andrey Markeev Feb 1 '12 at 12:21
add comment

2 Answers

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
add comment

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];
                linesInArray++;
            }
        }

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
add comment

Your Answer

 
discard

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.