文章标题
2017-07-30 17:09
162 查看
String类的常用方法
:*## 标题 ##类String
**1、String代表字符串。**Java中的所有字符串自面值(如”abc”)都作为此类的实例实现
字符串是常量,它们的值在创建之后不能更改,(字符串一但被赋值,其值不能在改变)不可变的字符序列
2、常用的构造方法
A:public String():空参构造方法
B:public String(byte[]bytes):将字节数组转换成字符串了
C:public String(byte[]bytes,int index,int length)将部分字节数组转换成字符串
D:public String(char[] value)将字符数组转换成字符串
E:public String(char[] value,int offset,int count)将字符数组的一部分转换成字符串
F:public String(String original)将字符串常量转换成字符串
3、String类中有一个特有功能:
public int length()返回此字符串的长度
在数组中有没有length方法,在字符串中有没有length方法?
答案:数组:length属性 字符串中有length()方法
代码:
public class StringDemo {
public static void main(String[] args) {
//A:public String():空参构造方法
String s1=new String();
System.out.println(“s1:”+s1);//空参
System.out.println(“s1.length():”+s1.length());
System.out.println(“——–”);
//B:public String(byte[]bytes):将字节数组转换成字符串了
byte[] bys={97,98,99};
String s2=new String(bys);
System.out.println(“s2:”+s2);
System.out.println(“s2.length():”+s2.length());
System.out.println(“——–”);
//C:public String(byte[]bytes,int index,int length)将部分字节数组转换成字符串
String s3=new String(bys,0,2);
System.out.println(“s3:”+s3);
System.out.println(“s3.length():”+s3.length());
System.out.println(“——–”);
//D:public String(char[] value)将字符数组转换成字符串
char[] ch={‘我’,’爱’,’学’,’习’};
String s4=new String(ch);
System.out.println(“s4:”+s4);
System.out.println(“s4.length():”+s4.length());
System.out.println(“——–”);
// E:public String(char[] value,int offset,int count)将字符数组的一部分转换成字符串
String s5=new String(ch,1,2);
System.out.println(“s5:”+s5);
System.out.println(“s5.length():”+s5.length());
System.out.println(“——–”);
//F:public String(String original)将字符串常量转换成字符串
String s6=new String(“hello”);
String s7=s6;
System.out.println(“s6:”+s6+”,”+”s7:”+s7);
}
}
4)字符串相加
字符串变量相加:先开辟空间,在拼接
字符串常量相加:先拼接,再去字符串常量池找有没有这个字符串常量,有就直接返回你的地址值,没有的情况下开辟空间
代码:
public class StringDemo {
public static void main(String[] args) {
String s1=”hello”;
String s2=”world”;
String s3=”helloworld”;
System.out.println(s3==s1+s2);//false
System.out.println(s3.equals(s1+s2));//true
System.out.println(“——–”);
System.out.println(s3==”hello”+”world”);//true
System.out.println(s3.equals(“hello”+”world”));//true
}
}
面试题:
以下创建字符串对象的方式有什么区别以及他们分别创建了几个对象
String s1=new String(“hello”);
String s2=”hello”;
答案:共同点:在内存中会构造一个字符串常量池,他们的值是”hello“
s1创建了两个对象
s2创建了一个对象,直接指向常量池中的
**5)String类的判断功能:** A:boolean equals(Object obj):将此字符串和指定的对象进行比较 B:boolean equalsIgnoreCase(String str):将此字符串和另一个字符串进行比较,忽略大小写 C:boolean contains(String str):此字符串中是否包含了一个子字符串:str D: boolean startsWith(String str):判断这个字符串是否以子字符(str)串开头; E:boolean endsWith(String str):判断这个字符串是否以子字符串str结尾 F:boolean isEmpty():判断该字符串是否为空
代码:
public class StringDemo {
public static void main(String[] args) {
//定义三个字符串
String s1=”helloworld”;
String s2=”helloworld”;
String s3=”Helloworld”;
//A:boolean equals(Object obj):将此字符串和指定的对象进行比较
System.out.println(“equals:”+s1.equals(s2));
System.out.println(“equals:”+s1.equals(s3));
System.out.println(“———-“);
// B:boolean equalsIgnoreCase(String str):将此字符串和另一个字符串进行比较,忽略大小写
System.out.println(“equalsIgnoreCase:”+s1.equalsIgnoreCase(s2));
System.out.println(“equalsIgnoreCase:”+s1.equalsIgnoreCase(s3));
System.out.println(“———-“);
// C:boolean contains(String str):此字符串中是否包含了一个子字符串:str
System.out.println(“contains:”+s1.contains(“rld”));
System.out.println(“———-“);
// D: boolean startsWith(String str):判断这个字符串是否以子字符(str)串开头;
System.out.println(“startsWith:”+s1.startsWith(“hel”));
System.out.println(“———-“);
// E:boolean endsWith(String str):判断这个字符串是否以子字符串str结尾
System.out.println(“endsWith:”+s1.endsWith(“ld”));
System.out.println(“———-“);
//F:boolean isEmpty():判断该字符串是否为空
System.out.println(“isEmpty:”+s1.isEmpty());
System.out.println(“———-“);
//java的开发原则
String s4=null;
if(s4!=null){
System.out.println(“s4.length():”+s4.length());
}
String s5=”“;//空字符串,有对象
System.out.println(“s5.length():”+s5.length());
}
}
6)案例:模拟用户登陆,给3次机会进行提示!
import java.util.Scanner;
/*
* 案例:模拟用户登陆,给3次机会进行提示!
* 分析:
* 1)假设现在已经存在用户名和密码
* 2)给3次机会使用for循环,
* for(int x=0;x<3;x++);即x=0,1,2;
* 3)键盘录入用户名和密码
* 在这里判断
* 用户输入的用户名和密码和已经存在的用户名和密码一致,登陆成功
* 4)不成功
* 给出提示
* 判断以下(2-x)==0次,给出另一种提示:账户已经被锁定,请联系管理员
*/
public class StringTest {
public static void main(String[] args) {
// 1)定义现在已经存在用户名和密码
String userName=”yrr”;
String userPassword=”123yrr”;
//创建键盘录入对象
for(int x=0;x<3;x++){
//创建键盘录入对象
Scanner sc=new Scanner(System.in);
//接收并录入数据
System.out.println(“请输入用户名”);
String name=sc.nextLine();
System.out.println("请输入密码");
String pwd=sc.nextLine();
//用户输入的用户名和密码和已经存在的用户名和密码一致,登陆成功
if(name.equals(userName) && pwd.equals(userPassword)){
System.out.println("恭喜您登陆成功");
break;
}else{//不成功
//给出提示
//
4000
判断以下(2-x)==0次,给出另一种提示:账户已经被锁定,请联系管理员
if(2-x==0){
//0次机会
System.out.println("账户已经被锁定,请联系管理员");
}else{
//不是0次
System.out.println("您还剩下"+(x-2)+"次机会,请重写输入");
}}}
}
}
7)String类的获取功能
A:int length():获取字符串的长度
B:char charAt(int index):获取指定索引处的字符
C:int indexOf(int ch):返回指定字符第一次在该字符中出现的索引
参数中为什么是int? 97和”a”
D:int indexOf(String str):返回指定子字符串(str)在大字符串中第一次出现的索引
E:int indexOf(int ch ,fromIndex):返回此字符从指定位置开始后第一次出现的索引
F:int indexOf(String str ,fromIndex):返回子字符串从指定位置开始后第一次出现的索引
G:String subString(int start):截取:从指定位置开始截取,默认截取到末尾
H:String subString(int start,int end):截取功能:从指定位置开始截取到指定位置,包前不包后
代码:
public class StringDemo {
public static void main(String[] args) {
//定义一个字符串
String s=”helloworld”;
// A:int length():获取字符串的长度
System.out.println(“length():”+s.length());
//B:char charAt(int index):获取指定索引处的字符
System.out.println(“charAt():”+s.charAt(3));
// C:int indexOf(int ch):返回指定字符第一次在该字符中出现的索引
System.out.println(“indexOf():”+s.indexOf(‘l’));
System.out.println(“indexOf():”+s.indexOf(‘n’));//该字符串中没有该字符,返回-1;
//D:int indexOf(String str):返回指定子字符串(str)在大字符串中第一次出现的索引
System.out.println(“indexOf():”+s.indexOf(“world”));
//E:int indexOf(int ch ,fromIndex):返回此字符从指定位置开始后第一次出现的索引
System.out.println(“indexOf():”+s.indexOf(‘l’,4));
// F:int indexOf(String str ,fromIndex):返回子字符串从指定位置开始后第一次出现的索引
System.out.println(“indexOf():”+s.indexOf(“or”,4));
//G:String subString(int start):截取:从指定位置开始截取,默认截取到末尾
System.out.println(“subString():”+s.substring(4));
// H:String subString(int start,int end):截取功能:从指定位置开始截取到指定位置,包前不包后
System.out.println(“subString():”+s.substring(4,8));
}
}
8)如何获取字符串里面的字符?
charAt(int index):获取指定索引处的字符
字符串的特有功能:转换功能:将一个字符串可以转换成字符数组toCharArry
例:public class StringTest {
public static void main(String[] args) {
//定义一个字符串
String s=”helloworld”;
//字符串的特有功能:转换功能:将一个字符串可以转换成字符数组toCharArry
char[] chs=s.toCharArray();
for(int i=0;i