开发者

Split a string on a string not a character

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

e.row.cells[1].Text.Spli开发者_开发知识库t("htmltag")


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.


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

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


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


Try this:

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


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

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


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);


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)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜