Split a string on a string not a character

11,261

Solution 1

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.

Solution 2

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

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

Solution 3

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...

Solution 4

Try this:

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

Solution 5

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

string test = "hello::there";
string[] array = test.Split(new string[]{ "::" }, StringSplitOptions.RemoveEmptyEntries);
Share:
11,261
John Smith
Author by

John Smith

Full Stack Developer =&gt; Angular 2+, Angularjs, C#, SQL, Jquery, Javascript

Updated on June 01, 2022

Comments

  • John Smith
    John Smith almost 2 years

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

    e.row.cells[1].Text.Split("htmltag")
    
  • Prof. Falken
    Prof. Falken over 5 years
    I get "identifier expected" on the New []