Below code is working to split a text become many texts using SPLIT method if the delimiter is only one character (the example using one character “|”).
But if the delimiter is more than one character (for example using delimiter “|||” like below code), then the program is not working. Please advise how to fix this. Thanks.
My apologies for not replying for this thread any sooner. I've missed this because I checked the OP's reply from the phone and I forget it when I logged in from PC again. :(
Thank you so much Dino for replying the OP and correcting my mistake.
Yes I've missed the MyText part. As Dino said, it always good to refer the MSDN document because if you've looked at it, you would have identify my mistake. :)
fialbz
Member
255 Points
161 Posts
To split a text become many texts using SPLIT method
Feb 26, 2012 11:16 AM|LINK
Below code is working to split a text become many texts using SPLIT method if the delimiter is only one character (the example using one character “|”).
<%@ Page Language="C#" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <script runat="server"> protected void Button1_Click(object sender, EventArgs e) { string MyText = "One|Two|Three|Four|Five"; string[] MyWord = MyText.Split('|'); MyTest1.InnerHtml = MyWord[0]; MyTest2.InnerHtml = MyWord[1]; MyTest3.InnerHtml = MyWord[2]; MyTest4.InnerHtml = MyWord[3]; MyTest5.InnerHtml = MyWord[4]; } </script> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title>Demo Split</title> </head> <body> <form id="form1" runat="server"> <div> <asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" /><br /> <span id="MyTest1" runat="server"></span><br /> <span id="MyTest2" runat="server"></span><br /> <span id="MyTest3" runat="server"></span><br /> <span id="MyTest4" runat="server"></span><br /> <span id="MyTest5" runat="server"></span><br /> </div> </form> </body> </html>But if the delimiter is more than one character (for example using delimiter “|||” like below code), then the program is not working. Please advise how to fix this. Thanks.
protected void Button1_Click(object sender, EventArgs e) { string MyText = "One|||Two|||Three|||Four|||Five"; string[] MyWord = MyText.Split('|||'); MyTest1.InnerHtml = MyWord[0]; MyTest2.InnerHtml = MyWord[1]; MyTest3.InnerHtml = MyWord[2]; MyTest4.InnerHtml = MyWord[3]; MyTest5.InnerHtml = MyWord[4]; }Ruchira
All-Star
44216 Points
7184 Posts
MVP
Re: To split a text become many texts using SPLIT method
Feb 26, 2012 12:04 PM|LINK
Change it to
string[] MyWord = Split(new string[]{ "|||" }, StringSplitOptions.None);
My Tech blog | My YouTube ChannelPlease 'Mark as Answer' if this post helps you.fialbz
Member
255 Points
161 Posts
Re: To split a text become many texts using SPLIT method
Feb 26, 2012 01:14 PM|LINK
I am using Visual Studio 2008, and your line-code is not working (error).
Is it due to typo or is it not supported in NET 3.5? Any other suggestion?
Please advice. Thanks.
Dino He - MS...
Star
8068 Points
1023 Posts
Microsoft
Re: To split a text become many texts using SPLIT method
Feb 28, 2012 01:06 AM|LINK
Hi
Ruchira is right but he ignore the MyText...
string[] MyWord = MyText.Split(new string[] { "|||" }, StringSplitOptions.None);
And I suggest you use msdn for some method usage.
http://msdn.microsoft.com/en-us/library/tabh47cf.aspx
It always provide some good example for you which will save you a lot of time to wait for answer.
Thanks
If you have any feedback about my replies, please contact msdnmg@microsoft.com
Microsoft One Code Framework
Ruchira
All-Star
44216 Points
7184 Posts
MVP
Re: To split a text become many texts using SPLIT method
Feb 28, 2012 04:46 AM|LINK
Hello Dino and fialbz,
My apologies for not replying for this thread any sooner. I've missed this because I checked the OP's reply from the phone and I forget it when I logged in from PC again. :(
Thank you so much Dino for replying the OP and correcting my mistake.
Yes I've missed the MyText part. As Dino said, it always good to refer the MSDN document because if you've looked at it, you would have identify my mistake. :)
My Tech blog | My YouTube ChannelPlease 'Mark as Answer' if this post helps you.