473,657 Members | 2,421 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Split

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.T oCharArray());

now is return me 9 items : "aa" , "a" , "" , "" , "bb" , "b" , "" , "" ,
"ccc"

Thanks


Nov 16 '05 #1
4 5774
"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.T oCharArray());


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.Reg ularExpressions ;

...

string str = "aa a - bb-b - ccc";

string del = " - ";

string[] temp = Regex.Split(str , del);
// Bjorn A
Nov 16 '05 #2
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.Visua lBasic.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.S plit if you need to split a string based on a collection
of specific characters. Each individual character is its own delimiter.

Use System.Text.Reg ularExpressions .RegEx.Split to split based
on matching patterns.
By referencing the Microsoft.Visua lBasic 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(s tr, del, -1, CompareMethod.B inary);

Hope this helps
Jay
"Itzik" <it****@pisgasy s.co.il> wrote in message
news:ej******** ******@tk2msftn gp13.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.T oCharArray());

now is return me 9 items : "aa" , "a" , "" , "" , "bb" , "b" , "" , "" ,
"ccc"

Thanks

Nov 16 '05 #3
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****@pisgasy s.co.il> wrote in message
news:ej******** ******@tk2msftn gp13.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.T oCharArray());

now is return me 9 items : "aa" , "a" , "" , "" , "bb" , "b" , "" , "" ,
"ccc"

Thanks

Nov 16 '05 #4
Justin,
COOL! I'll have to add that one to my little FAQ.

Thanks
Jay
"Justin Rogers" <Ju****@games4d otnet.com> wrote in message
news:OC******** ******@TK2MSFTN GP09.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****@pisgasy s.co.il> wrote in message
news:ej******** ******@tk2msftn gp13.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.T oCharArray());

now is return me 9 items : "aa" , "a" , "" , "" , "bb" , "b" , "" , "" ,
"ccc"

Thanks


Nov 16 '05 #5

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

Similar topics

5
31172
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 will split myString up using the delimiter of 1 space so that
11
2494
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 strings. I wonder if someone else had ever stumbled on it before, and if it has a good reason to work like it is. Both implementations take two parameters: the separator character and the max number of splits (maxsplit). However, string.split() accept
9
4342
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. I would have expected the second example to have also returned an empty
2
12822
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, the employer code in column 23 for 29 spaces and employer description in column 53 for 30 spaces. I have tried modifying an existing file with no real success. I haven't found anything that specifically answers my question. Any guidance would be...
6
6373
by: Senthil | last post by:
Code ---------------------- string Line = "\"A\",\"B\",\"C\",\"D\""; string Line2 = Line.Replace("\",\"","\"\",\"\""); string CSVColumns = Line2.Split("\",\"".ToCharArray());
19
10910
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 empty strings in the resulting string array.
14
34942
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, bytesRead) .... Dim parts() As String = data.Split(vbCrLf)
3
9656
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... field1...field2...field3...
5
5869
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() = Microsoft.VisualBasic.Strings.Split(aLine, vbNewLine, -1, CompareMethod.Binary)
0
8312
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8732
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8504
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6169
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5632
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4318
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2732
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1959
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1622
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.