Recognize strings in java

Recognize strings in java

Strings are handled as objects of type String . The String class is located in the java.lang package. By default, this package is automatically imported into all java programs

3 ways to create String objects :

String s1="imooc"; // Create a string object imooc named s1 
        String s2= new String(); // Create an empty string object named s2 
        String s3= new String("imooc"); // Create a string object imooc named s3

String immutability

After the String object is created, it cannot be modified and is immutable. The so-called modification is actually the creation of a new object, which points to a different memory space.

Once a string is created in memory, the string is immutable. If we need a string that can be changed, we can use StringBuffer or StringBuilder .

Each time a string is new , a new object is generated. Even if the content of the two strings is the same, it will be " false " when using " == " to compare . If you only need to compare whether the content is the same, you should use " equals() " method.

 

 

Common methods of String class

 

1. The index of the characters in the string str starts from 0 and ranges from 0 to str.length()-1

 

2. When using indexOf to search for characters or strings, if there is a match, return the position index; if there is no match, return -1

 

3. When using substring(beginIndex, endIndex) for string interception, the characters at the beginIndex position are included, but the characters at the endIndex position are not included .

public  static  void main(String[] args){
         // java file name 
        String fileName="HelloWorld.java" ;
         // mailbox 
        String email="[email protected]" ;
         // get the last occurrence of ". "Number position 
        int index=fileName.lastIndexOf('.' );
        String prefix = fileName.substring(index, fileName.length());
         // The judgment must contain "." and cannot appear in the first place. At the same time the suffix is ​​"java" 
        if (fileName.indexOf('.')>0&&prefix.equals(".java" )){
            System.out.println( "The java file name is correct"+index+ prefix);
        }else{
            System.out.println( "The java file name is invalid" );
        }
        int index2=email.indexOf('@' );
         int index3=email.indexOf('.' );
         // The judgment must contain "@", and "@" must be before "." 
        if (index2!=- 1&&index3> index2){
            System.out.println( "The message format is correct" );
        }else{
            System.out.println( "Invalid email format" );
        }
    }

 

Common methods of String class

==: Determine whether the first address of two strings in memory is the same, that is, determine whether they are the same string object

equals(): Compare whether the contents stored in two string objects are consistent

String s="kjawlkankalaalwka";
        int count=0;
        for(int i=0;i<s.length();i++){
            if(s.charAt(i)=='a'){
                count++;
            }
        }
        System.out.println( "Number of occurrences of character a: "+count);

StringBuilder StringBuffer

With String , an additional object is created when the program runs, holding "helloworld" . When strings are frequently manipulated, many additional temporary variables are generated. Use StringBuilder or StringBuffer to avoid this problem. As for StringBuilder and StringBuffer , they are basically similar, the difference is that StringBuffer is thread-safe, while StringBuilder does not implement thread-safety functions, so the performance is slightly higher. Therefore, in general, if you need to create a string object with variable content, you should give priority to using the StringBuilder class.

public static void main(String[] args){
    StringBuilder str1 = new StringBuilder(); // Create an empty StringBuilder object 
    StringBuilder str2= new StringBuilder("imooc"); // Create a string imooc 
    System.out.println(str2);
}

 

Common methods of StringBuilder class

public  static  void main(String[] args){
         // Create a StringBuilder object and store the string 
        StringBuilder str= new StringBuilder("hello" );
        str.append( "imooc"); // Append the string after the string 
        str.append(520); // Append the integer after the string 
        System.out.println("String length: "+ str.length( ));
        System.out.println( "Before insert: "+ str);
        str.insert( 11, "?!"); // Insert content at the specified position 
        String str2=str.toString(); // Convert to String object 
        System.out.println("After insertion: "+ str2);
    }
public static void main(String[] args){
        StringBuilder str=new StringBuilder();
        str.append( "lkasjgpelkdl" );
         // Insert commas every 3 digits from back to front 
        for ( int i=str.length()-3;i>0;i-=3 ){
            str.insert(i, ",");
        }
        System.out.println(str.toString());
    }

 

*** END

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324816751&siteId=291194637