Click here to Skip to main content
15,881,803 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
hi,
i have string like
"Employee Name:john,age:45,salary:$1,000.45"
.When i split the string with "," the result is showing like this.
C#
sAry[0]="Employee Name:john";
sAry[1]="age:45";
sAry[2]="salary:$1";
sAry[3]="000.45";


But the array result should be like this
C#
sAry[0]="Employee Name:john";
sAry[1]="age:45";
sAry[2]="salary:$1,000.45";


Please help me to resolve this .
thanks and Best Regards
Posted
Updated 30-Dec-15 2:25am
v2
Comments
Ralf Meier 30-Dec-15 7:02am    
Is the string in the sample the complete string or are there following several items ?
mohan jana 4-Jan-16 2:19am    
The actual string is not simple as in the Question,it will much longer like

"Subscriber: David Benedict 406001229459 | National Rural Electric Co-Op Assoc, 76411406 \r\nYou are viewing: In network Medical Benefits as of 01/01/2015 for David Benedict [sep]Copays\r\n\r\n$0.00,$0.00,$0.00,$0.00\r\nOffice Visit ,Urgent Care ,Emergency Room ,Hospital Admission \r\n\r\nMajor Medical\r\n\r\nBenefit Percentage\r\n90%,10%\r\nPlan pays,You pay\r\n[sep]Individual Annual Maximum, , ,Individual Lifetime Maximum, \r\nNot Applicable, , ,Not Applicable, \r\n, , ,\r\n, , ,\r\n, , ,\r\n \r\nIndividual Integrated Deductible, , ,Family Integrated Deductible, \r\nNot Applicable, , ,\"\r\n100%\", \r\n, , ,Applied*,Remaining,Max\r\n, , ,\"$4,000.00\",$0.00,\"$4,000.00\"\r\n, , ,*Applied amount includes $706.92 for prescriptions\r\n \r\nIndividual Integrated Out-of-Pocket, , ,Family Integrated Out-of-Pocket, \r\n\"\r\n100%\", , ,\"\r\n52%\", \r\nApplied*,Remaining,Max, , ,Applied*,Remaining,Max\r\n\"$2,000.00\",$0.00,\"$2,000.00\", , ,\"$2,093.40\",\"$1,906.60\",\"$4,000.00\"\r\n*Applied amount includes $126.52 for prescriptions, , ,*Applied amount includes $189.75 for prescriptions\r\n \r\n"
Raje_ 30-Dec-15 8:24am    
One more thing you can do is you can build your string separated by | instead of comma.

That's always going to be a problem, because your data is using the same symbol for two different purposes: as a data separator, and as an optional thousands indicator.
What I would probably do is set up a regular expression to do the break down instead:
Employee Name:(?<EmpName>.+?)\,age:(?<EmpAge>\d+)\,salary:(?<EmpSalary>.*)
The three groups then give you the info you need.
 
Share this answer
 
Comments
Maciej Los 30-Dec-15 7:47am    
Great!
An implementation of what OriginalGriff said, but with bit changed pattern:

C#
string s = @"Employee Name:john,age:45,salary:$1,000.45,Employee Name:joshua,age:38,salary:$1,200.88,Employee Name:johnattan,age:29,salary:$1,050.23";
 
string pattern = @"(Employee Name:\w*|age:\d+|salary:\$\b\d+\,\d{3}\.\d{0,2}\b)+";

System.Text.RegularExpressions.Regex searchTerm =
    new System.Text.RegularExpressions.Regex(pattern);
 
var data = Regex.Matches(s,pattern, RegexOptions.ExplicitCapture | RegexOptions.CultureInvariant)
		.Cast<System.Text.RegularExpressions.Match>()
		.Select(m => new
		{
			Property = m.Value.Split(':')[0],
			Value = m.Value.Split(':')[1]
		});

Console.WriteLine("{0}\t\t{1}", "Property", "Value");
foreach(var d in data)
{
	Console.WriteLine("{0}\t\t{1}", d.Property, d.Value);
}


Result:

Property Value
Employee Name john 
age 45 
salary $1,000.45 
Employee Name joshua 
age 38 
salary $1,200.88 
Employee Name johnattan 
age 29 
salary $1,050.23 
 
Share this answer
 
v2
Comments
mohan jana 4-Jan-16 2:13am    
Hello Maciej Los many thanks for you solution.But the problem is the string is not static and it may be a much longer also.so i can't generate pattern for that.will you please help me to find and replace a string which having a pattern like "$[0-9]," to "$[0-9]|".If this is possible then splitting the string is much easier for me.
Maciej Los 4-Jan-16 2:43am    
The length of string is not important! The most important is to find proper pattern. See updated answer.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900