【问题标题】:How to replace text in a string如何替换字符串中的文本
【发布时间】:2013-11-27 05:41:37
【问题描述】:

这是我的代码,我想用最简单的方式把“这里”换成“土豆”。

Private Sub btnreplace_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)      Handles btnreplace.Click
    txttyping.Text.Replace("here", "potato")
End Sub

【问题讨论】:

    标签: .net vb.net string


    【解决方案1】:

    字符串(文本)是不可变的。这意味着它们不能直接更改,要更改一个,创建/返回一个 新字符串

    txttyping.Text = txttyping.Text.Replace("here", "potato)
    

    Replace() 返回一个需要分配的新字符串。这适用于 所有 更改字符串的 String 方法:ToLower()ToUpper()Remove()、PadLeft()、Copy()Remove()TrimEnd()

    MSDN String Class:

    String 对象被称为不可变(只读),因为它的值在创建后无法修改。看似修改 String 对象的方法实际上会返回一个包含修改的新 String 对象。

    【讨论】:

      【解决方案2】:

      因为它是一个特定的词,你可以使用简单的文本替换而不是使用 regex.replace()

      试试这个

      txttyping.Text = Replace(txttyping.Text, "here", "potato")
      

      这里替换(sourcestring,findword,replaceword)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-03-24
        • 2014-01-26
        • 1970-01-01
        • 2010-12-27
        • 1970-01-01
        • 2021-09-11
        • 2018-02-05
        • 1970-01-01
        相关资源
        最近更新 更多