Some usages of String

1. String is immutable. Once generated, it will exist in memory until the system automatically recycles it. From the perspective of memory safety, the array form is more secure than String. For example with char[]  you can explicitly change the elements of the array, so with arrays the security information will likely not be stored anywhere in system memory.


2.  JAVA 7 and later versions are supported. In JDK 7, you are allowed to use strings as comparison conditions for switch statements. Versions before jdk 6 cannot be used like this:

 
 
  1. // java 7 only! 
  2. switch (str.toLowerCase()) { 
  3.       case"a" 
  4.            value = 1
  5.            break
  6.       case"b" 
  7.            value = 2
  8.            break

3 How to convert a string to a numeric int type?

 
 
  1. int n = Integer.parseInt("10"); 

Simple, but often used and easily overlooked.

4. How to split a string with a space character?

We can easily use regular expressions to split strings, "s" means space characters, such as " ", "t", "r", "n"

 
 
  1. String[] strArray = String.split("s+"); 


5. Comparison of equals() and == 

6, There is a replacement method in the String method. format

Example:   The { } symbol can be removed

String s = String.format("I'm testing the {%s} format of the string. If there is no problem {%s}","abc","It's over");
  System.out.println("S == "+s);

 
 



 




Guess you like

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