Click here to Skip to main content
15,848,724 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,
Please let me know, the case is

i have a text field which contains 20 characters, i have split it again as 14+6 by using substring method which is store in two different fields in database table.
Problem is now when i pass less that 14 or less than 6 characters in text fields it gives length exception, because in substring it demanded for 14 and 6 length respectively.

i need to know how can i pass less than 14 and less than 6 value in text field.

What I have tried:

string fullname = Name_txt.Text;
string val2 = fullname.Substring(0, 14); //validity
string val3 = fullname.Substring(14,6);

now for substring (val2) 1 14 is fixed if i go for entering 13 character it will throw exception and same if i go to enter less than6 charcacter then it will give exception of length count as well
Posted
Updated 18-Jan-18 21:15pm

I think it would be a lot easier if you used two textboxes, one for the first name and one for the last name.
Another idea is to use a separator between first and last name, e.g. a hyphen, and then use the Split() function.
 
Share this answer
 
Comments
ANKIT JOHRI 19-Jan-18 2:24am    
sir that is mandatory to take one and split and name total can be 20 characters long but in substring it should include in first string from 0 to 14 and then in second substring from 15 to 20.
Alex Schunk 19-Jan-18 9:51am    
Sounds fishy... Also a very bad design... John Stone for instance has to write his name this way? "John_________Stone"
But well... If you need it... Here:

var fullname = (Name_txt.Text ?? "").PadRight(20, ' ');
var val2 = fullname.Substring(0, 14); //validity
var val3 = fullname.Substring(14,6);
just validate the length of the text
string fullname = Name_txt.Text;
      string val2, val3;
      if (fullname.Length >= 20)
      {
          val2 = fullname.Substring(0, 14);
          val3 = fullname.Substring(14, 6);
      }
      else {
          // show validation message to the user to enter 20+ characters
      }
 
Share this answer
 
Comments
ANKIT JOHRI 19-Jan-18 2:26am    
No name can be from 2 to 20 characters long... so substring need to accept first substring shoudl take 2 to 14 characters ( 14 or less than 14) and then substring 2 should take 15th to 20th(5 or less than 5)
Karthik_Mahalingam 19-Jan-18 2:41am    
try
   string fullname =   "Name_txt.Text";
        string val2, val3;
         
        if (fullname.Length <= 14)
            val2 = fullname.Substring(0, fullname.Length); 
        else
        {
            val2 = fullname.Substring(0, 14);
            if (fullname.Length <= 20)
                val3 = fullname.Substring(14, fullname.Length - 14);
            else
                val3 = fullname.Substring(14, 6);
        }
ANKIT JOHRI 19-Jan-18 4:02am    
i did this way now string fullname = Name_txt.Text;
string val2 = "";
string val3 = "";
if (fullname.Length >= 14)
{
val2 = fullname.Substring(0, 14); //name
val3 = fullname.Substring(14);
}
else
{
val2 = fullname; //name
//val3 = fullname.Substring(14, 6);

}
Karthik_Mahalingam 19-Jan-18 4:43am    
it wont work if the text is more than 20, the second string will the remaining chars.

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