[Hinweis] Java: Zeichenfolgensuche

Einführung

Ich denke, Sie verwenden oft "Trimmen" und "enthält".

String-Suche

1, contains Suchen Sie, ob eine bestimmte Zeichenfolge in der Zeichenfolge enthalten ist. Klein- und Großbuchstaben </ font>. True falls enthalten. False wenn nicht enthalten.

StringSearch.java


public class StringSearch {
    public static void main(String[] args) {
        String JAVA = "Java";
        String RUBY = "Ruby";
        String SOFTWARE = "Software";

        String word1 = "I study Java";
        String word2 = "I made app made by Ruby on rails";
        String word3 = "I want to be Software developer";

        boolean javaValid = word1.contains(JAVA);
        boolean rubyValid = word2.contains(RUBY);
        boolean softwareValid = word3.contains(SOFTWARE);

        System.out.println("\"I study Java\".contains(\"Java\") : "+javaValid);
        System.out.println("\"I made app made by Ruby on rails\".contains(\"Ruby\") : "+rubyValid);
        System.out.println("\"I want to be Software developer\".contains(\"Software\") : "+softwareValid);

        boolean result = word1.contains("java");
        System.out.println("\"Java\".contains(\"java\") : "+result);
    }
}

Ergebnis.java



        "I study Java".contains("Java") : true
        "I made app made by Ruby on rails".contains("Ruby") : true
        "I want to be Software developer".contains("Software") : true
        "Java".contains("java") : false

2, startsWith Bestimmen Sie, ob eine Zeichenfolge mit einer bestimmten Zeichenfolge beginnt. Klein- und Großbuchstaben </ font>. Leer wird auch als erstes Zeichen </ font> bewertet. True wenn es losgeht. False wenn es nicht startet.

sample.java



        String word4 = "JavaScript";
        String word5 = " JavaScript";
        boolean starts1 = word4.startsWith("J");
        boolean starts2 = word4.startsWith("j");
        boolean startsWithSpace1 = word5.startsWith("J");
        boolean startsWithSpace2 = word5.startsWith(" ");

        System.out.println("\"JavaScript\".startsWith(\"J\") : " + starts1);
        System.out.println("\"JavaScript\".startsWith(\"j\") : " + starts2);
        System.out.println("\" JavaScript\".startsWith(\"J\") : " + startsWithSpace1);
        System.out.println("\" JavaScript\".startsWith(\" \") : " + startsWithSpace2);

Ergebnis.java


        "JavaScript".startsWith("J") : true
        "JavaScript".startsWith("j") : false
        " JavaScript".startsWith("J") : false
        " JavaScript".startsWith(" ") : true

3, endsWith Bestimmen Sie, ob eine Zeichenfolge mit einer bestimmten Zeichenfolge endet. Klein- und Großbuchstaben </ font>. Beurteilen Sie, dass das Leerzeichen auch das letzte Zeichen ist </ font>. True wenn fertig. Wenn es nicht endet, false.

sample.java



        String word4 = "JavaScript";
        String word5 = " JavaScript ";

        boolean end1 = word4.endsWith("t");
        boolean end2 = word4.endsWith("T");
        boolean endWithSpace1 = word5.endsWith("t");
        boolean endWithSpace2 = word5.endsWith(" ");
        System.out.println("\"JavaScript\".endsWith(\"t\") : " + end1);
        System.out.println("\"JavaScript\".endsWith(\"T\") : " + end2);
        System.out.println("\" JavaScript \".endsWith(\"t\") : " + endWithSpace1);
        System.out.println("\" JavaScript \".endsWith(\" \") : " + endWithSpace2);

Ergebnis.java



        "JavaScript".endsWith("t") : true
        "JavaScript".endsWith("T") : false
        " JavaScript ".endsWith("t") : false
        " JavaScript ".endsWith(" ") : true

String-Konvertierung

1, toLowerCase Wenn Sie diese Methode für eine bestimmte Zeichenfolge verwenden, wird sie in Kleinbuchstaben konvertiert. Das Übergeben von Japanisch verursacht keinen Fehler.

sample.java



        String onlyUpper = "JAVASCRIPT";
        String onlyLower = "javascript";
        String mixed = "JaVaScRiPt";
        String onlyLowerWithSpace = "i love java";
        String onlyUpperWithSpace = "I LOVE JAVA"; 
        String mixedWithSpace = "I lOvE jAvA";
        String japanese = "Hallo. Heute ist es sonnig.";

        String expectLower1 = onlyUpper.toLowerCase();
        String expectLower2 = onlyLower.toLowerCase();
        String expectLower3 = mixed.toLowerCase();
        String expectLower4 = onlyLowerWithSpace.toLowerCase();
        String expectLower5 = onlyUpperWithSpace.toLowerCase();
        String expectLower6 = mixedWithSpace.toLowerCase();
        String expectLower7 = japanese.toLowerCase();

        System.out.println("\"JAVASCRIPT\".toLowerCase() => " + expectLower1);
        System.out.println("\"javascript\".toLowerCase() => " + expectLower2);
        System.out.println("\"JaVaScRiPt\".toLowerCase() => " + expectLower3);
        System.out.println("\"i love java\".toLowerCase() => " + expectLower4);
        System.out.println("\"I LOVE JAVA\".toLowerCase() => " + expectLower5);
        System.out.println("\"I lOvE jAvA\".toLowerCase() => " + expectLower6);
        System.out.println("\"Hallo. Heute ist es sonnig.\".toLowerCase() => " + expectLower7);

Ergebnis.java


        "JAVASCRIPT".toLowerCase() => javascript
        "javascript".toLowerCase() => javascript
        "JaVaScRiPt".toLowerCase() => javascript
        "i love java".toLowerCase() => i love java
        "I LOVE JAVA".toLowerCase() => i love java
        "I lOvE jAvA".toLowerCase() => i love java
        "Hallo. Heute ist es sonnig.".toLowerCase() => Hallo. Heute ist es sonnig.

2, toUpperCase Wenn Sie diese Methode für eine bestimmte Zeichenfolge verwenden, wird sie in Großbuchstaben konvertiert.

sample.java


        String onlyUpper = "JAVASCRIPT";
        String onlyLower = "javascript";
        String mixed = "JaVaScRiPt";
        String onlyLowerWithSpace = "i love java";
        String onlyUpperWithSpace = "I LOVE JAVA"; 
        String mixedWithSpace = "I lOvE jAvA";
        String japanese = "Hallo. Heute ist es sonnig.";
        
        String expectUpper1 = onlyUpper.toUpperCase();
        String expectUpper2 = onlyLower.toUpperCase();
        String expectUpper3 = mixed.toUpperCase();
        String expectUpper4 = onlyLowerWithSpace.toUpperCase();
        String expectUpper5 = onlyUpperWithSpace.toUpperCase();
        String expectUpper6 = mixedWithSpace.toUpperCase();
        String expectUpper7 = japanese.toUpperCase();

        System.out.println("\"JAVASCRIPT\".toUpperCase() => " + expectUpper1);
        System.out.println("\"javascript\".toUpperCase() => " + expectUpper2);
        System.out.println("\"JaVaScRiPt\".toUpperCase() => " + expectUpper3);
        System.out.println("\"i love java\".toUpperCase() => " + expectUpper4);
        System.out.println("\"I LOVE JAVA\".toUpperCase() => " + expectUpper5);
        System.out.println("\"I lOvE jAvA\".toUpperCase() => " + expectUpper6);
        System.out.println("\"Hallo. Heute ist es sonnig.\".toUpperCase() => " + expectUpper7);

Ergebnis.java


        "JAVASCRIPT".toUpperCase() => JAVASCRIPT
        "javascript".toUpperCase() => JAVASCRIPT
        "JaVaScRiPt".toUpperCase() => JAVASCRIPT
        "i love java".toUpperCase() => I LOVE JAVA
        "I LOVE JAVA".toUpperCase() => I LOVE JAVA
        "I lOvE jAvA".toUpperCase() => I LOVE JAVA
        "Hallo. Heute ist es sonnig.".toUpperCase() => Hallo. Heute ist es sonnig.

3, trim Entfernen Sie die führenden und nachfolgenden Leerzeichen in einer Zeichenfolge. Hinweis: Weiß wird nur vorher und nachher entfernt, nicht Weiß in der Zeichenfolge. </ font>

sample.java


        String trim1 = "  Java and JavaScript  ";
        String trim2 = "Java and JavaScript";

        String afterTrim1 = trim1.trim();
        String afterTrim2 = trim2.trim();

        System.out.println("\"  Java and JavaScript  \".length() => " + trim1.length());
        System.out.println("\"  Java and JavaScript  \".trim().length() => " + afterTrim1.length());
        System.out.println("\"  Java and JavaScript  \".trim() => " + afterTrim1);

        System.out.println("\"Java and JavaScript\".length() => " + trim2.length());
        System.out.println("\"Java and JavaScript\".trim().length() => " + afterTrim2.length());
        System.out.println("\"Java and JavaScript\".trim() => " + afterTrim2);

Ergebnis.java


        "  Java and JavaScript  ".length() => 23
        "  Java and JavaScript  ".trim().length() => 19
        "  Java and JavaScript  ".trim() => Java and JavaScript
        "Java and JavaScript".length() => 19
        "Java and JavaScript".trim().length() => 19
        "Java and JavaScript".trim() => Java and JavaScript

4, replace Ersetzen Sie eine bestimmte Zeichenfolge, die in einer bestimmten Zeichenfolge enthalten ist, durch eine beliebige Zeichenfolge. Klein- und Großbuchstaben </ font>.

sample.java


        String beforeRep = "I like Java and Ruby, Python";
        String afterRep1 = beforeRep.replace("Java", "Golang");
        String afterRep2 = beforeRep.replace("java", "Golang");

        System.out.println("\"I like Java and Ruby, Python\".replace(\"Java\", \"Golang\") => " + afterRep1);
        System.out.println("\"I like Java and Ruby, Python\".replace(\"java\", \"Golang\") => " + afterRep2);

Ergebnis.java


"I like Java and Ruby, Python".replace("Java", "Golang") => I like Golang and Ruby, Python
"I like Java and Ruby, Python".replace("java", "Golang") => I like Java and Ruby, Python

Recommended Posts