2014-11-28 18 views

risposta

10
string A = "Hello_World"; 
string str = A.Substring(A.IndexOf('_') + 1); 
0
var foo = str.Substring(str.IndexOf('_') + 1); 
1
string a = "Hello_World"; 
a = a.Substring(a.IndexOf("_")+1); 

provare questo? o la parte A = nella tua A = Hello_World inclusa?

1

Avere provate questo:

string input = "Hello_World" 
    string output = input.Substring(input.IndexOf('_') + 1); 
    output = World 

È possibile utilizzare il metodo IndexOf e il metodo sottostringa.

Creare la funzione come questa

public string RemoveCharactersBeforeUnderscore(string s) 
{ 
string splitted=s.Split('_'); 
return splitted[splitted.Length-1] 
} 

Usare questa funzione come questa

string output = RemoveCharactersBeforeUnderscore("Hello_World") 
output = World 
0
string orgStr = "Hello_World"; 
string newStr = orgStr.Substring(orgStr.IndexOf('_') + 1); 
1

Hai già ricevuto a perfectly fine answer. Se siete disposti a fare un passo avanti, si potrebbe concludere la a.SubString(a.IndexOf('_') + 1) in un metodo di estensione robusto e flessibile:

public static string TrimStartUpToAndIncluding(this string str, char ch) 
{ 
    if (str == null) throw new ArgumentNullException("str"); 
    int pos = str.IndexOf(ch); 
    if (pos >= 0) 
    { 
     return str.Substring(pos + 1); 
    } 
    else // the given character does not occur in the string 
    { 
     return str; // there is nothing to trim; alternatively, return `string.Empty` 
    } 
} 

che si usa in questo modo:

"Hello_World".TrimStartUpToAndIncluding('_') == "World"