views:

284

answers:

7

I want split a gridview row on an html tag. How can i do this preferably in C#??

e.row.cells[1].Text.Split("htmltag")

Thanks in advance

+6  A: 

Yes. Use the overload

String.Split(String[], StringSplitOptions)

or

String.Split(String[], int, StringSplitOptions)

Example:

var split = e.row.cells[1].Text.Split(
                new[] { "</b>" },
                StringSplitOptions.RemoveEmptyEntries
            );

But do heed StrixVaria's comment above. Parsing HTML is nasty so unless you're an expert offload that work to someone else.

Jason
+3  A: 

Try this:

e.Row.Cells[1].Text.Split( new string[] { "</b>" }, StringSplitOptions.None );
csm8118
+3  A: 

One of the overloads of String.Split takes a String[] and a StringSplitOptions - this is the overload you want:

e.row.cells[1].Text.Split(new string[] { "</b>" }, StringSplitOptions.None);

or

e.row.cells[1].Text.Split(new string[] { "</b>" }, StringSplitOptions.RemoveEmptyEntries);

depending on what you want done with empty entries (ie when one delimiter immediately follows another).

However, I would urge you to heed @StrixVaria's comment...

AakashM
A: 

To split a string with a string, you would use this..

string test = "hello::there";
string[] array = test.Split(new string[]{ "::" }, StringSplitOptions.RemoveEmptyEntries);
Eclipsed4utoo
A: 

Use one of the overloads of string.Split(...). But as the comment says, perhaps another method of doing it would be preferrable.

e.row.cells[1].Text.Split(new [] { "</b>"}, StringSplitOptions.None);
aanund
A: 

In addition to string.split, you can use Regex.Split (in System.Text.RegularExpressions):

string[] lines = Regex.Split(.row.cells[1].Text, "htmlTag");
rosscj2533
A: 

This is one of those times where I go old school VB and use just use:

Split(expression, delimiter)

or in C#

Microsoft.VisualBasic.Strings.Split(expression,delimiter)
Chris Haas