tags:

views:

577

answers:

3

How can I trim MyString to be MyStr?

Thanks, google failed again :(

+10  A: 
YourString.Left(YourString.Length-4)

or:

YourString.Substring(0,YourString.Length-4)
Rob Farley
I don't believe there is a left function in vb.net 2008? Correct me if I'm wrong.
Or: YourString.Substring(0,YourString.Length-4)
Rob Farley
@Jenna: I'ts in the Microsoft.VisualBasic namespace
Bill
A: 

c#

string s = "MyString";
Console.WriteLine(s.Substring(0, s.Length - 3));

vb.net

dim s as string
s = "MyString"
Console.WriteLine(s.Substring(0, s.Length - 3))

vb.net (with VB6 style functions)

dim s as string
s = "MyString"
Console.WriteLine(Mid(s, 1, len(s) - 3))
shahkalpesh
A: 

Rob's answer is mostly correct but the SubString solution will fail whenever the string has less than 4 characters in it. If the length goes past the end of the string an exception will be thrown. The following fixes that issue

Public Function TrimRight4Characters(ByVal str As String) As String
  If 4 > str.Length Then
    return str.SubString(4, str.Length-4)
  Else
    return str
  End if
End Function
JaredPar