2

I'm trying to convert a string that contains someones name as "Last, First" to "First Last".

This is how I am doing it now:

name = name.Trim
name = name.Substring(name.IndexOf(",") + 1, name.Length) & " " & name.Substring(0, name.IndexOf(",") - 1)

When I do this I get the following error:

ArgumentOutOfRangeException was unhandled

Index and length must refer to a location within the string

Parameter name: length

Can someone explain why I am getting this error and how I should be doing this?

Community
  • 1
  • 1
Petefic
  • 657
  • 6
  • 14
  • 31

5 Answers5

6

You are getting error on this:

name.Substring(name.IndexOf(",") + 1, name.Length)

name.Length should have subtracted with the length of the string before the comma.

The best way for that is to split the string.

Dim oFullname as string = "Last, First"
Dim oStr() as string = oFullname.split(","c)
oFullname = oStr(1).trim & " " & oStr(0).trim
MsgBox (oFullname)
John Woo
  • 258,903
  • 69
  • 498
  • 492
  • Slightly related - what is the 'c' for as part of your split call? I haven't seen that before (until now, in all of the answers to this question.) – Calvin Allen May 24 '12 at 00:39
  • 1
    It converts string to character. – John Woo May 24 '12 at 00:42
  • @HillBilly.Developer The separator in this case is an instance of the `Char` data type; thus it is appended with the literal type character `c`. – John Woo May 24 '12 at 00:47
1

simply ,you only need to split the string

Dim originalName As String = "Last,First"
Dim parts = name.Split(","C)
Dim name As String = parts(1) & "  " & parts(0)
Sleiman Jneidi
  • 22,907
  • 14
  • 56
  • 77
  • 1
    I won't downvote you because your answer is wrong. IN Vb.net, concactenation of the string don't use `+` but instead `&` – John Woo May 24 '12 at 00:25
  • @johntotetwoo His answer is in fact correct you can use both + and & to concactenate strings in [Vb.net](http://msdn.microsoft.com/en-us/library/te2585xw(v=VS.71).aspx). &, is the recommended one to use for strings, but both work – Mark Hall May 24 '12 at 00:28
  • @MarkHall yes , you can use both, but & is more recommended ,check this http://stackoverflow.com/questions/1088053/vb-net-string-manipulation-or – Sleiman Jneidi May 24 '12 at 00:31
  • @sleimanjneidi Won't this put it back in FirstName LastName order? OP wanted it the other way around! – Bridge May 24 '12 at 00:31
  • oops sorry, didn't have a compiler here. you are both correct. :( – John Woo May 24 '12 at 00:32
  • @sleimanjneidi I stated so in my Comment, all I was saying is that it is not **wrong** to use + – Mark Hall May 24 '12 at 00:34
1

The second parameter for String.Substring is the length of the substring, not the end position. For this reason, you're always going to go out of bounds if you do str.Substring(n, str.Length) with n > 0 (which would be the whole point of a substring).

You need to subtract name.IndexOf(",") + 1 from name.Length in your first substring. Or just split the string, as the others have suggested.

Jonathan S.
  • 2,238
  • 16
  • 16
0

If you're using the Unix command line--like the terminal on a Mac--you can do it like this:

Let's say that you have a file containing your last-comma-space-first type names like this:

Last1, First1
Last2, First2
Last3, First3

OK, now let's save it as last_comma_space_first.txt. At this point you can use this command I came up with for your particular problem:

sed -E 's/([A-Za-z0-9]+), ([A-Za-z0-9]+)/\2 \1/g' last_comma_space_first.txt > first_space_last.txt

--->>> Scroll --->>>

You're done! Now, go check that first_space_last.txt file! ^_^ You should get the following:

First1 Last1
First2 Last2
First3 Last3

Tell your friends... Or don't...

0

This would work keeping to the posters format.

Name = "Doe,John"
Name = Replace(Name.Substring(Name.IndexOf(","), Name.Length - Name.IndexOf(",")) & " " & Name.Substring(0, Name.IndexOf(",")), ",", "")

Result Name = "John Doe"

HaveNoDisplayName
  • 8,291
  • 106
  • 37
  • 47