I am trying to separate an entered name into 2 strings.
Each name is entered in the convention of lastName, firstName or Ex: Smith, John
I would like to separate the name into a lastName and firstName variable by trimming the string before and after the comma and space.
I have tried
Dim nameSeparator() As Char = {",", " "} Dim lastName = txtEditName.Text.TrimEnd(nameSeparator) Dim firstName = txtEditName.Text.TrimStart(nameSeparator) But after running this, lastName and firstName both equal the full string from txtEditName.Text
1 Answer
If you want to "split" a string in two substrings using a certain separator then you should use the proper method: string.Split
Only after the splitting you can remove unneeded characters at begin or end of a string using the string.Trim method
Dim input() as String = txtEditName.Text.Split(",") Dim lastName = input(0).Trim() Dim firstName = input(1).Trim() Of course this example assumes that you have exactly the input described in your question. If you want to use this approach in a real application then you should check if the result from the splitting produces exactly two substring before trying to access the substrings