String类的概述
1.String类的概述
通过JDK提供的API,查看String类的说明
字符串字面值"abc"也可以看成是一个字符串对象。 字符串是常量,一旦被赋值,就不能被改变。
public class Demo1_String {
public static void main ( String[ ] args) {
String str = "abc" ;
str = "def" ;
System. out. println ( str) ;
}
}
2.String类的构造方法
常见构造方法
public String():
空构造public String(byte[] bytes):
把字节数组转成字符串public String(byte[] bytes,int index,int length):
把字节数组的一部分转成字符串public String(char[] value):
把字符数组转成字符串public String(char[] value,int index,int count):
把字符数组的一部分转成字符串public String(String original):
把字符串常量值转成字符串
public class Demo2_StringCon {
public static void main ( String[ ] args) {
String s1 = new String ( ) ;
System. out. println ( s1) ;
byte [ ] arr1 = { 97 , 98 , 99 } ;
String s2 = new String ( arr1) ;
System. out. println ( s2) ;
byte [ ] arr2 = { 97 , 98 , 99 , 100 , 101 , 102 } ;
String s3 = new String ( arr2, 2 , 3 ) ;
System. out. println ( s3) ;
char [ ] arr3 = { 'a' , 'b' , 'c' , 'd' , 'e' } ;
String s4 = new String ( arr3) ;
System. out. println ( s4) ;
String s5 = new String ( arr3, 1 , 3 ) ;
System. out. println ( s5) ;
String s6 = new String ( "javaEE" ) ;
System. out. println ( s6) ;
}
}
3.String类常见的面试题
* 1. 判断定义为String类型的s1和s2是否相等
* String s1 = "abc" ;
* String s2 = "abc" ;
* System. out. println ( s1 == s2) ;
* System. out. println ( s1. equals ( s2) ) ;
* 2. 下面这句话在内存中创建了几个对象?
* String s1 = new String ( "abc" ) ;
* 3. 判断定义为String类型的s1和s2是否相等
* String s1 = new String ( "abc" ) ;
* String s2 = "abc" ;
* System. out. println ( s1 == s2) ;
* System. out. println ( s1. equals ( s2) ) ;
* 4. 判断定义为String类型的s1和s2是否相等
* String s1 = "a" + "b" + "c" ;
* String s2 = "abc" ;
* System. out. println ( s1 == s2) ;
* System. out. println ( s1. equals ( s2) ) ;
* 5. 判断定义为String类型的s1和s2是否相等
* String s1 = "ab" ;
* String s2 = "abc" ;
* String s3 = s1 + "c" ;
* System. out. println ( s3 == s2) ;
* System. out. println ( s3. equals ( s2) ) ;
public class Demo3_String {
public static void main ( String[ ] args) {
String s1 = "ab" ;
String s2 = "abc" ;
String s3 = s1 + "c" ;
System. out. println ( s3 == s2) ;
System. out. println ( s3. equals ( s2) ) ;
}
private static void demo4 ( ) {
String s1 = "a" + "b" + "c" ;
String s2 = "abc" ;
System. out. println ( s1 == s2) ;
System. out. println ( s1. equals ( s2) ) ;
}
private static void demo3 ( ) {
String s1 = new String ( "abc" ) ;
String s2 = "abc" ;
System. out. println ( s1 == s2) ;
System. out. println ( s1. equals ( s2) ) ;
}
private static void demo2 ( ) {
String s1 = new String ( "abc" ) ;
System. out. println ( s1) ;
}
private static void demo1 ( ) {
String s1 = "abc" ;
String s2 = "abc" ;
System. out. println ( s1 == s2) ;
System. out. println ( s1. equals ( s2) ) ;
}
}
4.String类的判断功能
String类的判断功能
boolean equals(Object obj):
比较字符串的内容是否相同,区分大小写boolean equalsIgnoreCase(String str):
比较字符串的内容是否相同,忽略大小写boolean contains(String str):
判断大字符串中是否包含小字符串boolean startsWith(String str):
判断字符串是否以某个指定的字符串开头boolean endsWith(String str):
判断字符串是否以某个指定的字符串结尾boolean isEmpty():
判断字符串是否为空。
public class Demo4_StringMethod {
public static void main ( String[ ] args) {
String s1 = "javaEE" ;
String s2 = "" ;
String s3 = null;
System. out. println ( s1. isEmpty ( ) ) ;
System. out. println ( s2. isEmpty ( ) ) ;
System. out. println ( s3. isEmpty ( ) ) ;
}
private static void demo2 ( ) {
String s1 = "我爱heima,哈哈" ;
String s2 = "heima" ;
String s3 = "baima" ;
String s4 = "我爱" ;
String s5 = "哈哈" ;
System. out. println ( s1. contains ( s2) ) ;
System. out. println ( s1. contains ( s3) ) ;
System. out. println ( "------------------" ) ;
System. out. println ( s1. startsWith ( s4) ) ;
System. out. println ( s1. startsWith ( s5) ) ;
System. out. println ( "------------------" ) ;
System. out. println ( s1. endsWith ( s4) ) ;
System. out. println ( s1. endsWith ( s5) ) ;
}
private static void demo1 ( ) {
String s1 = "heima" ;
String s2 = "heima" ;
String s3 = "HeiMa" ;
System. out. println ( s1. equals ( s2) ) ;
System. out. println ( s2. equals ( s3) ) ;
System. out. println ( "---------------" ) ;
System. out. println ( s1. equalsIgnoreCase ( s2) ) ;
System. out. println ( s1. equalsIgnoreCase ( s3) ) ;
}
}
5.String类的获取功能
String类的获取功能
int length():
获取字符串的长度。char charAt(int index):
获取指定索引位置的字符int indexOf(int ch):
返回指定字符在此字符串中第一次出现处的索引。int indexOf(String str):
返回指定字符串在此字符串中第一次出现处的索引。int indexOf(int ch,int fromIndex):
返回指定字符在此字符串中从指定位置后第一次出现处的索引。int indexOf(String str,int fromIndex):
返回指定字符串在此字符串中从指定位置后第一次出现处的索引。lastIndexOf
String substring(int start):
从指定位置开始截取字符串,默认到末尾。String substring(int start,int end):
从指定位置开始到指定位置结束截取字符串。
public class Demo5_StringMethod {
public static void main ( String[ ] args) {
String s = "woaiheima" ;
s. substring ( 4 ) ;
System. out. println ( s) ;
}
private static void demo4 ( ) {
String s1 = "heimawudi" ;
String s2 = s1. substring ( 5 ) ;
System. out. println ( s2) ;
String s3 = s1. substring ( 0 , 5 ) ;
System. out. println ( s3) ;
}
private static void demo3 ( ) {
String s1 = "woaiheima" ;
int index1 = s1. indexOf ( 'a' , 3 ) ;
System. out. println ( index1) ;
int index2 = s1. lastIndexOf ( 'a' ) ;
System. out. println ( index2) ;
int index3 = s1. lastIndexOf ( 'a' , 7 ) ;
System. out. println ( index3) ;
}
private static void demo2 ( ) {
String s1 = "heima" ;
int index = s1. indexOf ( 'e' ) ;
System. out. println ( index) ;
int index2 = s1. indexOf ( 'z' ) ;
System. out. println ( index2) ;
int index3 = s1. indexOf ( "ma" ) ;
System. out. println ( index3) ;
int index4 = s1. indexOf ( "ia" ) ;
System. out. println ( index4) ;
}
private static void demo1 ( ) {
String s1 = "heima" ;
System. out. println ( s1. length ( ) ) ;
String s2 = "你要减肥,造吗?" ;
System. out. println ( s2. length ( ) ) ;
char c = s2. charAt ( 5 ) ;
System. out. println ( c) ;
char c2 = s2. charAt ( 10 ) ;
System. out. println ( c2) ;
}
}
6.字符串的遍历
public class Demo6_StringMethod {
public static void main ( String[ ] args) {
String s1 = "heiMA" ;
String s2 = "chengxuYUAN" ;
String s3 = s1. toLowerCase ( ) ;
String s4 = s2. toUpperCase ( ) ;
System. out. println ( s3) ;
System. out. println ( s4) ;
System. out. println ( s3 + s4) ;
System. out. println ( s3. concat ( s4) ) ;
}
private static void demo3 ( ) {
char [ ] arr = { 'a' , 'b' , 'c' } ;
String s = String. valueOf ( arr) ;
System. out. println ( s) ;
String s2 = String. valueOf ( 100 ) ;
System. out. println ( s2 + 100 ) ;
Person p1 = new Person ( "cc" , 23 ) ;
System. out. println ( p1) ;
String s3 = String. valueOf ( p1) ;
System. out. println ( s3) ;
}
private static void demo2 ( ) {
String s = "heima" ;
char [ ] arr = s. toCharArray ( ) ;
for ( int i = 0 ; i < arr. length; i++ ) {
System. out. print ( arr[ i] + " " ) ;
}
}
private static void demo1 ( ) {
String s1 = "abc" ;
byte [ ] arr = s1. getBytes ( ) ;
for ( int i = 0 ; i < arr. length; i++ ) {
}
String s2 = "dd" ;
byte [ ] arr2 = s2. getBytes ( ) ;
for ( int i = 0 ; i < arr2. length; i++ ) {
}
String s3 = "i" ;
byte [ ] arr3 = s3. getBytes ( ) ;
for ( int i = 0 ; i < arr3. length; i++ ) {
System. out. print ( arr3[ i] + " " ) ;
}
}
}
7.String类的其他功能
String的替换功能及案例演示
String replace(char old,char new)
String replace(String old,String new)
String的去除字符串两空格及案例演示
String的按字典顺序比较两个字符串及案例演示
int compareTo(String str)
(暂时不用掌握)int compareToIgnoreCase(String str)(
了解)
public class Demo7_StringMethod {
public static void main ( String[ ] args) {
String s1 = "a" ;
String s2 = "aaaa" ;
int num = s1. compareTo ( s2) ;
System. out. println ( num) ;
String s3 = "黑" ;
String s4 = "马" ;
int num2 = s3. compareTo ( s4) ;
System. out. println ( '黑' + 0 ) ;
System. out. println ( '马' + 0 ) ;
System. out. println ( num2) ;
String s5 = "heima" ;
String s6 = "HEIMA" ;
int num3 = s5. compareTo ( s6) ;
System. out. println ( num3) ;
int num4 = s5. compareToIgnoreCase ( s6) ;
System. out. println ( num4) ;
private static void demo2 ( ) {
String s = " hei ma " ;
String s2 = s. trim ( ) ;
System. out. println ( s2) ;
}
private static void demo1 ( ) {
String s = "heima" ;
String s2 = s. replace ( 'i' , 'o' ) ;
System. out. println ( s2) ;
String s3 = s. replace ( 'z' , 'o' ) ;
System. out. println ( s3) ;
String s4 = s. replace ( "ei" , "ao" ) ;
System. out. println ( s4) ;
}
}
练习
A:案例演示
需求:模拟登录,给三次机会,并提示还有几次。 用户名和密码都是admin 分析: 1,模拟登录,需要键盘录入,Scanner 2,给三次机会,需要循环,for 3,并提示有几次,需要判断,if
import java. util. Scanner;
public class Test1 {
public static void main ( String[ ] args) {
Scanner sc = new Scanner ( System. in) ;
for ( int i = 0 ; i < 3 ; i++ ) {
System. out. println ( "请输入用户名:" ) ;
String userName = sc. nextLine ( ) ;
System. out. println ( "请输入密码:" ) ;
String password = sc. nextLine ( ) ;
if ( "admin" . equals ( userName) && "admin" . equals ( password) ) {
System. out. println ( "欢迎" + userName + "登录" ) ;
break ;
} else {
if ( i == 2 ) {
System. out. println ( "您的错误次数已到,请明天再来吧" ) ;
} else {
System. out. println ( "录入错误,您还有" + ( 2 - i) + "次机会" ) ;
}
}
}
}
}
2.遍历字符串
public class Test2 {
public static void main ( String[ ] args) {
String s = "heima" ;
for ( int i = 0 ; i < s. length ( ) ; i++ ) {
System. out. println ( s. charAt ( i) ) ;
}
}
}
3.统计
A:案例演示
需求:统计一个字符串中大写字母字符,小写字母字符,数字字符出现的次数,其他字符出现的次数。 ABCDEabcd123456!@#$%^ 分析:
字符串是有字符组成的,而字符的值都是有范围的,通过范围来判断是否包含该字符 如果包含就让计数器变量自增
public class Test3 {
public static void main ( String[ ] args) {
String s = "ABCDEabcd123456!@#$%^" ;
int big = 0 ;
int small = 0 ;
int num = 0 ;
int other = 0 ;
for ( int i = 0 ; i < s. length ( ) ; i++ ) {
char c = s. charAt ( i) ;
if ( c >= 'A' && c <= 'Z' ) {
big++ ;
} else if ( c >= 'a' && c <= 'z' ) {
small++ ;
} else if ( c >= '0' && c <= '9' ) {
num++ ;
} else {
other++ ;
}
}
System. out. println ( s + "中大写字母有:" + big + "个,小写字母有:" + small + "个,数字字符:"
+ num + "个,其他字符:" + other + "个" ) ;
}
}
4.案例演示
需求:把一个字符串的首字母转成大写,其余为小写。(只考虑英文大小写字母字符) 链式编程:只要保证每次调用完方法返回的是对象,就可以继续调用
public class Test4 {
public static void main ( String[ ] args) {
String s = "woaiHEImaniaima" ;
String s2 = s. substring ( 0 , 1 ) . toUpperCase ( ) . concat ( s. substring ( 1 ) . toLowerCase ( ) ) ;
System. out. println ( s2) ;
}
}
5.案例五
/**
* A:案例演示
* 需求:把数组中的数据按照指定个格式拼接成一个字符串
* 举例:
* int[] arr = {1,2,3};
* 输出结果:
* "[1, 2, 3]"
*
分析:
1,需要定义一个字符串"["
2,遍历数组获取每一个元素
3,用字符串与数组中的元素进行拼接
*/
public class Test5 {
public static void main ( String[ ] args) {
int [ ] arr = { 1 , 2 , 3 } ;
String s = "[" ;
for ( int i = 0 ; i < arr. length; i++ ) {
if ( i == arr. length - 1 ) {
s = s + arr[ i] + "]" ;
} else {
s = s + arr[ i] + ", " ;
}
}
System. out. println ( s) ;
}
}
案例六
import java. util. Scanner;
public class Test6 {
public static void main ( String[ ] args) {
Scanner sc = new Scanner ( System. in) ;
System. out. println ( "请输入一个字符串:" ) ;
String line = sc. nextLine ( ) ;
char [ ] arr = line. toCharArray ( ) ;
String s = "" ;
for ( int i = arr. length- 1 ; i >= 0 ; i-- ) {
s = s + arr[ i] ;
}
System. out. println ( s) ;
}
}