2014年7月13日 星期日

[RESOLVED] Removing comma and some text from a string


txtbUSERNAME.Text = txtbFIRST.Text.ToLower().Substring(0, 1) + txtbLAST.Text.ToLower().ToString();



This code takes the first letter of the first and the rest of the last name and puts it in a textbox.


Example: John Smith = jsmith.



But sometimes, I might have this example: John Smith, III.

and I dont want it to be jsmith, iii.

I still want it jsmith


How do I remove the comma  and whatever comes after it?



You can try with the below code


//Find the index of , in sname
int index = txtbLAST.Text.IndexOf(",");
//Remove all characters after comma
txtbUSERNAME.Text = txtbFIRST.Text.ToLower().Substring(0, 1) + txtbLAST.Text.Substring(0, index).ToLower().ToString();



If you want to grab whatever comes after a comma, you can simply use the String.Split() method to grab everything appears prior to the first comma :


// This grabs everything prior to the comma
var sectionBeforeComma = yourString.Split(',')[0];

So in your example, "John Smith, III" would yield "John Smith".



thank you!


沒有留言:

張貼留言