473,396 Members | 1,702 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,396 software developers and data experts.

Split strings

Tem
What would be the best way to split a string into a string array. with the
following possible inputs?

abc1 abc2 abc3 -single space
abc1 abc2 abc3 -multiple spaces
abc1,abc2,abc3 -comma
abc1, abc2, abc3 -comma + space
abc1;abc2;abc3 -semicolon
abc1; abc2; abc3 -semicolon + space

Thanks

Tem

Dec 28 '07 #1
4 2366
"Tem" <te*****@yahoo.comwrote in message
news:ee**************@TK2MSFTNGP05.phx.gbl...
What would be the best way to split a string into a string array. with the
following possible inputs?

abc1 abc2 abc3 -single space
abc1 abc2 abc3 -multiple spaces
abc1,abc2,abc3 -comma
abc1, abc2, abc3 -comma + space
abc1;abc2;abc3 -semicolon
abc1; abc2; abc3 -semicolon + space

string[] pieces = theString.Split(new char[]{' ',',',';'},
StringSplitOptions.RemoveEmptyEntries);
Dec 28 '07 #2
Hi Tem,

you can use this

[String.Split Method]
http://msdn2.microsoft.com/en-us/lib...ing.split.aspx

But you also can use RegExp Class, but the Split Method
is ok here,...

Regards

Kerem

--
-----------------------
Beste Grüsse / Best regards / Votre bien devoue
Kerem Gümrükcü
Microsoft Live Space: http://kerem-g.spaces.live.com/
Latest Open-Source Projects: http://entwicklung.junetz.de
-----------------------
"This reply is provided as is, without warranty express or implied."
Dec 28 '07 #3
Here is one way:

string s = "ab, cd;ef gg";
string[] array = s.Split(" ,;".ToCharArray(),
StringSplitOptions.RemoveEmptyEntries);
foreach(string el in array)
Console.WriteLine("{0} count:{1}",el, el.Length);
W

"Tem" <te*****@yahoo.comwrote in message
news:ee**************@TK2MSFTNGP05.phx.gbl...
What would be the best way to split a string into a string array. with the
following possible inputs?

abc1 abc2 abc3 -single space
abc1 abc2 abc3 -multiple spaces
abc1,abc2,abc3 -comma
abc1, abc2, abc3 -comma + space
abc1;abc2;abc3 -semicolon
abc1; abc2; abc3 -semicolon + space

Thanks

Tem
Dec 28 '07 #4
"Alberto Poblacion" <ea******************************@poblacion.org>
wrote in news:Om*************@TK2MSFTNGP06.phx.gbl:
>What would be the best way to split a string into a string array.
with the following possible inputs?

abc1 abc2 abc3 -single space
abc1 abc2 abc3 -multiple spaces
abc1,abc2,abc3 -comma
abc1, abc2, abc3 -comma + space
abc1;abc2;abc3 -semicolon
abc1; abc2; abc3 -semicolon + space


string[] pieces = theString.Split(new char[]{' ',',',';'},
StringSplitOptions.RemoveEmptyEntries);
or:

using System.Text.RegularExpressions;
string[] pieces = Regex.Split(theString, "[ ,;]+");

although both of these will also split things with any number (1 or more)
of any of the three characters (space, comma, semicolon), not just the
specific examples you list.

-mdb
Dec 28 '07 #5

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

Similar topics

6
by: Senthil | last post by:
Code ---------------------- string Line = "\"A\",\"B\",\"C\",\"D\""; string Line2 = Line.Replace("\",\"","\"\",\"\""); string CSVColumns = Line2.Split("\",\"".ToCharArray());
4
by: Itzik | last post by:
can i split this string string str = "aa a - bb-b - ccc" with this delimiter string del = " - " i want recieve 3 items : "aa a" , "bb-b" , "ccc"
14
by: Ron | last post by:
Hello, I am trying to parse a string on the newline char. I guess vbCrLf is a string constant. How can I parse my string - data - on the newline char? .... data += ASCII.GetString(buffer, 0,...
5
by: kurt sune | last post by:
The code: Dim aLine As String = "cat" & vbNewLine & "dog" & vbNewLine & "fox" & vbNewLine Dim csvColumns1 As String() = aLine.Split(vbNewLine, vbCr, vbLf) Dim csvColumns2 As String() =...
2
by: Digital Fart | last post by:
following code would split a string "a != b" into 2 strings "a" and "b". but is there a way to know what seperator was used? string charSeparators = { "=", ">=", "<=" , "!=" }; string s1 =...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.