String类
String类是字符串的描述类型
1.String的两种实例化方式
(1)String类对象直接赋值
public class StringDemo {
public static void main(String[] args)
{
String str = "hello" ;
System.out.println(str);
}
}执行结果为 hello
(2)利用构造方法赋值
public class StringDemo {
public static void main(String[] args)
{
String str = new String ("hello");
System.out.println(str);
}
}
两种实例化的的区别
直接赋值法:只会开辟一个堆内存,切字符串内容会自动存入对象池,以供下次使用;
构造方法:开辟另个堆内存的空间,切有一个会成为垃圾内存,且不会存入字符串池,但可以手动入池,以供下次直接使用;
一般开发中使用直接赋值,便于优化程序响应时间
2.字符串相等比较
String类相等比较使用 equals()方法 A.equals(B);
但是也会有"=="比较的是数值,即比较两个对象的内存的地址数值;
3.字符串匿名对象
所有使用字符串符号("")的对象的都是String的匿名的对象。
public class StringDemo {
public static void main(String[] args)
{
String str = new String ("hello");
System.out.println("hello".equals(str));
}
}
4.String类型的常用方法
(1)DOC文档
类的组成结构: 类中的成员(Filed)类的构造方法(Constructor)类的普通方法(Method)
(2)字符串与字符数组
构造方法为:public String (char[] value){} //将所有的字符数组变为String类对象
普通方法为:public char[] toCharArray(){} 代码为:str.toCharArray()
public class StringDemo {
public static void main(String[] args)
{
String str = "hello";
char [] data = str.toCharArray();
for(int i= 0 ;i<data.length; i++){
System.out.print(data[i] + ".");
}
}
}
运行结果 h.e.l.l.o.
(3)字节与字符串
构造方法为:public String(byte[] byte){} //将所有字节数组变为字符串
普通方法为:public byte[] getBytes(){} //将字符串变为字节数组 代码:byte [] data =str.getBytes();
public class StringDemo {
public static void main(String[] args)
{
String str = "helloworld";
byte [] data = str.getBytes();
System.out.print(new String(data));
}
}执行结果 helloworld
(4)字符串比较
public boolean equals() {} //区分大小写比较 代码:A.equals(B);
public boolean equalsIgnorCasw() {} //区分大小写比较 代码:A.equalsIgnorCasw(B);
public init compareto() {} //比较两者之间相差多少比较结果又三种(相等 结果为0;大于结果为1;小于结果为-1)代码:A.compareTo(B);
(5)字符查找
public boolean contains() {} //判断指定内容是否存在 代码:A.contains(B);
public int indexOf() {} //找到字符串的开始索引 (有多个时,只返回第一个的索引) 代码:A.indexOf(B)
public int lastindexOf() {} //从后向前查找 代码:A.lastIndexOf(B);
public boolean startsWith() {} //判断是否以B开头 代码:A.startsWith(B);
public boolean endsWith() {} //判断是否以B结尾 代码:A.endsWith(B);
public class StringDemo {
public static void main(String[] args)
{
String str = "**helloworld**";
System.out.println(str.contains("world"));
System.out.println(str.indexOf("world"));
System.out.println(str.indexOf("l"));
System.out.println(str.indexOf("l" , 3));//从第三个索引开始查找
System.out.println(str.lastIndexOf("l"));//从后向前开始查找
System.out.println(str.startsWith("**" ));//判断开头是否为**
System.out.println(str.endsWith("##"));//判断结尾是否为##
System.out.println(str.length());//输出str的长度
}
}
true
7
4
4
10
true
false
14
(6)字符串替换
public String replaceAll() {} //替换全部 代码:str.replaceAll("l","-");
public String replaceFirst() {} //替换第一个满足的值 代码:str.replaceFirst("l","-");
public class StringDemo {
public static void main(String[] args)
{
String str = "**helloworld**";
System.out.println(str.replaceAll("l","-"));//替换全部的l
System.out.println(str.replaceFirst("l","-"));//替换第一个l
}
}
执行结果
**he--owor-d**
**he-loworld**
(7)字符串拆分
public String [] spilt() {} //全部拆分
public String [] spilt( , ) {} //部分拆分
遇到不能拆的加转义字符 "\\"
public class StringDemo {
public static void main(String[] args)
{
String str = "hello world hnist";
String result[] = str.split(" ");//从" "全部拆分
String result1[] = str.split(" ", 2);//从" "拆分成两个
for(int i=0;i<result.length;i++){
System.out.println(result[i] + ",");}
for(int j=0;j<result1.length;j++){
System.out.println(result1[j] + ",");
}
}
}
执行结果
hello,
world,
hnist,
hello,
world hnist
(8)字符串的截取
public String substring (int 索引值) {} //从指定索引截取到结尾
public String substring ( int 开始位置, int 截取长度) {} //j截取部位子字符串的数据
public class StringDemo {
public static void main(String[] args)
{
String str = "helloworldhnist";
String resultA = str.substring(5);
String resultB = str.substring(0, 5);
System.out.println(resultA);
System.out.println(resultB);
}
}
执行结果
worldhnist
hello
(9)其他方法、
public String toUpperCase(){} //大写
public String toLowerCase(){} //小写
public String concat(){} //字符串连接
public boolean isEmpty(){} //判断字符串长度是否为 0
public String initcap(){} //首字母大写
public class StringDemo{
public static void main(String[] args)
{
String str = "helloworld";
System.out.println(str);
System.out.println(initcap(str));
}
public static String initcap(String str){ //定义一个首字母大写的方法
if(str==null||"" .equals(str)){
return str ;
}
if(str.length()>1){
return str.substring(0,1).toUpperCase()+str.substring(1) ;
}
return str.toUpperCase();
}
}
浙公网安备 33010602011771号