Reputation: 7
The string I have is in this format: "String1" String2
I need to separate out the string1 and string2 but can't seem to get this working using substring.
Has anyone got any advice please?
Thanks
Edit: Here is the code I am working with
int startIndex = testString.IndexOf("\"") + "\"".Length;
int endIndex = testString.IndexOf("\"");
string string1 = "";
string string2 = "";
if (endIndex > 0)
{
string1 = testString.Substring(startIndex, endIndex - startIndex);
string2 = testString.Substring(endIndex, testString.Length);
}
Upvotes: 0
Views: 106
Reputation: 1856
Following code will be helpful for you,
// Split('"') function split the string on the " character. So here we get an
// array of: "String1" " String2"
var strs= @"""String1"" String2".Split('"');
// foreach iterate over the resulted array to get each string
foreach(var str in strs){
// Trim() remove the leading and trailing space from the string
Console.WriteLine(str.Trim());
}
Upvotes: 2
Reputation: 1416
Perhaps a vague question but if I understand correctly, string one is in double inverted commas and string two is beside it?
If this indeed the case:
string combined = "\"string1\" string2";
string[] split = combined.Split(new[] { '"' }, StringSplitOptions.RemoveEmptyEntries);
string str1 = split[0];
string str2 = split[1];
Upvotes: 1