拆分字符和数字

时间:2012-12-27 02:40:54

标签: vb.net split format currency

我在分裂

中遇到了这个问题

我想将数字转换为formatcurrency,但它包含一个Character

例如:

POC 1900
output :
POC $1,900.00

或者

    1900 POC 
    output :
    $1,900.00 POC

谢谢

2 个答案:

答案 0 :(得分:1)

使用正则表达式匹配和替换值。

Dim str = "1900 POC" //Or "POC 1900"
Dim reg = New Regex("\d+")

Dim match = reg.Match(str)
If match.Success Then
  str = reg.Replace(str, Decimal.Parse(match.Value).ToString("C"))
  Console.WriteLine(str)
End If

编辑:

如果字符串在数字之间包含逗号(,),则将其替换为:

Dim str = "1,900 POC".Replace(",", "")

答案 1 :(得分:0)

Dim strValue As String = "POC 1900"
Dim strOutput As String = String.Empty
Dim strNum As String() = strValue .Split(" ")
Dim strDisplay As String = String.Empty
For x As Integer = 0 To strNum.Length - 1
   strDisplay = String.Concat(strDisplay, " ", IIf(IsNumeric(strNum(x)), FormatCurrency(Val(strNum(x)), 2), strNum(x)))
Next

strOutput = strDisplay