2009-08-27 22 views

risposta

27
YourString.Left(YourString.Length-4) 

o:

YourString.Substring(0,YourString.Length-4) 
+1

Non credo ci sia una funzione di sinistra in vb.net 2008? Correggimi se sbaglio. –

+3

Oppure: YourString.Substring (0, YourString.Length-4) –

+1

@Jenna: I't nello spazio dei nomi Microsoft.VisualBasic – Bill

6

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 (con funzioni di stile VB6)

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

risposta di Rob è in gran parte corretto, ma la soluzione SubString fallirà ogni volta che la stringa ha meno di 4 caratteri in esso. Se la lunghezza supera la fine della stringa, verrà generata un'eccezione. Le seguenti correzioni che emettono

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 
1

Questo è quello che ho usato nel mio programma (VB.NET):

Public Function TrimStr(str As String, charsToRemove As String) 
     If str.EndsWith(charsToRemove) Then 
      Return str.Substring(0, str.Length - charsToRemove.Length) 
     Else 
      Return str 
     End If 
    End Function 

Usage:

Dim myStr As String = "hello world" 
myStr = TrimStr(myStr, " world") 

Questa è la mia prima risposta. Spero che aiuti qualcuno. Sentiti libero di votare a meno se non ti piace questa risposta.

Problemi correlati