Hello, I have a problem with split line. According to the task, I have to take a string and determine if there are certain separators in it, telling us that the string is an array. The line gets from the file, but it doesn’t matter.
Example:
The condition is that a string is an array only if it contains ',' or ':' , not both (the first condition that appears is a separator, and therefore, if it hits another separator, it is considered as a simple part of the string, not a separator)
At the same time, "\," and "\:" are not determinants of the array.
Therefore at:
string s="a,b,f:g"; create an array:
array[0]="a"; array[1]="b"; array[2]="f:g"; And at:
string s="a,b\,f\"; is created:
array[0]="a"; array[1]="b\,f\"; If possible, tell me how to achieve this? I tried to make a split, but then I do not know which separator was used. (And I need to know for the subsequent restoration of the line to its original form)
UPD
I made a method that does not look very elegant, but partially copes with the task (gives information on which separator can separate the string)
static char IsArray(string line) { string s; s=line; s=s.Replace("\\,", ""); s=s.Replace("\\:", ""); int firstSeparator = s.IndexOf(','); int secondSeparator = s.IndexOf(':'); if (firstSeparator > secondSeparator && secondSeparator != -1) firstSeparator = secondSeparator; if (firstSeparator != -1) return s[firstSeparator]; if (secondSeparator != -1) return s[secondSeparator]; return '\0'; } The question remains how to divide the string into the given separator ignoring "\," and "\:"