Marc Intes
Marc Intes

Reputation: 737

VB.net replace a specific word in a string

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:

TWINKLE TWINKLE LITTLE BAT FISH

is this possible? thanks in advance.

Upvotes: 1

Views: 8691

Answers (4)

supwat
supwat

Reputation: 11

As Tim Schmelter posted. I followed, but it not working on ignorance case. If somebody face the same problem, could follow these.

Change:

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

To:

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

No need Dim regex = New Regex("STAR", RegexOptions.IgnoreCase)

Upvotes: 0

Wujing Klaus
Wujing Klaus

Reputation: 1

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

Upvotes: 0

Christian Sauer
Christian Sauer

Reputation: 10909

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

Upvotes: 0

Tim Schmelter
Tim Schmelter

Reputation: 460268

So you want to Replace all occurences of one word with another?

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

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

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

(remember to add Imports System.Text.RegularExpressions)

Upvotes: 4

Related Questions