A one-character constant is a character enclosed in single quotes, such as 'a', '*', 'good'. A Chinese character is also a character of Unicode, so it is also a character of Java;
character type
single character
package hello;
import java.util.Scanner;
public class Main{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
char c = 'A';
char d = 'D';
c ++;
System.out.println(c);
System.out.println(d - c);
System.out.println((char)65);
}
}
case conversion
package hello;
import java.util.Scanner;
public class Main{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
char c = 'A';
System.out.println((char)(c + 32));
}
}
escape character
package type
For primitive data types, Java provides corresponding wrap types. These wrapper types convert data of a primitive data type into objects, so that they can participate in operations and transfer like objects. The following table lists the wrapped types corresponding to the basic data types:
Except for int and char, the wrapped type is to capitalize the first letter of the name of the basic type. In Java's system class library, all capital letters are the name of the class. Therefore, when editing the program, be careful with upper and lower case, so as not to accidentally make mistakes;
The usefulness of the package class:
get the maximum and minimum values of the type
Integer.MAX_VALUE
Integer.MIN_VALUE
(-2147483648 2147483647)
.operator
When you need to make a class or object do something, use the . operator;
Character
package hello;
import java.util.Scanner;
public class Main{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
String str = in.nextLine();
int count = 0;
for(int i = 0; i < str.length(); i ++)
{
if(Character.isDigit(str.charAt(i)))//判断字符串中有几个数字
{
count ++;
}
}
System.out.println(count);
}
}
Math class
abs absolute value
round rounding integer
random random number
pow power
string
A string variable is similar to an array variable, it does not store the string, it is not the owner of the string, it is the manager of the string;
Java's string is also a special "immutable" object, all string operations are to generate a new string, rather than modify the original string. It is important to understand this;
String is a class, and the variable of String is the manager of the object rather than the owner;
String s = new String("a string")
Process:
Create a String object,
initialize the object with "a string",
create a variable s to manage the object, and
let the manager manage the object;
Note: You can also use it directly:
String a = "a string"
The compiler helps us create an object of the String class and give it to s to manage;
input string
in.next() reads a word, and the sign of the word is a space;
spaces include spaces, tabs, and newlines;
in.nextLine() reads a whole line;
Compare two Strings
Compare if the same manager
if(input == "bye"){
……
}
Compare whether the content is the same
if(input.equals("bye")){
……
}
String manipulation
Strings size comparison
Two strings can be compared in size
s1.compareTo(s2);
If s1 is less than s2, then the result is negative; if s1 and s2 are equal, then the result is 0; if s1 is greater than s2, then the result is positive;
compareToIgnoreCase 可以不区分大小写来比较大小
Get the length of String
用 length() 函数
It is not the same as the length of the array, the array is a.length and the string is a.length();
,
Access characters in String
s.charAt(index)
- returns a single character at index
- index ranges from 0 to length() - 1
- The index of the first character is 0, huh, the same as the array
- But you can't use a for-each loop to iterate over a string
package hello;
import java.util.Scanner;
public class Main{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
String a = "all lla";
for(int i = 0; i < a.length(); i ++)
{
System.out.println(a.charAt(i));
}
}
}
get string
Get the whole content from position n to the end;
the subscript starts from 0;
s.substring(n)
Get the content from position b to position e;
the subscript starts from 0, excluding position e;
s.substring(b, e)
package hello;
import java.util.Scanner;
public class Main{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
String s = "0123456789";
System.out.println(s.substring(2, 5));
}
}
/*234*/
find characters
Subscripts start from 0;
package hello;
import java.util.Scanner;
public class Main{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
String s = "0123456789";
System.out.println(s.indexOf('5'));
}
}
If the string appears more than one, want to find the second 3;
package hello;
import java.util.Scanner;
public class Main{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
String s = "0123456389";
int n = s.indexOf('3');
System.out.println(s.indexOf('3', n + 1));
}
}
/*7*/
s.lastIndexOf() searches from the right, but the returned index is still counted from the left;
package hello;
import java.util.Scanner;
public class Main{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
String s = "0123456389";
System.out.println(s.lastIndexOf('3'));
}
}
Other String operations
- s.startsWith(t) whether the string starts with t
- s.endsWith((t) whether the string ends with t
- s.trim() removes spaces on both sides of a string
- s.replace(c1, c2) replaces all c1 in the string with c2
- s.toLowerCaes() replaces all uppercase letters in the entire string with lowercase letters
- s.toUpperCase(0 replaces all lowercase letters in the entire string with uppercase letters
None of this series of operations can change its own province, but create a new string;
Using strings in switch-case
switch(s){
case "this" …… break;
case "this" …… break;
……
}