Scanner类
Scanner类是用于获取用户键盘输入的类。
System类下有静态字段:
public static final InputStream in;//标准的输入流,对应着键盘输入。
Scanner类中的hasNext()方法和next()方法。
hasNext()方法是判断下一个是不是某种类型的元素。
Scanner input= new Scanner ( System. in) ;
if ( input. hasNextInt ( ) ) {
System. out. println ( "输入的是整形数据。" ) ;
} else {
}
next()方法是获取下一个输入项。
nextInt()方法是获取一个int类型的值。
nextLine()方法是获取一个String类型的值。
String类
字符串:由多个字符组成的字符序列,可以看成字符数组。
字符串的字面值可以看成是一个字符串对象,字符串一旦被创建,就不能改变,改变的只是对字符串的引用。
String类的构造方法
public String():空构造
public String(String original):把字符串常量值转成字符串
public static void main ( String[ ] args) {
String s= new String ( "abc" ) ;
System. out. println ( s) ;
}
public String(byte[] bytes):把字节数组转成字符串
public static void main ( String[ ] args) {
byte [ ] bytes= new byte [ ] { 1 , 127 } ;
String s= new String ( bytes) ;
System. out. println ( s) ;
}
public String(byte[] bytes,int index,int length):把字节数组的一部分转成字符串(index:表示的是从第几个索引开始, length表示的是长度)
byte [ ] bytes= new byte [ ] { 1 , 127 , 53 , 55 , 56 } ;
String s= new String ( bytes, 2 , 2 ) ;
System. out. println ( s) ;
public String(char[] value):把字符数组转成字符串
public static void main ( String[ ] args) {
char [ ] chars= new char [ ] { 'c' , 'd' , 'a' , 'e' } ;
String s= new String ( chars) ;
System. out. println ( s) ;
}
public String(char[] value,int index,int count):把字符数组的一部分转成字符串
public static void main ( String[ ] args) {
char [ ] chars= new char [ ] { 'c' , 'd' , 'a' , 'e' } ;
String s= new String ( chars, 2 , 2 ) ;
System. out. println ( s) ;
}
字符串的方法
public int length():返回此字符串的长度。
public static void main ( String[ ] args) {
String s= "adsadasdasd" ;
System. out. println ( s. length ( ) ) ;
}
String类的判断功能
public boolean equals(Object obj): 比较字符串的内容是否相同,区分大小写
public static void main ( String[ ] args) {
String s1= new String ( "abc" ) ;
String s2= "abc" ;
System. out. println ( s1. equals ( s2) ) ;
}
public boolean equalsIgnoreCase(String str): 比较字符串的内容是否相同,忽略大小写
public static void main ( String[ ] args) {
String s1= new String ( "abc" ) ;
String s2= "ABC" ;
System. out. println ( s1. equalsIgnoreCase ( s2) ) ;
}
public boolean contains(String str): 判断字符串中是否包含传递进来的字符串
public static void main ( String[ ] args) {
String s= "zhaorunfa" ;
System. out. println ( s. contains ( "zhao" ) ) ;
}
public boolean startsWith(String str): 判断字符串是否以传递进来的字符串开头
public static void main ( String[ ] args) {
String s= "zhaorunfa" ;
System. out. println ( s. startsWith ( "zhao" ) ) ;
}
public boolean endsWith(String str): 判断字符串是否以传递进来的字符串结尾
public static void main ( String[ ] args) {
String s= "zhaorunfa" ;
System. out. println ( s. endsWith ( "fa" ) ) ;
}
public boolean isEmpty(): 判断字符串的内容是否为空串""。
public static void main ( String[ ] args) {
String s= "zhaorunfa" ;
System. out. println ( s. isEmpty ( ) ) ;
}
String类的获取功能
public char charAt(int index): 获取指定索引位置的字符
public static void main ( String[ ] args) {
String s= "zhaorun" ;
System. out. println ( s. charAt ( 2 ) ) ;
}
public int indexOf(int ch): 返回指定字符在此字符串中第一次出现处的索引。
public static void main ( String[ ] args) {
String s= "zhaorun" ;
System. out. println ( s. indexOf ( 'a' ) ) ;
}
public int indexOf(String str): 返回指定字符串在此字符串中第一次出现处的索引。
public static void main ( String[ ] args) {
String s= "zhaorun" ;
System. out. println ( s. indexOf ( "ao" ) ) ;
}
public int indexOf(int ch,int fromIndex):返回指定字符在此字符串中从指定位置后第一次出现处的索引。
public int indexOf(String str,int fromIndex): 返回指定字符串在此字符串中从指定位置后第一次出现处的索引。
public static void main ( String[ ] args) {
String s= "zhaorun" ;
System. out. println ( s. indexOf ( "ao" ) ) ;
}
可以顺带提一下lastIndexOf系列//从字符串的后面获取索引
public String substring(int start): 从指定位置开始截取字符串,默认到末尾。
public static void main ( String[ ] args) {
String s= "zhaorunao" ;
System. out. println ( s. substring ( 2 ) ) ;
}
public String substring(int start,int end): 从指定位置开始到指定位置结束截取字符串。
public static void main ( String[ ] args) {
String s= "zhaorunao" ;
System. out. println ( s. substring ( 2 , 7 ) ) ;
}
String的转换功能:
public byte[] getBytes(): 把字符串转换为字节数组。
public char[] toCharArray(): 把字符串转换为字符数组。
public static void main ( String[ ] args) {
String s= "zhaorunfa" ;
System. out. println ( s. toCharArray ( ) ) ;
}
public static String valueOf(char[] chs): 把字符数组转成字符串。
public static String valueOf(int i): 把int类型的数据转成字符串。
注意:String类的valueOf方法可以把任意类型的数据转成字符串。
public String toLowerCase(): 把字符串转成小写。
public String toUpperCase(): 把字符串转成大写。
public static void main ( String[ ] args) {
String s= "zhaorunfa" ;
System. out. println ( s. toUpperCase ( ) . toLowerCase ( ) ) ;
}
public String concat(String str): 把字符串拼接。
public static void main ( String[ ] args) {
String s= "zhaorun" ;
System. out. println ( s. concat ( "fa" ) ) ;
}
String类的替换功能:
public String replace(char old,char new) 将指定字符进行互换
public String replace(String old,String new) 将指定字符串进行互换
public static void main ( String[ ] args) {
String s= "zhaorunao" ;
System. out. println ( s. replace ( "ao" , "!" ) ) ;
}
String的去除字符串两空格
public String trim() 去除两端空格
public static void main ( String[ ] args) {
String s= " zhaorunao " ;
System. out. println ( s. trim ( ) ) ;
}
String的按字典顺序比较两个字符串
public int compareTo(String str) 会对照ASCII 码表 从第一个字母进行减法运算 返回的就是这个减法的结果
如果前面几个字母一样会根据两个字符串的长度进行减法运算返回的就是这个减法的结果
public static void main ( String[ ] args) {
String s= "abc" ;
System. out. println ( s. compareTo ( "abc" ) ) ;
}
如果两个字符串一摸一样 返回的就是0
public int compareToIgnoreCase(String str) 忽略大小写的比较
这个方法忽略大小写的比较,如果说“abc”与“ABC”用这个方法比较的话,返回值也是0。