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 "\:"

  • you can split it with the help of the regulars - Grundy
  • I am ashamed .. but it says nothing to me - Demolver
  • Regex.Split - Grundy

1 answer 1

I would do this using a split on a regular expression with a negative look back:

 using System.IO; using System; using System.Text.RegularExpressions; class Program { static void Main() { string s="a,b,f:g"; char separator = IsArray(s); string[] ss = Regex.Split(s, "(?<!\\\\)" + separator); foreach(string a in ss){ System.Console.WriteLine(a); } s = "a,b\\,f\\"; separator = IsArray(s); ss = Regex.Split(s, "(?<!\\\\)" + separator); foreach(string a in ss){ System.Console.WriteLine(a); } } 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'; } } 

a
b
f: g
a
b \, f \

The essence of the construction of the regular season with a negative view back (?<!\\\\), That is, the regular routine finds the desired character, then returns back ( ?<! ) And if it precedes the backslash character, then it does not consider this place a coincidence with the regular schedule.

running the tutorialpoint code

  • I'm sorry, it turned out that I can't use regular expressions, is there another way? - Demolver
  • @Demolver, why can't you? - Grundy
  • The clarification was that you need to use the standard c # libraries, I just did not pay attention to it - Demolver
  • @Demolver, uh, Regex is the standard class c # :) - Grundy
  • I did not know, then thank you very much - Demolver