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 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);
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
"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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Senthil |
last post by:
Code
----------------------
string Line = "\"A\",\"B\",\"C\",\"D\"";
string Line2 = Line.Replace("\",\"","\"\",\"\"");
string CSVColumns = Line2.Split("\",\"".ToCharArray());
|
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"
|
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,...
|
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() =...
|
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 =...
|
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...
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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,...
|
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...
|
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,...
|
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: 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,...
| |