开发者

Vbscript - move characters to the right from a comma character

This is probably a quick one. But I've got this string which looks like this:

Gabriel, Pet开发者_C百科er

And I want this string to look like this:

Peter Gabriel

I know about the replace and split functions, but I'm not sure how I can make this as simple as possible without messing around too much?

Edit: I actually fixed it by messing around a little. Here's the code

    Dim strDisplayName as string
    strDisplayName = "Gabriel, Peter"
    Dim strTemp() As String
    strTemp = Split(strDisplayName, ",")
    strTemp(1) = Replace(strTemp(1), " ", "")
    strDisplayName = strTemp(1) & " " & strTemp(0)

It might not be the most elegant solution, but atleast I can tell what's going on :p


Here is the solution:

Dim YourString As String = "Gabriel, Peter"

Dim Temp() As String = Split(YourString, ", ", -1, CompareMethod.Binary)

Dim Result As String = Temp(1) & " " & Temp(0)

'now Result = "Peter Gabriel"

Note: This solution has been tested OK in Visual Studio 2010.


I did not test this because I am on a mac, but here is how I guess it will work

dim fullname as string = "Gabriel, Peter"

dim order_name as string = fullname.substring(indexof(", ")+1) & " " & fullname.substring(1,indexof(","))

msgbox order_name


    Dim strDisplayName As String = "Gabriel, Peter"
    Dim strTemp() As String = Split(strDisplayName, ",")
    Array.Reverse(strTemp)
    Dim PersonName As String = Join(strTemp, " ")

This should work in VB.Net


If you know the comma is present:

aParts = split(oldvalue, ",")
newvalue = trim(aPart(1)) & " " & trim(aPart(0))

You could probably do it all on one line and not need the intermediate array (or maybe not, my vbscript is a little rusty) but that would mean calling split twice so would likely be less efficient anyway.

Or without split, just use basic string functions:

newvalue = trim(mid(oldvalue, instr(oldvalue,",")+1) & " " & trim(mid(oldvalue, instr(oldvalue,",")-1)

Edit: I've just noticed you've tagged the question vb.net as well as vbscript. The above is vbscript, I know little of vb.net...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜