将纯文本转换为文本框格式

时间:2015-07-01 14:55:03

标签: vb.net

我正在搜索将纯文本转换为此格式的代码

我的意思是

例如,如果有纯文本= v6.23b 12完整

所以我希望它将此文本转换为此格式

格式

版本6.23 Build 12

就是这样。不应以格式

显示“完整”一词

2 个答案:

答案 0 :(得分:0)

怎么样

Dim oldText As String = "v6.23b 12 Full"
Dim newText As String = oldText.Replace("v", "version ").Replace("b", " Build").Replace("Full", String.Empty)

请注意,如果字符串中还有其他“v”或“b”,则会出现问题。

答案 1 :(得分:0)

强迫自己学习正则表达式,所以这似乎是一个很好的练习......

我使用这两个网站来解决这些问题:

http://www.regular-expressions.info/tutorial.html

https://msdn.microsoft.com/en-us/library/az24scfc(v=vs.110).aspx

这是我使用正则表达式的版本:

Imports System.Text.RegularExpressions
Public Class Form1

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim version As String = "v6.23b 12 Full"
        Debug.Print(version)

        ' Find a "v" at the beginning of a word boundary, with an optional space afterwards,
        ' that is followed by one or more digits with an optional dot after them.  Those digits with an optional dot
        ' after them can repeat one or more times.  
        ' Change that "v" to "version" with a space afterwards:
        version = Regex.Replace(version, "\bv ?(?=[\d+\.?]+)", "version ")
        Debug.Print(version)

        ' Find one or more or digits followed by an optional dot, that type of sequence can repeat.
        ' Find that type of sequence followed by an optional space and a "b" or "B" on a word boundary.
        ' Change the "b" to "Build" preceded by a space:
        version = Regex.Replace(version, "(?<=[\d+\.?]+) ?b|B\b", " Build") ' Change "b" to "Build"
        Debug.Print(version)

        ' Using a case-insensitive search, replace a whole word of "FULL" or "TRIAL" with a blank string:
        version = Regex.Replace(version, "(?i)\b ?full|trial\b", "")
        Debug.Print(version)
    End Sub

End Class