Click here to Skip to main content
15,892,697 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All
I have string like below
string STR="0004168858,AB0688120881,15,PLATE CLAMPING CO,KTL 16,CEQP,GI31,1111,DV1111,22222222"

Now I want to validate the input string with STR that this much field is there with comma separator

In this STR 10 fields are there so I want to check in my new string is there 10 fields
or not

So how should I write code for that
Please help

Thanks & Regards
Indrajit Dasgupta
Posted
Updated 25-Oct-12 23:47pm
v2
Comments
MT_ 26-Oct-12 5:27am    
Not very clear question. Do you want to validate pattern of comma separated sub-string? Or pattern of the STR itself ? What is the pattern?
IndrajitDasgupat 26-Oct-12 5:36am    
In this STR I have 10 field so what ever new string I am going to input it should check is there 10 fields or not
MT_ 26-Oct-12 6:13am    
Okay, Added solution below. See if it helps. Let us know if you it solves the problem or you are still looking for some other issue.

Try:
C#
string STR="0004168858,AB0688120881,15,PLATE CLAMPING CO,KTL 16,CEQP,GI31,1111,DV1111,22222222";
string[] parts = STR.Split(',');
This will break each element into a separate string in the array
 
Share this answer
 
Comments
ridoy 26-Oct-12 7:24am    
+5
Based on your latest comment, you need to validate the count of substring separated by comma in the main string STR.
C#
STR.Split(',').Count() 
Above code will give you the count of the substrings in the main string.
Now if you want to validate through some kind of validation control, then you can use ErrorProvider control for windows application. Look for more details of error provider at http://www.c-sharpcorner.com/UploadFile/scottlysle/UseErrorProvider03082007210138PM/UseErrorProvider.aspx[^]

Hope that helps.

Milind
 
Share this answer
 
To check that your new string have 10 feild or not just split the string and get count of array by its arr.lenght proprety.

Thanks,
Ambesha
 
Share this answer
 
First you need to split the string to arrays for the validation.

C#
string STR="0004168858,AB0688120881,15,PLATE CLAMPING CO,KTL 16,CEQP,GI31,1111,DV1111,22222222";

string[] strArrays = STR.Split(',');

Now you have list of values in the 'strArrays', and you can validate the fields using foreach (or) for loop iterations.

C#
foreach(string st in strArrays)
{
  st //your validation goes here;
}

OR
C#
for(int i=0;i<strarrays.count;i++)>
{
 strArrays[i] //your validation goes here;
}
 
Share this answer
 
v2

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