-1

I am trying to parse arguments from text from a speech recogniser and I want to be able to convert this: "int my variable string some other variable" into an array containing two strings like this: "int my variable" and "string some other variable" As you can see, I need to split it using a string array and then keep the delimiters I split using. Anyone got any ideas of how to do this?

SPV2008
  • 1
  • 1
  • Why did you choose to split it this way (3 words + 4 words) and not some other way (e.g. 4 words + 3 words)? – Yacoub Massad Apr 15 '16 at 19:52
  • 1
    There are many white spaces in your string and you chose one specific white space to split it. How do you decide which white-space to split at? – Vikhram Apr 15 '16 at 19:56
  • I am not splitting with whitespace. I'm splitting using an array of strings. I did say this. – SPV2008 Apr 15 '16 at 19:59
  • If you're not splitting on white space, then white space are not delimiters anymore. You're question is a bit confusing. It seems like you're really asking "How can a split a string on words from a list?" – hatchet - done with SOverflow Apr 15 '16 at 20:26
  • No. What I want to do, is split a string, with an array of strings as the delimiters, but keep the delimiters in. The delimiters in my example are int and string, but obviously if I split it using usual methods, it will remove int and string, which isn't what I want. – SPV2008 Apr 15 '16 at 20:27
  • I'm going to mark this as a duplicate, because the essential answer lies in a similar question. Applying that to your specific need gives something like `var r = Regex.Split(s,"(?=int|string)");` It's a java question, but C# has Regex and it's similar. – hatchet - done with SOverflow Apr 15 '16 at 20:54
  • See also http://stackoverflow.com/questions/521146/c-sharp-split-string-but-keep-split-chars-separators – hatchet - done with SOverflow Apr 15 '16 at 20:57

1 Answers1

0

you can do this by using this code but this code is only as per current question that you ask

string s = "int my variable string some other variable";

string[] sarr1 = new string[] { "string" };
string[] sarr=s.Split(sarr1, StringSplitOptions.None);
for (int i = 0; i < sarr.Length; i++)
{
    if (i == sarr.Length - 1)
        Console.WriteLine(sarr1[0] + sarr[i]);
    else
        Console.WriteLine(sarr[i]);
}

and if you are using the list of array you can use this approach

string s = "int my variable string some other variable float is another variable float is another variable";

 //you can create any string for replacement over here that
 //suit or fits your requirement
s = s.Replace("int", "<@@> <##>");
s = s.Replace("float", "<$$> <%%>");
s = s.Replace("string", "<^^> <&&>");

Dictionary<string, string> dict = new Dictionary<string, string>();
string[] sarr3 = new string[] { "<@@>","<$$>","<^^>" };
string[] sarr = s.Split(sarr3, StringSplitOptions.None);

for (int i = 0; i < sarr.Length; i++)
{
    string temp = sarr[i].Replace("<##>", "int").Replace("<%%>", "float").Replace("<&&>", "string");
    Console.WriteLine(temp);
}
rashfmnb
  • 9,959
  • 4
  • 33
  • 44
  • Yeah, this would work if it was the only time I'd need to use it, but the string I mentioned was an example. I could have ones that are very different, like "char input letter int a number" or something like that. – SPV2008 Apr 15 '16 at 20:25
  • check the update code – rashfmnb Apr 15 '16 at 21:03