Voici une autre ancienne fonction je de mon C# 1 jours, ce serait une façon plus élégante de l'écrire:Quelle est la manière la plus élégante de mettre en œuvre GetTextAfterMarker()
//method: gets the text in a string in front of a marker, if marker is not there, then return empty string
//example: GetTextAfterMarker("documents/jan/letter043.doc","/") returns "documents"
//example: GetTextAfterMarker("letter043.doc","/") returns ""
//rank:8
public static string GetTextAfterMarker(string line, string marker) {
string r = "";
int pos = line.IndexOf(marker);
if(pos != -1) {
r = line.Substring(pos+(marker.Length),line.Length-pos-(marker.Length));
} else {
r = "";
}
return r;
}
ce qui est une question de savoir comment? –
Je pense qu'il y a un bug dans votre code. L'utilisation de pos + (marker.Length) comme position de départ pour Substring va commencer après le marqueur, et retournera, dans votre premier exemple, "jan/letter043.doc". Donc soit votre code a un bug ou vous avez mal nommé la fonction. –
désolé, le nom de la fonction est faux, mais Fredrik Mörk m'a aidé à écrire de cette façon: http://stackoverflow.com/questions/1172216/how-can-i-refactor-this-c-code-using-split –