Must practice questions for String, StringBuilder and StringBuffer

Table of contents

1. The difference between String, StringBuffer and StringBuilder

2. The first unique character in the string

 Method: auxiliary array method

 3. The length of the last word of the string

Method 1: use split

Method 2: use lastIndexOf, substring

4. Verify the palindrome


1. The difference between String , StringBuffer and StringBuilder

(1) The content of String cannot be modified, but the content of StringBuffer and StringBuilder can be modified .

(2) Most of the methods of StringBuffer and StringBuilder are the same, but there are differences with String.

(3) StringBuffer uses synchronous processing, which is a thread-safe operation; while StringBuilder does not use synchronous processing, which is a thread-unsafe operation.

Supplement 1:

StringBuilder and StringBuffer classes, most of the functions of these two classes are the same, here are some commonly used methods
method
illustrate
StringBuff append(String str)
Append at the end, equivalent to String 's += , you can append: variables of boolean , char , char[] , double, flfloat , int , long , Object , String , StringBuffff
char charAt(int index)
Get the character at the index position
int length()
Get the length of the string
int capacity()
Get the total size of the underlying storage string space
void ensureCapacity(int
mininmumCapacity)
expansion
void setCharAt(int index, char ch)
Set the character at index position to ch
int indexOf(String str)
Returns the position of the first occurrence of str
int indexOf(String str, int
fromIndex)
Find the first occurrence of str from the fromIndex position
int lastIndexOf(String str)
Returns the position of the last occurrence of str
int lastIndexOf(String str, int fromIndex)
Find the last occurrence of str from the fromIndex position
StringBuff insert(int
offset, String str)
Insert at offset position: eight base class types & String type & Object type data
StringBuffer
deleteCharAt(int index)
Delete the index position character
StringBuffer delete(int
start, int end)
Delete the characters in the interval [start, end)
StringBuffer replace(int
start, int end, String str)
Replace the characters at [start, end) with str
String substring(int start)
The characters from start to the end are returned as String
String substring(int
start int end)
Return the characters in the range of [start, end) as String
StringBuffer reverse()
reverse string
String toString()
Return all characters as String

Supplement 2:

Try to avoid directly modifying String type objects, because the String class cannot be modified, and all modifications will create new objects, which is very inefficient.

2. The first unique character in the string

The first unique character in a string icon-default.png?t=N7T8https://leetcode.cn/problems/first-unique-character-in-a-string/

 

 Method: auxiliary array method

class Solution {
    public int firstUniqChar(String s) {
     int[] arr=new int[26];
     for(int i=0;i<s.length();i++){
         arr[s.charAt(i)-97]++;
     }
     for(int i=0;i<s.length();i++){
         if(arr[s.charAt(i)-97]==1){
             return i;
         }
     }
     return -1;
    }
}

Parse:

We can traverse the string twice. In the first pass, we use the auxiliary array to count the number of occurrences of each character in the string. arr[s.charAt(i)-97]++;

In the second traversal, as long as we traverse the character that appears once, then return its index

if(arr[s.charAt(i)-97]==1){
             return i;
   }

Otherwise return −1 after traversal


 3. The length of the last word of the string

The problem is very simple, as long as you make good use of the methods in the library functions of String.

Method 1: use split

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String s= in.nextLine();
        String[] arr=s.split(" ");
        System.out.println(arr[arr.length-1].length());
    }
}

Method 2: use lastIndexOf, substring

public class Main {
   public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String s=in.nextLine();
        int x=s.lastIndexOf(" ");
        System.out.println(s.substring(x+1).length());
    }
}

4. Verify the palindrome

Validate palindrome icon-default.png?t=N7T8https://leetcode.cn/problems/valid-palindrome/

 

class Solution {
     public static boolean isNumOrCharacter(char ch) {
        if (ch >= '0' && ch <= '9' || ch >= 'a' && ch <= 'z') {
            return true;
        }
        return false;
    }

    public static boolean isPalindrome(String s) {
        s = s.toLowerCase();
         int i=0,j=s.length()-1;
         while(i<j){
             while(i<j&&!isNumOrCharacter(s.charAt(i))){
                 i++;
             }
             while(i<j&&!isNumOrCharacter(s.charAt(j))){
                 j--;
             }

             if(s.charAt(i)!=s.charAt(j)){
                 return false;
             }
             i++;
             j--;
         }
         return true;
    }
}

 Parse:


The above is my personal sharing, if you have any questions, welcome to discuss! !

I've seen this, why don't you pay attention and give a free like 

 

Guess you like

Origin blog.csdn.net/WHabc2002/article/details/132699498