how to create a string based on Textbox value with special char

Posted by Hurera1111 under C# on 4/4/2013 | Points: 10 | Views : 1061 | Status : [Member] | Replies : 1
I have a string like XXXXNNNN-AN in one textbox and on button click I want to create 2 string one as 8(4X~4N),2(1A~1N) and other one is for separator 9(-), if I will get the string as XXAANN-XX-NN/AA then I want to create one string as 6(2X~2A~2N),2(2X),2(2N),2(2A), and other string will be as 7(-),10(-),13(/)




Responses

Posted by: Niladri.Biswas on: 5/3/2013 [Member] Platinum | Points: 25

Up
0
Down
Here is a way

string input = "XXXXNNNN-AN";

char[] seperators = new[] { '/', '-' };
var splittedValues = input.Split(seperators, StringSplitOptions.RemoveEmptyEntries); ;
string intermediate = string.Empty;
List<string> lstString = new List<string>();
List<string> lstSeperators = new List<string>();
int sum = 0;


for (int i = 0; i < splittedValues.Count(); i++)
{
var res = splittedValues[i]
.ToCharArray()
.GroupBy(g => g)
.Select(
j =>
new
{
Key = j.Key,
KeyCount = j.Count()
}
);

foreach (var r in res)
{
sum += r.KeyCount;
intermediate += "~" + r.KeyCount + r.Key.ToString();
}
lstString.Add(sum + "(" + intermediate.Substring(1) + ")");
intermediate = string.Empty;
sum = 0;
}

//for finding the seperator positions
for (int i = 0; i < input.Length; i++)
if (seperators.Contains(input[i]))
lstSeperators.Add((i+1)+"(" + input[i] + ")");


var string1 = string.Join(",", lstString);
var string2 = string.Join(",", lstSeperators);


Best Regards,
Niladri Biswas

Hurera1111, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response