String connection
When outputting multiple results, multiple results can be connected together for output to solve the hoarding of code, splicing and easy to read string types.
- +: addition operation can splice any data type into a string, but there must be a string type (which can be an empty string). It is best to put it at the beginning, otherwise it will be added if multiple same data types are encountered.
- concat(String str): only string types can be spliced,
public class dome2{ public static void main(String[] args) { String str="three countries"; int i=99; float f=55.0f; // String connected with + System.out.println(i+f+"i and f Added"); System.out.println("i and f No addition"+i+f); //Connect using concat method System.out.println(str.concat(",Water Margin").concat(",The Dream of Red Mansion").concat(",Journey to the West")); //Use together System.out.println(str.concat(",Water Margin")+",The Dream of Red Mansion".concat(",Journey to the West")); } }
result
154.0i and f are added
i and f do not add 9955.0
Three Kingdoms, outlaws of the marsh, dream of Red Mansions, journey to the West
Three Kingdoms, outlaws of the marsh, dream of Red Mansions, journey to the West
String length
- The length method is used to obtain the string length
- character string. length();
Example 1
Enter the bank card password. The password can only be 4-6 digits, not too long or too short. Write a Java program
public class dome2{ public static void main(String[] args) { Scanner sc=new Scanner(System.in); boolean b=true; while(b) { System.out.println("Please input a password"); String input=sc.next(); if(input.length()<7&&input.length()>=4) { System.out.println("The password is entered correctly"); b=false; }else if(input.length()>=7) { System.out.println("The password is too long, please re-enter"); }else { System.out.println("The password is too short. Please re-enter it"); } } } }
toggle case
- toLowerCase(); Replace all characters of the string with lowercase, and non characters are not affected.
- toUpperCase() ; Converts all characters of the string to uppercase, and non characters are not affected.
public class dome2{ public static void main(String[] args) { String str="Hello World!"; System.out.println(str.toLowerCase()); System.out.println(str.toUpperCase()); } }
result
hello world!
HELLO WORLD!
String de space
- Generally, spaces are useless for strings, but sometimes there are more spaces when participating in the operation, which will affect the result, and the space character is also a character. Sometimes, when comparing characters, the characters are the same, that is, there are more spaces, resulting in errors.
- The trim() method is introduced here, but it can only remove the space character of the beginning and end = =
public class dome2{ public static void main(String[] args) { String str=" H ell o Wo r ld! "; System.out.println(str.length()); System.out.println(str.trim().length()); } }
result
23
16
Extraction of substring
When operating a String, it is often filtered to extract the unnecessary and useful. Two methods are also provided in the String.
- substring(int beginIndex); Extract the string from the index to the end. Beginindex starts with the string to be extracted and returns the newly extracted string.
- substring(int beginIndex,int endIndex) ; Extract the characters from beginindex (start index) to endindex (end index), but excluding the characters at the end index, and return the extracted new string.
- substring() is truncated by character, not character.
- String interception starts with the first character index of 0 and ends with the first character of 1.
public class dome2{ public static void main(String[] args) { String str="hello woeld!"; System.out.println(str.substring(4)); System.out.println(str.substring(3,7)); } }
result
o woeld!
lo w