String common interface API

In the development process, it is often necessary to manipulate strings. Here is a summary of the String interface that is often used, which is also convenient for me to view in the future

One, get character, position

Example: String str="abc_def"

1. Get the length of the string and the number of characters contained in the string.
      Method: int length()
      call:str.length()

2.
      Method of obtaining a certain character in the position according to the position : char charAt(int index)
      call:str.charAt(2) //获取到的字符是"a"

3. Get the position of the character in the string according to the character

      a) Returns the position of the first occurrence of str in the string

int indexOf(String str)

       b) Starting from the position specified by fromIndex, get the position where str appears in the string

int indexOf(String str, int fromIndex)

      c) Scan from back to front to find

int lastIndexOf(String str)

2. Judgment

1. Does the string contain a certain substring?

boolean contains("XXX")

2. Whether the character is empty

boolean isEmpty()//判断长度是否为0

3. Whether the string starts with the specified content

boolean startsWith("XXX");

4. Whether the string ends with the specified content

boolean endsWith("XXX");

5. To determine whether the string content is the same, the equals method is rewritten

boolean equals("XXX");

6. Determine whether the content is the same, and ignore case

boolean equalsIgnoreCase();

Three, conversion

1. Convert a string into a character array

char[] toCharArray()

2. Convert basic data types into strings

static String valueOf(int)
static String valueOf(double)

3. Convert the string to uppercase or lowercase

String toUpperCase();//全部转成大写
String toLowerCase();//全部转为小写

Fourth, replace and split

Replace, the first parameter is the character to be replaced

String replace(oldchar,newchar);

According to a character, the string is divided into a string array

String[] split("X");

Five, intercept and remove spaces

Intercept string

String substring(begin);
String substring(begin,end);//含头不含尾

Remove multiple spaces at both ends of the string

String trim();

6. Convert between String and other types

1、StringBuffer转String

  StringBuffer sb=new StringBuffer("hello world");
  String str=sb.toString();

2. String to JSONObject

//如:str="{\"time\": 1597649129406,\"name\": \"张三\"}";
JSONObject jsonObject =JSONObject.parseObject(str);//String转JSONObject,
String name=jsonObject.getString("name");//name等于张三

3. String to JSONObject, nested

//{"code":0,"data":{"expiresIn":7200000,"surplusCount":89,"accessToken":"dVpBeklKOU93TFdQamlVNW4zUThoQT09"}}
JSONObject jsonObject =JSONObject.parseObject(s).getJSONObject("data");//String转JSONObject,
String Token=jsonObject.getString("accessToken");

4. String to Integer

//String转Integer,back是String类型
Integer id=Integer.valueOf(back);

Everyone is welcome to read. I have limited knowledge. It is inevitable that there are mistakes or omissions in the blog I wrote. I also hope that everyone will give me some advice. Thank you.

Guess you like

Origin blog.csdn.net/qq_41936224/article/details/107844294