Trim word from a string ?

vbdotnetnoobie

New member
Joined
Jan 7, 2010
Messages
1
Programming Experience
Beginner
What code can I use to trim a word from a string in Visual Basic.NET?

for example, If I had the following string:

Dim str As String = "Hello World"

And I want to trim this string so it only displays "Hello"

I have tried a few trim functions but can;t work it out.

Thanks very much.
 
str = str.Replace(" World", "")
str = str.Remove(5)
str = str.Remove(str.IndexOf(" World")) 'note, wil raise an exception if text not found
str = str.TrimEnd("d"c).TrimEnd("l"c).TrimEnd("r"c).TrimEnd("o"c).TrimEnd("W"c).TrimEnd(" "c) 'well, you did ask about the trim functions! :)
 
Back
Top