J’ai une chaîne (qui est essentiellement un nom de fichier suivant une convention de nommage) abc.def.ghi
Je voudrais extraire la sous-chaîne avant la première .
(c.-à-d. un point)
Dans java doc api, je n’arrive pas à trouver une méthode dans Ssortingng qui fait ça.
Est-ce que je manque quelque chose? Comment faire?
regardez Ssortingng.indexOf
et Ssortingng.subssortingng
.
Assurez-vous de vérifier -1 pour indexOf
.
Vous pouvez simplement diviser la chaîne.
public Ssortingng[] split(Ssortingng regex)
Notez que java.lang.Ssortingng.split utilise la valeur d’expression régulière du délimiteur. En gros, comme ça …
Ssortingng filename = "abc.def.ghi"; // full file name Ssortingng[] parts = filename.split("\\."); // Ssortingng array, each element is text between dots Ssortingng beforeFirstDot = parts[0]; // Text before the first dot
Bien sûr, cela est divisé en plusieurs lignes pour la clarté. Il pourrait être écrit comme
Ssortingng beforeFirstDot = filename.split("\\.")[0];
La réponse acceptée est correcte mais elle ne vous dit pas comment l’utiliser. Voici comment vous utilisez les fonctions indexOf et sous-chaîne ensemble.
Ssortingng filename = "abc.def.ghi"; // full file name int iend = filename.indexOf("."); //this finds the first occurrence of "." //in ssortingng thus giving you the index of where it is in the ssortingng // Now iend can be -1, if lets say the ssortingng had no "." at all in it ie no "." is found. //So check and account for it. Ssortingng subSsortingng; if (iend != -1) { subSsortingng= filename.subssortingng(0 , iend); //this will give abc }
Si votre projet utilise déjà commons-lang , SsortingngUtils fournit une méthode intéressante à cet effet:
Ssortingng filename = "abc.def.ghi"; Ssortingng start = SsortingngUtils.subssortingngBefore(filename, "."); // returns "abc"
voir javadoc [2.6] [3.1]
ou vous pouvez essayer quelque chose comme
"abc.def.ghi".subssortingng(0,"abc.def.ghi".indexOf(c)-1);
Que diriez-vous d’utiliser regex?
Ssortingng firstWord = filename.replaceAll("\\..*","")
Cela remplace tout du premier point jusqu’à la fin par “” (c’est-à-dire qu’il disparaît, vous laissant avec ce que vous voulez)
Voici un test:
System.out.println("abc.def.hij".replaceAll("\\..*", "");
Sortie:
abc
Dans java.lang.Ssortingng, vous obtenez des méthodes comme indexOf (): qui vous renvoie le premier index d’un caractère / chaîne. et lstIndexOf: qui vous renvoie le dernier index de Ssortingng / char
De Java Doc:
public int indexOf(int ch) public int indexOf(Ssortingng str)
Retourne l’index dans cette chaîne de la première occurrence du caractère spécifié.
Voici le code qui renvoie une sous-chaîne à partir d’une Ssortingng
jusqu’à ce que l’une des listes de caractères données:
/** * Return a subssortingng of the given original ssortingng until the first appearance * of any of the given characters. * * eg Original "ab&cd-ef&gh" * 1. Separators {'&', '-'} * Result: "ab" * 2. Separators {'~', '-'} * Result: "ab&cd" * 3. Separators {'~', '='} * Result: "ab&cd-ef&gh" * * @param original the original ssortingng * @param characters the separators until the subssortingng to be considered * @return the subssortingng or the original ssortingng of no separator exists */ public static Ssortingng subssortingngFirstOf(Ssortingng original, List characters) { return characters.stream() .map(original::indexOf) .filter(min -> min > 0) .reduce(Integer::min) .map(position -> original.subssortingng(0, position)) .orElse(original); }