Combine adjacent characters of the special character "-"

I am developing an application using C # .net in which I need so that if the input entered by the user contains a "-" character (hyphen), then I want the immediate hyphen (-) neighbors to be combined, for example, if the user introduces

ABC then i want it to be replaced with ABC AB-CD then i want it to be replaced like BC ABC-DE then i want it to be replaced like CDE AB-CD-K then i want it to be replaced like BC and DK both separated by keyword and 

after receiving this I have to prepare my query for the database.

I hope I have clarified the problem, but if I need to clarify, let me know. Any help would be greatly appreciated.

Thanks Devjosh

+6
source share
4 answers

Unconfirmed, but this should do the trick, or at least lead you in the right direction.

 private string Prepare(string input) { StringBuilder output = new StringBuilder(); char[] chars = input.ToCharArray(); for (int i = 0; i < chars.Length; i++) { if (chars[i] == '-') { if (i > 0) { output.Append(chars[i - 1]); } if (++i < chars.Length) { output.Append(chars[i]) } else { break; } } } return output.ToString(); } 

If you want each pair to create a separate object in an array, try the following code:

 private string[] Prepare(string input) { List<string> output = new List<string>(); char[] chars = input.ToCharArray(); for (int i = 0; i < chars.Length; i++) { if (chars[i] == '-') { string o = string.Empty; if (i > 0) { o += chars[i - 1]; } if (++i < chars.Length) { o += chars[i] } output.Add(o); } } return output.ToArray(); } 
+3
source

Using:

 string[] input = { "ABC", "AB-CD", "ABC-DE", "AB-CD-K" }; var regex = new Regex(@"\w(?=-)|(?<=-)\w", RegexOptions.Compiled); var result = input.Select(s => string.Concat(regex.Matches(s) .Cast<Match>().Select(m => m.Value))); foreach (var s in result) { Console.WriteLine(s); } 

Conclusion:

 ABC BC CDE BCDK 
+4
source

Correct me if I am wrong, but, of course, all you have to do is delete the '-'?

like this:

 "ABC".Replace("-",""); 
+1
source

You can even solve this with a single line (albeit a bit ugly):

 String.Join(String.Empty, input.Split('-').Select(q => (q.Length == 0 ? String.Empty : (q.Length > 1 ? (q.First() + q.Last()).ToString() : q.First().ToString())))).Substring(((input[0] + input[1]).ToString().Contains('-') ? 0 : 1), input.Length - ((input[0] + input[1]).ToString().Contains('-') ? 0 : 1) - ((input[input.Length - 1] + input[input.Length - 2]).ToString().Contains('-') ? 0 : 1)); 

first it splits the string into an array on each '-' , then it combines only the first and last characters of each string (or only a single character, if there is only one, and leaves an empty string if there is nothing there), and then it combines the resulting enumerated string . Finally, we separate the first and last letters if they are not in the required range.

I know this is ugly, I just say that it is possible ..

It's probably best to just use simple

 new Regex(@"\w(?=-)|(?<=-)\w", RegexOptions.Compiled) 

and then work with it.

EDIT @ Cyril Polishchuk was faster .. his solution should work.

EDIT 2

After the question has been updated, here is a snippet that should do the trick:

  string input = "ABC"; string s2; string s3 = ""; string s4 = ""; var splitted = input.Split('-'); foreach(string s in splitted) { if (s.Length == 0) s2 = String.Empty; else if (s.Length > 1) s2 = (s.First() + s.Last()).ToString(); else s2 = s.First().ToString(); s3 += s4 + s2; s4 = " and "; } int beginning; int end; if (input.Length > 1) { if ((input[0] + input[1]).ToString().Contains('-')) beginning = 0; else beginning = 1; if ((input[input.Length - 1] + input[input.Length - 2]).ToString().Contains('-')) end = 0; else end = 1; } else { if ((input[0]).ToString().Contains('-')) beginning = 0; else beginning = 1; if ((input[input.Length - 1]).ToString().Contains('-')) end = 0; else end = 1; } string result = s3.Substring(beginning, s3.Length - beginning - end); 

It's not very elegant, but it should work (not tested, though ..). it works almost the same as the single line above ...

+1
source

Source: https://habr.com/ru/post/907917/


All Articles