开发者

Trimming text in ASP.NET

How to get foll开发者_开发技巧owing result in ASP.NET:

INPUT string: "Duck, Donald"

Required String: "Donald Duck"

P.S: The input string is dynamic.


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


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


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")


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!

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜