First of all:
wrong approach! Why?
You have to work on data, not on string representation of data!
Assuming that each part of string (between [
-
]) represents some portion of data, you have to define class which can store that piece of data. For training purposes only i decided to write simple
HelperClass
with 2 properties:
Header
and
Number
.
Header
property is used to store string till the last [
-
] and
Number
property to store number.
public class HelperClass
{
private string header = string.Empty;
private int nmb = 0;
public string Header
{
get {return header;}
set {header = value;}
}
public int Number
{
get {return nmb;}
set {nmb = value;}
}
public override string ToString()
{
return header;
}
}
How to use it?
void Main()
{
List<string> numbers = new List<string>()
{
"S00118-JR10-B-NO-R-JS-26",
"S00118-JR10-B-NO-R-JS-22",
"S00118-JR10-B-NO-R-JS-24",
"S00201-0110-B-NO-R-ST-16",
"S00201-0110-B-NO-R-ST-15",
"S00201-JRSR-B-RE-R-TS-20",
"S00118-0110-B-NO-R-JK-46",
"S00201-0104-M-NO-R-SH-11",
"S00118-0110-B-NO-R-JK-34"
};
var highestNumbers = numbers.Select(x=>new HelperClass
{
Header = x.Substring(0,22),
Number = Convert.ToInt32(x.Substring(22,2))
})
.GroupBy(x=>x.Header)
.Select(grp=> grp.Max(y=>y.Header) + grp.Max(y=>y.Number));
foreach(var hn in highestNumbers)
{
Console.WriteLine("{0}", hn);
}
}
</string></string>
Result:
S00118-JR10-B-NO-R-JS-26
S00201-0110-B-NO-R-ST-16
S00201-JRSR-B-RE-R-TS-20
S00118-0110-B-NO-R-JK-46
S00201-0104-M-NO-R-SH-11