Manipulating strings using the split method

I have a problem using Split Method() .

I have a line like this:

 string diagnosis = "001.00 00 002.01 00 003.00 01"; 

And the conclusion should be:

 001.00 002.01 003.00 

I tried in two ways to remove two digits:

1

  string[] DiagnosisCodesParts = diagnosis.Split(); if (DiagnosisCodesParts[x].Length > 3) { //here } 

A..

2

 string DiagnosisCodestemp = diagnosis.Replace(" 00 ", " ").Replace(" 01 ", " ").Replace(" 02 ", " ") 

Is there any other way to delete two digits?

+4
source share
7 answers

It would be easier for me

 Regex.Matches(diagnosis, @"\d+\.\d+").Cast<Match>().Select(m => m.Value); 
+7
source

You can do this using regular expressions. Example:

 var data = "001.00 00 002.01 00 003.00 01"; var re = new Regex(@"(\d+\.\d+)\ \d+\ ?"); var matches = re.Matches(data); for (int i = 0; i < matches.Count; i++ ) { var m = matches[i]; Console.WriteLine(m.Groups[1]); } Console.ReadLine(); 

It is output:

 001.00 002.01 003.00 
+3
source

If we can assume that the actual length of the element is 6, then:

 var items = diagnosis.Split(' ') .Where(item => item.Length == 6) .ToList(); 

If the condition is the Length element MORE THAN 2:

 var items = diagnosis.Split(' ') .Where(item => item.Length > 2) .ToList(); 
+1
source

You can extract parts with more than three consecutive characters, such as:

 var resultSet = from sz in diagnosis.Split(new char[] {' '}) where sz.Length > 3 select sz; 

or get any string with a decimal point in it:

 var resultSet = from sz in diagnosis.Split(new char[] {' '}) where sz.Contains(".") select sz; 

then you can output it as follows:

 foreach(var entry in resultSet) Console.WriteLine(entry); 
+1
source

You can filter the result so that it contains only lines longer than two characters:

 string[] DiagnosisCodesParts = diagnosis.Split().Where(s => s.Length > 2).ToArray(); 
+1
source

It will work -

 var arr = System.Text.RegularExpressions.Regex.Split("001.00 00 002.01 00 003.00 01",@"\s\d{2}\s*").Take(3); 
+1
source

You can use this and exclude blank lines

 var DiagnosisCodesParts = Regex.Split(diagnosis, "\\s\\d\\d\\s?"); 

or use it and crop the results.

 var DiagnosisCodesParts = Regex.Split(diagnosis, "\\d\\d"); 
0
source

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


All Articles