Click here to Skip to main content
15,891,423 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello, I want to split a value taken from a textbox and save it in two different variables. I have a phone number of 10 digit eneterd in a textbox and I want to save first 3 digit into a variable called "code" and next 7 digit into a variable called number. Please specify a concept to do this. By using which methodology can I achieve this??
Posted

If it's always 10 characters then just use string.Substring:
C#
string code = tbNumber.Text.Substring(0, 3);
string number = tbNumber.Text.SubString(3);
 
Share this answer
 
Comments
Member 11949886 30-Sep-15 9:36am    
yeah its always ten character.. but does it works??
Member 11949886 30-Sep-15 9:37am    
string number = tbNumber.text.substring(3) is it correct or tbNumber.text.substring(4,9) is it correct.
phil.o 30-Sep-15 9:40am    
First one. You have to assign the result of Substring method to a variable, because the original string will not be modified.
OriginalGriff 30-Sep-15 9:48am    
Try it - or look at the MSDN documentation.
Substring has two overloads:
https://msdn.microsoft.com/en-us/library/system.string.substring%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396
Hello

Please use below code

C#
string sPhoneNumber = "1234567890";
           string sCode = "";
           string sNumber = "";
           if (sPhoneNumber.Length==10)
           {
               sCode = sPhoneNumber.Substring(0, 3);
               sNumber = sPhoneNumber.Substring(3);
           }



Code have the first three digit and number have the last 7 digit.
 
Share this answer
 
Comments
Member 11949886 30-Sep-15 9:39am    
are u sure?? sNumber = sPhoneNumber.Substring(3) is correct??
CB Sharma 30-Sep-15 9:41am    
Yes, Implement my code and debug.

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