Splitting a string into an array using a STRING value

  • Thread starter Thread starter Jason
  • Start date Start date
J

Jason

Hi all

basically its what i said in the subject. Would just like to know how to
split a string using a STRING value as the delimeter and not a CHAR as the
delimeter. Example: "I like Cheese", how would i get the following array:
strArray[0] = "I"; strArray[1] = "Cheese", using "like" as the delimeter

Thanks!
Jason
 
You can use substring with a string arg, as follows:

Console.WriteLine("Enter a string to split:");
string inS = Console.ReadLine();
int inSL = inS.Length;

Console.WriteLine("Enter the string to split the first string:");
string sString = Console.ReadLine();
int sSL = sString.Length;

int pos = inS.IndexOf(sString);

if(pos >= 0)
{
Console.WriteLine("The strings are:");
string pre = inS.Substring(0, pos);
int preL = pre.Length;
Console.WriteLine("\"" + pre + "\"");

int postStart = sSL + preL;
int postEnd = inSL - postStart -1;
string post = inS.Substring(postStart, postEnd);

Console.WriteLine("\"" + post + "\"");
}
else
{
Console.WriteLine("Cannot find " + sString + " in " + inS);
}

Miha Markic said:
Hi Jason,

Try Regex.Split method.

--
Miha Markic - RightHand .NET consulting & development
miha at rthand com

Jason said:
Hi all

basically its what i said in the subject. Would just like to know how to
split a string using a STRING value as the delimeter and not a CHAR as the
delimeter. Example: "I like Cheese", how would i get the following array:
strArray[0] = "I"; strArray[1] = "Cheese", using "like" as the delimeter

Thanks!
Jason
 
hmm yeh thanks
Have done this before. roundabout way, thanks for the reply! was wondering
if there was an easier way.

will try regex

Thanks
Jason

Doug said:
You can use substring with a string arg, as follows:

Console.WriteLine("Enter a string to split:");
string inS = Console.ReadLine();
int inSL = inS.Length;

Console.WriteLine("Enter the string to split the first string:");
string sString = Console.ReadLine();
int sSL = sString.Length;

int pos = inS.IndexOf(sString);

if(pos >= 0)
{
Console.WriteLine("The strings are:");
string pre = inS.Substring(0, pos);
int preL = pre.Length;
Console.WriteLine("\"" + pre + "\"");

int postStart = sSL + preL;
int postEnd = inSL - postStart -1;
string post = inS.Substring(postStart, postEnd);

Console.WriteLine("\"" + post + "\"");
}
else
{
Console.WriteLine("Cannot find " + sString + " in " + inS);
}

Miha Markic said:
Hi Jason,

Try Regex.Split method.

--
Miha Markic - RightHand .NET consulting & development
miha at rthand com

Jason said:
Hi all

basically its what i said in the subject. Would just like to know how to
split a string using a STRING value as the delimeter and not a CHAR as the
delimeter. Example: "I like Cheese", how would i get the following array:
strArray[0] = "I"; strArray[1] = "Cheese", using "like" as the delimeter

Thanks!
Jason
 
hmm yeh thanks
Have done this before. roundabout way, thanks for the reply! was wondering
if there was an easier way.

will try regex

What's wrong with String.Split()?

String sSep = "SEPARATOR";
String asTokens[] = String.Split(myMasterString, CType(sSep, char[]);

--Jekke
Brain for hire. E-mail at jekke-at-insidejoke-dot-tv
 
Hi,

Did you actually try it - it should split text on multiple (each char) chars
and not on all of them togethe.
Example:
Dim sep As String = ",;"

Dim s As String() = "ab,cd;ef".Split(CType(sep, Char()))

Will produce an array with ab, cd, ef.


--
Miha Markic - RightHand .NET consulting & development
miha at rthand com



Jekke said:
hmm yeh thanks
Have done this before. roundabout way, thanks for the reply! was wondering
if there was an easier way.

will try regex

What's wrong with String.Split()?

String sSep = "SEPARATOR";
String asTokens[] = String.Split(myMasterString, CType(sSep, char[]);

--Jekke
Brain for hire. E-mail at jekke-at-insidejoke-dot-tv
 
Back
Top