string delimiter

  • Thread starter Thread starter Hetal Shah
  • Start date Start date
H

Hetal Shah

It is a C# code.

I have a string like, one||two||three

I want to split it into one, two and three.


string str = "one||two||three";
string[] myStrs = str.Split("||");

This code does not work.

How can I do it?

Yhanks
Hetal
 
Hetal Shah said:
It is a C# code.

I have a string like, one||two||three

I want to split it into one, two and three.


string str = "one||two||three";
string[] myStrs = str.Split("||");

This code does not work.

How can I do it?

Well, if you use a single character delimiter, eg | instead of ||,
it'll work.

Otherwise, you could use Regex.Split or write your own splitting
routine using String.IndexOf and String.Substring.
 
Hetal,
In addition to the other comments.

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 = "one||two||three";
string [] fields = Strings.Split(str, "||", -1,
CompareMethod.Binary);


Hope this helps
Jay


Hetal Shah said:
It is a C# code.

I have a string like, one||two||three

I want to split it into one, two and three.


string str = "one||two||three";
string[] myStrs = str.Split("||");

This code does not work.

How can I do it?

Yhanks
Hetal
 
Hetal said:
It is a C# code.

I have a string like, one||two||three

I want to split it into one, two and three.

string str = "one||two||three";
string[] myStrs = str.Split("||");

This code does not work.

How can I do it?

Yhanks
Hetal

Could you do something like this:

string [] myStrs = str.Replace("||", "|").Split(new char[]{'|');
 
Back
Top