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"
i use this syntax :
string str = "aa a - bb-b - ccc";
string del = " - ";
string[] temp = str.Split(del.ToCharArray());
now is return me 9 items : "aa" , "a" , "" , "" , "bb" , "b" , "" , "" ,
"ccc"
Thanks 4 5739
"Itzik" wrote... 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"
i use this syntax : string str = "aa a - bb-b - ccc";
string del = " - ";
string[] temp = str.Split(del.ToCharArray());
Here's one mistake. What you do here is to split the *delimiter* into an
array of chars. The resulting array of ToCharArray is:
{' ', '-', ' ')
Which means that you split the string by both spaces and hyphens.
Another "problem" is that String.Split only works with just characters as
delimiters, but underneath it really makes use of another method:
Regex.Split.
With that you can use the *string* "del" as the delimiter, e.g.:
using System.Text.RegularExpressions;
...
string str = "aa a - bb-b - ccc";
string del = " - ";
string[] temp = Regex.Split(str, del);
// Bjorn A
Itzik,
As Bjorn stated, String.Split splits based on individual characters. If you
want to split based on words I would recommend Strings.Split or RegEx.Split.
There are three Split functions in .NET:
Use Microsoft.VisualBasic.Strings.Split if you need to split a string based
on a specific word (string). It is the Split function from VB6.
Use System.String.Split if you need to split a string based on a collection
of specific characters. Each individual character is its own delimiter.
Use System.Text.RegularExpressions.RegEx.Split to split based
on matching patterns.
By referencing the Microsoft.VisualBasic assembly in C# you can use the
Strings.Split function to split a string based on a word.
Something like: string str = "aa a - bb-b - ccc"
string del = " - "
string [] fields = Strings.Split(str, del, -1, CompareMethod.Binary);
Hope this helps
Jay
"Itzik" <it****@pisgasys.co.il> wrote in message
news:ej**************@tk2msftngp13.phx.gbl... 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"
i use this syntax : string str = "aa a - bb-b - ccc";
string del = " - ";
string[] temp = str.Split(del.ToCharArray());
now is return me 9 items : "aa" , "a" , "" , "" , "bb" , "b" , "" , "" , "ccc" Thanks http://weblogs.asp.net/justin_rogers.../14/89545.aspx
A very fast string splitter that thwomps both Regex, because of the
overhead, and VB.Split, because they have some weird heuristics.
--
Justin Rogers
DigiTec Web Consultants, LLC.
Blog: http://weblogs.asp.net/justin_rogers
"Itzik" <it****@pisgasys.co.il> wrote in message
news:ej**************@tk2msftngp13.phx.gbl... 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"
i use this syntax : string str = "aa a - bb-b - ccc";
string del = " - ";
string[] temp = str.Split(del.ToCharArray());
now is return me 9 items : "aa" , "a" , "" , "" , "bb" , "b" , "" , "" , "ccc" Thanks
Justin,
COOL! I'll have to add that one to my little FAQ.
Thanks
Jay
"Justin Rogers" <Ju****@games4dotnet.com> wrote in message
news:OC**************@TK2MSFTNGP09.phx.gbl... http://weblogs.asp.net/justin_rogers.../14/89545.aspx
A very fast string splitter that thwomps both Regex, because of the overhead, and VB.Split, because they have some weird heuristics.
-- Justin Rogers DigiTec Web Consultants, LLC. Blog: http://weblogs.asp.net/justin_rogers
"Itzik" <it****@pisgasys.co.il> wrote in message news:ej**************@tk2msftngp13.phx.gbl... 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"
i use this syntax : string str = "aa a - bb-b - ccc";
string del = " - ";
string[] temp = str.Split(del.ToCharArray());
now is return me 9 items : "aa" , "a" , "" , "" , "bb" , "b" , "" , "" , "ccc" Thanks
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Stu Cazzo |
last post by:
I have the following:
String myStringArray;
String myString = "98 99 100";
I want to split up myString and put it into myStringArray.
If I use this:
myStringArray = myString.split(" ");
it...
|
by: Carlos Ribeiro |
last post by:
Hi all,
While writing a small program to help other poster at c.l.py, I found
a small inconsistency between the handling of keyword parameters of
string.split() and the split() method of...
|
by: Will McGugan |
last post by:
Hi,
I'm curious about the behaviour of the str.split() when applied to empty
strings.
"".split() returns an empty list, however..
"".split("*") returns a list containing one empty string.
...
|
by: SL_McManus |
last post by:
Hi All;
I am fairly new to Perl. I have a file with close to 3000 lines
that I would like to split out in a certain way. I would like to put
the record type starting in column 1 for 2 spaces,...
|
by: Senthil |
last post by:
Code
----------------------
string Line = "\"A\",\"B\",\"C\",\"D\"";
string Line2 = Line.Replace("\",\"","\"\",\"\"");
string CSVColumns = Line2.Split("\",\"".ToCharArray());
|
by: David Logan |
last post by:
We need an additional function in the String class. We need the ability
to suppress empty fields, so that we can more effectively parse. Right
now, multiple whitespace characters create multiple...
|
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: Ben |
last post by:
Hi
I am creating a dynamic function to return a two dimensional array from a
delimeted string.
The delimited string is like:
field1...field2...field3...
field1...field2...field3......
|
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: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
| |