【问题标题】:converting plain text into textbox format将纯文本转换为文本框格式
【发布时间】:2015-07-02 01:13:16
【问题描述】:

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

我是说

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

所以我希望它将这个文本转换成这种格式

格式

版本 6.23 Build 12

就是这样。格式中不应出现“full”字样

【问题讨论】:

    标签: vb.net


    【解决方案1】:

    怎么样

    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”,则会出现问题。

    【讨论】:

    • 它有效,但可能还有其他情况..如果有相同的文本,它会但我希望它不应该在“12”之后显示任何单词,因为可能有一个单词“full”或“踪迹” 。所以请帮助我
    • 那么你需要运用你的编程技能。如果您总是只想要前 10 个字符(或其他字符),请先编码 SubString。如果您总是想删除“full”,请编码,如果您总是想删除“trial”,请编码。
    • 好吧,我知道我应该做什么了..感谢指导:)
    【解决方案2】:

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

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

    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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-01-17
      • 2012-09-05
      • 1970-01-01
      • 1970-01-01
      • 2012-04-12
      • 2014-09-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多