在某个字符后面的字符串中添加空格(VB代码)

时间:2016-12-08 05:48:31

标签: string vb.net textbox space

大家好。

    Dim txt1 As Double = Convert.ToDouble(TextBox1.Text) / 100
    Dim txt2 As Double = Convert.ToDouble(TextBox2.Text)
    Dim txt3 As Double = Convert.ToDouble(TextBox3.Text)
    Dim txtResult As Double = Convert.ToDouble(TextBox4.Text)

    Dim result As Double = txt1 * txt2 * txt3
    TextBox4.Text = result

正如您所看到的,我根据用户输入的内容得到了我的结果。所以我必须在某个字符之后添加一个空格。 Textbox14.text(0)< ---之后我想要我的空间。这样,在价值高于999后,它应该输出1 000而不是1000.非常感谢您提供任何有用的帮助,我真的到处寻找,我无法找到任何东西。

2 个答案:

答案 0 :(得分:0)

你在谈论组分隔符。 Custom Numeric Format Strings

您可以使用.ToString()方法并以格式定义组分隔符。

TextBox4.Text = result.ToString("0,0.000")

将根据本地系统的语言/区域设置使用不同的分隔符。

您可以手动定义您的客户分隔符

var cultureInfo = new System.Globalization.CultureInfo("en-US");
var numberInfo = cultureInfo.NumberFormat;
numberInfo.NumberGroupSeparator = " ";

TextBox4.Text = result.ToString("0,0.000", numberInfo)   

答案 1 :(得分:0)

如果我做对了,你想要每3个字符一个空格,对吧? 像1 000 000? 试试这个:

Dim result As String, str As String, ret As String
Dim i As Integer
Dim arr As Char()
'your text to space 
result = "10000000"
'reverte so we start with the end
result = StrReverse(result)
i = 0
ret = ""
' make a char array which each char is an own array element
arr = result.Take(result.Length).ToArray
'iterate through all elements
For Each str In arr
    ' skip the first element . 
    ' only add a space every 3 elements 
    If (i <> 0) And (i Mod 3 = 0) Then
        ret = ret + " "
    End If
    ret = ret + str
    i = i + 1
Next
' revers again the output
ret = StrReverse(ret)
MsgBox(ret)