VB.net 替换字符串中的特定单词

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18527844/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 14:52:11  来源:igfitidea点击:

VB.net replace a specific word in a string

vb.netreplace

提问by Marc Intes

I want to create a program that could replace a specific word which I can freely set.

我想创建一个可以替换我可以自由设置的特定单词的程序。

Sample word:

示例词:

Dim sample As String = "TWINKLE TWINKLE LITTLE STAR FISH"

i want to replace the word STAR into BAT so the new output would be:

我想将单词 STAR 替换为 BAT,因此新输出将是:

TWINKLE TWINKLE LITTLE BAT FISH

is this possible? thanks in advance.

这可能吗?提前致谢。

回答by Tim Schmelter

So you want to Replaceall occurences of one word with another?

所以你想把Replace一个词与另一个词的所有出现都放在一起?

sample = sample.Replace("STAR", "BAT")

If you want to ignore the case (.NET is case sensitive) you can use a regex:

如果您想忽略大小写(.NET 区分大小写),您可以使用正则表达式:

Dim regex = New Regex("STAR", RegexOptions.IgnoreCase)
sample = regex.Replace(sample, "BAT")

(remember to add Imports System.Text.RegularExpressions)

(记得加Imports System.Text.RegularExpressions

回答by Christian Sauer

result = sampe.Replace("STAR", "BAT") is your friend...

result = sampe.Replace("STAR", "BAT") 是你的朋友...

回答by Wujing Klaus

Dim sample as string = "TWINKLE TWINKLE LITTLE STAR FISH"
if sample.contains("STAR") then
    dim change_star as string
    change_star = sample.replace("STAR","BAT")
    messagebox.show("change_star")
    'NOTE: output change_star
Endif