473,395 Members | 2,436 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

What's wrong with this Regex.Split

string sTest="TEST1||TEST2";
string[] asTest =Regex.Split(sTest, "||" );

I want to get an array with two elements TEST1 and TEST2, but it returs
every char inside the sTest as a seperate array element.

Thanks,
Jianwei
Nov 17 '05 #1
5 1414
Why not just:
string[] asTest =sTest.Split("||".ToCharArray());

--
Adam Clauss
ca*****@tamu.edu

"Jianwei Sun" <js***********@gmail.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
string sTest="TEST1||TEST2";
string[] asTest =Regex.Split(sTest, "||" );

I want to get an array with two elements TEST1 and TEST2, but it returs
every char inside the sTest as a seperate array element.

Thanks,
Jianwei

Nov 17 '05 #2
This will return an array with three elements with one empty string
inside, instead of two. Still, appreciate your reply.
Adam Clauss wrote:
Why not just:
string[] asTest =sTest.Split("||".ToCharArray());

Nov 17 '05 #3
Try escaping the pipes.

string sTest = "TEST1||TEST2";
string[] asTest = Regex.Split(sTest, @"\|\|");

--
Tim Wilson
..Net Compact Framework MVP

"Jianwei Sun" <js***********@gmail.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
string sTest="TEST1||TEST2";
string[] asTest =Regex.Split(sTest, "||" );

I want to get an array with two elements TEST1 and TEST2, but it returs
every char inside the sTest as a seperate array element.

Thanks,
Jianwei

Nov 17 '05 #4
Work beautifully, thanks.

John
Tim Wilson wrote:
Try escaping the pipes.

string sTest = "TEST1||TEST2";
string[] asTest = Regex.Split(sTest, @"\|\|");

Nov 17 '05 #5
Ahhh.... you're absolutely right - sorry!

--
Adam Clauss
ca*****@tamu.edu

"Jianwei Sun" <js***********@gmail.com> wrote in message
news:%2***************@TK2MSFTNGP15.phx.gbl...
This will return an array with three elements with one empty string
inside, instead of two. Still, appreciate your reply.
Adam Clauss wrote:
Why not just:
string[] asTest =sTest.Split("||".ToCharArray());

Nov 17 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: H | last post by:
This is kind of an followup on oneof my previous questions, and it has with RegEx to do. I have a string containing of several words. What would a good regex expression looklike to get one match...
4
by: dchan1 | last post by:
String line = " I am new to c# "; How to use Regex to split the string so that It would return a String array with: token = "I" token = "am" token = "new" token =...
7
by: Ali-R | last post by:
Hi all, I am getting a CSV file like this from our client: "C1","2","12344","Mr","John","Chan","05/07/1976"......... I need to validate **each filed value** against a set of rules ,for...
4
by: Cor | last post by:
Hi Newsgroup, I have given an answer in this newsgroup about a "Replace". There came an answer on that I did not understand, so I have done some tests. I got the idea that someone said,...
3
by: Michael D Murphy | last post by:
Hi, I would like to know how to use Regular Expressions to iteratively return and print the items between the colons in the following string to say the console.. Any help would be appreciated....
5
by: yxq | last post by:
Imports System.Text.RegularExpressions Dim objRegex As Regex MessageBox.Show(objRegex.Split("C:\Test.txt", ":\")(0)) The error will occour, seem that the "\" can not become list separator. If...
7
by: Jordi Rico | last post by:
Hi, I know I can split a string into an array doing this: Dim s As String()=Regex.Split("One-Two-Three","-") So I would have: s(0)="One" s(1)="Two"
8
by: adamisko | last post by:
Hello, what is wrong with this code, I want to divide string to pieces 4 chars length. string tekst = "divide string to 4 chars pieces"; Regex regex = new Regex("(.{1,4})"); string substrings =...
14
by: Stevo | last post by:
If you split a string into an array using the split method, it's not working the way I'd expect it to. That doesn't mean it's wrong of course, but would anyone else agree it's working somewhat...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.