views:

76

answers:

4

How to get following result in ASP.NET:

INPUT string: "Duck, Donald"

Required String: "Donald Duck"

P.S: The input string is dynamic.

+2  A: 

Hey,

You have to write the code yourself to split by the , using the string's split method, and reverse them yourself.

Brian
+1  A: 

There's a million ways to skin a cat, but how about using Linq?

using System.Linq;

string input = "Duck, Donald";
string output = string.Concat(input.Split(',').Select(x => (" " + x)).Reverse()).Trim();

I'm sure someone will come up with an easier way!

nukefusion
@nukefusion - thanks, i appreciate your help. But is the code for C# ? I have asp.net with VB and framework 1.1
RMN
@RMN, are you using framework 1.1? Why oh why? Save yourself a lot of trouble and get a newer version
Esben Skov Pedersen
i would definately love to do that, if that was in my hands :( Unfortunately i have no options other than solving this in 1.1 :)
RMN
@RMN: Yeah, sorry, didn't see the VB tag.... doh!
nukefusion
+5  A: 
Dim name As String = "Duck, Donald"
        If name.Contains(",") Then
            Dim fullname As Array = Split(name.ToString, ",")
            Dim final As String = fullname(1).trim() + " " + fullname(0).trim()
        End If
Johnny DropTables
highlight your example in the editor and click the little 101010 button to format it as code. also you should probably call `.Trim()` on each array item.
lincolnk
Thanks. Im new to the site
Johnny DropTables
@Johnny, Thanks. The suggestion looks good. Can you suggest me an additional 'if' condition to identify if there is any Comma in string, then only to reverse/split these strings. Probably can be exceptional condition.
RMN
Just use, an "if name.contains(",") then" statement
Mike Cellini
Dim name As String = "Duck, Donald" If name.Contains(",") Then Dim fullname As Array = Split(name.ToString, ",") Dim final As String = fullname(1).trim() + " " + fullname(0).trim() End If
Johnny DropTables
@Johnny, Thanks a tonn for your help... :)...
RMN
+1  A: 

You could create a function that takes the comma separated name as input, splits it and returns the new, rearranged name as output.

Public Function CreateName(ByVal name as String) as String

  Dim values() as String = name.Split(",")
  Dim newName as String = String.Empty

  If values.length > 1 Then
    newName = values(1).Trim() & " " & values(0).Trim()
  Else
    newName = values(0).Trim()
  End If

  Return newName

End Sub


.
.
.

Dim rearranged as String = CreateName("Duck, Donald")
@riygbiv: This one is perfect :) ... Thnx!
RMN