JAVA Basics 5 - Strings

1. String definition

Direct definition: String str1 = "abc" ; String str2 = "abc" ;,

Create a new string: String str3 = new String(); String str4 = new String();

The string type is a reference type, and the defined variable is an object, which is written in the same way as object-oriented.

Note: str1 == str2 The return value is true, because the value of abc is in the constant pool of memory. If the value is the same when it is defined again, both str1 and str2 point to the same value.

   str3 == str4 The return value is false, because new is used, and two spaces are created in memory, and str3 and str4 point to different objects respectively.

   String str3 = new String("abc") is equivalent to String str3 = "abc", but the underlying operating mechanism is different.

   String str1 = null and String str2 = "" have different meanings, the first one does not point to anything, and the second one is empty.

2. String concatenation

Each string is connected with "+". When connecting with other data types, the return value after the connection is a string.

3. Common string manipulation methods

  String str = "abc"; String str2 = "abc"

  • str.equals(str2); // Compare whether the values ​​of two strings are equal; ( == when used for non-basic data types refers to comparing whether the addresses pointed to by two variables are the same, and equals compares whether the values ​​of two objects are equal equal)
  • str.length();// Get the length of the entire string;
  • str.trim();// remove the spaces on both sides of the string;
  • str.charAt(int i);// Get the character at an index value;
  • str.contains(CharSequence ab);// Whether it contains a string; CharSequence character sequence
  • str.startsWith(String ab); // Determine if it starts with a string;
  • str.endsWith(String ab); // Determine if it ends with a string;
  • str.replace(char a, char e); // Replace all a in the string with e;
  • str.replace(CharSequence ab, CharSequence aa);// Replace all ab in the string with aa;
  • str.split(String s); // Split the string; split the string by s, the return value is an array;
  • str.toUpperCase(); // Convert the string to uppercase
  • str.toLowerCase(); // Convert to lowercase
  • str.indexOf(String ab);//Get the index position of the first occurrence of this string
  • str.lastIndexOf(String s);//Get the index position of the last occurrence of this string
  • str.substring(int i);//The index value is the string following the integer parameter
  • str.substring(int i, int j);//Take the string between i and j (excluding j)
  • String.valueOf(any args); // Convert any parameter to a string; it is a method on the class, which is a static method.

4. Common string buffer operation methods (mainly used to construct strings)

The return value is generally StringBuffer. Coherent operations of the class can be performed, but it is not recommended.

  StringBuffer buffer = new Stringbuffer("abc")

  • buffer.append(String 123); // append a string;
  • buffer.insert(int 1, String 123); // Insert a string at an index; output is a123bc
  • buffer.deleteCharAt(int index);// delete the character at a certain index position;
  • buffer.delete(int start, int end);//Delete characters between two indices;
  • buffer.replace(int start, int end, String s);//替换两个索引之间的字符;
  • buffer.setCharAt(int index, char ch);设置某个索引位置上的字符;
  • buffer.toString();把字符串缓冲转换成字符串;在打印输出时默认调用此方法;
  • buffer.reverse() // 反转字符串

注意:字符串与字符串缓冲区别在于字符串是常量,无法更改,当进行操作时其效率远低于使用字符串缓冲;

五.System类

  • System.currentTimeMillis() ;// 返回当前的系统时间(以时间戳的形式)
  • System.exit(0);//退出当前正在运行的java虚拟机, 0表示正常退出,其他都是不正常退出,一般用-1,表示整个都不运行了。
  • System.gc();//手动启动垃圾回收机制;JAVA里自动启动垃圾回收机制,超出变量作用域的对象, 会及时的被垃圾回收机制回收。不推荐手动调用此方法(降低程序运行效率),此方法会自动调用finalize方法。
  • System.arraycopy(Object src, int srcPos, Object dest, int destPos, int length);//数组的复制;分别指复制来源名,来源开始复制索引位置,复制目标名,目标开始复制索引位置,复制长度。

六.数字包装类

自动装箱:

基础数据类型不能等于null,将其进行包装便可以,每一个基础数据类型都有自己的包装类。

int 对应Integar;char 对应character;其他都是首字母大写例: long对应Long;包装后是一种对象,可以等于null。

Integar i = 2;此段代码让程序完成了对变量i的自动装箱;

自动拆箱:

  • Integar.parseInt("123");//将字符串123转换成数字123;如果字符串中有非数字字符则会报错;
  • Integer.parseInt(String s, int radix); // 将其他进制的字符串转换成int;逗号后面写被转换的进制,如写16则是将16进制的字符串转化为10进制;radix:基数;
  • Integer.toBinaryString(int i); // 将一个int值转换成二进制的字符串
  • Integer.toOctalString(int i); // 将一个int转换成八进制的字符串
  • Integer.toHexString(int i); // 将一个int转换成16进制的字符串

 

Guess you like

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