Supposons que j'ai une ficelle A
, par exemple:
string A = "Hello_World";
Je veux supprimer tous les caractères jusqu'au (et y compris) le _
. Le nombre exact de caractères avant la _
peut varier. Dans l'exemple ci-dessus, A == "World"
après le retrait.
Réponses:
11 pour la réponse № 1string A = "Hello_World";
string str = A.Substring(A.IndexOf("_") + 1);
1 pour la réponse № 2
string a = "Hello_World";
a = a.Substring(a.IndexOf("_")+1);
essaye ça? ou la partie A = de votre A = Hello_World est-elle incluse?
1 pour la réponse № 3
Avez-vous essayé ceci:
string input = "Hello_World"
string output = input.Substring(input.IndexOf("_") + 1);
output = World
Vous pouvez utiliser la méthode IndexOf et la méthode Substring.
Créez votre fonction comme ça
public string RemoveCharactersBeforeUnderscore(string s)
{
string splitted=s.Split("_");
return splitted[splitted.Length-1]
}
Utilisez cette fonction comme ceci
string output = RemoveCharactersBeforeUnderscore("Hello_World")
output = World
1 pour la réponse № 4
Vous avez déjà reçu une réponse parfaite. Si vous êtes prêt à aller un peu plus loin, vous pouvez conclure le a.SubString(a.IndexOf("_") + 1)
dans une méthode d'extension robuste et flexible:
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`
}
}
que vous utiliseriez comme ceci:
"Hello_World".TrimStartUpToAndIncluding("_") == "World"
0 pour la réponse № 5
var foo = str.Substring(str.IndexOf("_") + 1);
0 pour la réponse № 6
string orgStr = "Hello_World";
string newStr = orgStr.Substring(orgStr.IndexOf("_") + 1);