Important Uses and Examples of java String

  • 2021-07-13 05:19:32
  • OfStack

1. Returns the String "length" method

How do you determine the length of a given String? java provides a method called "length ()". Use it for the length of String you need to find.


public class Str_Sample {

  public static void main(String[] args){

    // Test String Length method 

    String name="hello work";

    //length Method returns an integer 

    int num=name.length();

    System.out.println(" Length of string: "+num);

  }

}

Output:

String length: 10

2. String "indexOf ()" method

How can I find which character is in which position?

indexOf "can help you specify the position of the first occurrence of a specific character. If you can't find it, return-1


public class Str_Sample{

  public static void main(String[] args){

    String name="I like java. How do you like java?";

     int num=name.indexOf("java");

     System.out.println("java No. 1 1 The location of the second occurrence: "+num);

  }

}

Output:

The first occurrence of java: 7

3. String "lastindexOf ()" method

If I know the length, I want to find where the role is from the back of the string.

"lastindexOf" can reverse retrieval from the specified position, return the last place where you specified a specific character appears, and return-1 if you can't find it


public class Str_Sample{

  public static void main(String[] args){

    String name="I like java. How do you like java?";

    //name The length of 

    int num=name.length();

      //lastindexOf( Specify special characters, specify positions )

       int index=name.lastindexOf("java",num);

        System.out.println("java Finally 1 The location of the second occurrence: "+index);

     

  }

]

Output:

Last occurrence of java: 29

4. String "substring ()" method

What should I do if I only want one paragraph in the string?

"subString" can return the truncated string from the specified header and the truncated string. Note: The range indicated in java contains head but not tail.


public class Str_Sample{

  public static void main(String[] args){

    String name="I like java. How do you like java?";

    // Interception How This letter, first of all, you should know H Subscript of , You can use the previous indexOf Method 

    int num=name.indexOf("H");

    // Due to java The range shown in the middle includes the head but not the tail, so it is necessary to add more 1 Bit 

    String str=name.substring(num,num+3);

    System.out.println(str);

    // It can also be truncated directly from the specified position to the end of the string 

    String str2=name.substring(num);

    System.out.println(str2);

  }

]

Output:

How

How do you like java?

5. String "charAt ()" method

How can I get characters by position?

"chatAt" can help you to return the specified subscript characters


public class Str_Sample{

  public static void main(String[] args){

    String name="I like java. How do you like java?";

    // Create a loop traversal name Subscript of 

    for(int i=0;i<name.length();i++){

      // Put the subscript into the charAt Method 

      char ch=name.charAt(i);

      System.out.print(ch);

    }

  }

]

Output:

I like java. How do you like java?

6. String "startsWith (), endsWith ()" method

How do you tell what a string begins or ends with?

"startsWith ()" to check whether the string begins with the specified string. 'endsWith ()' checks whether the string ends with the specified string


public class Str_Sample{

  public static void main(String[] args){

    String name="I like java. How do you like java?";

    // Whether to use " I "Begin, return correctly true Otherwise, return false

    boolean s1=name.startsWith("I");

    //startsWith() No. 1 2 To determine whether the specified position is a specified string 

    boolean s2=name.startsWith("java",7);

    // Determines whether the string is marked with " ? "End 

    boolean e1=name.endsWith("?");

    System.out.println(" Whether to use " I "At the beginning: "+s1);  

    System.out.println(" Position 7 Whether it is " java "At the beginning: "+s2);

    System.out.println(" Whether to use " ? "At the end: "+e1);

   }

}

Output:

No starting with "I": true

Is Position 7 beginning with "java": true

Is it "?" End: true

7. String "compareTo ()" method

"compareTO" It compares from bit 1 and returns the difference between the ascii values of the two characters if it encounters a different character. The return value is of type int.


public class Str_Sample{

  public static void main(String[] args){

    //A Adj. ascli Value is 65 , a Adj. ascli Value is 97

    String a="A";

    String b="a";

    String c="aa";

    String d="abc";

    String e="ad";

    int num=a.compareTo(b);

    // And 1 This method ignores case for comparison 

    int num2=a.compareToIgnoreCase(b);

    // No length 1 Sample and the first few characters are not 1 Sample , From the first 1 Bit starts to look, and when you find it, you don't 1 Sample character, the value returned is the value compared between the two characters 

    int num3=c.compareTo(d);

    // Such as multiple characters , No. 1 1 If the characters are the same, compare them directly 2 Characters , And so on 

    int num4=e.compareTo(c);

    System.out.println("a And b Compare: "+num);

    System.out.println("a And b Compare (ignoring case): "+num2);

    System.out.println("c And d Compare: "+num3);

    System.out.println("e And d Compare: "+num4);

   }

}

Output:

a vs b:-32

a vs. b (case ignored): 0

c versus d:-1

Comparison of e and d: 3

8. String "contains ()" method

If you want to know if the string contains the string you want?

Then "contanins" can meet your needs and determine whether it contains the specified string


public class Str_Sample{

  public static void main(String[] args){

    String name="I like java. How do you like java?";

    // Determine whether to include " you "This string 

    boolean bl=name.contains("you");

    System.out.println("name Whether the string contains " you " :"+bl);

  }

]

Output:

Does the name string contain "you": true

9. String "replace ()" method

You can specify the part of the string to replace and the replacement string in the parameter.


public class Str_Sample{

  public static void main(String[] args){

    String name="I like java. How do you like java?";

    String str=name.replace("java", "php");

    System.out.println(" Before replacement: "+name);

    System.out.println(" After replacement: "+str);

  }

]

Output:

Before replacement: I like java. How do you like java?

After replacement: I like php. How do you like php?

10. String "toLowerCase ()" and "toUpperCase ()" methods

"toLowerCase ()" displays strings in lowercase and toUpperCase () "displays strings in uppercase.


public class Str_Sample{

  public static void main(String[] args){

    String name="I like java. How do you like Java?";

    String low=name.toLowerCase();

    String upp=name.toUpperCase();

    System.out.println(" Lowercase display: "+low);

    System.out.println(" Capitalized display: "+upp);

  }

]

Output:

Lowercase: i like java. how do you like java?

Capitalized: I LIKE JAVA. HOW DO YOU LIKE JAVA?


Related articles: