.java.long.String类
1.String类的两种实例化方式
a.直接赋值(直接赋值,在堆上分配空间)
String str1 = "Hello World";
System.out.println(str1);
b.传统方法(通过构造方法实例化String对象)
String str2 = new String("Hello World");
System.out.println(str2);
2.字符串相等比较
如果是两个int型,判断其相等完全可以用==来判断
public boolean equals(String anotherString):成员方法(通过对象调用)
str1.equals(anotherString)
==比较的是两字符串的地址 equals比较的是两字符串的内容
String str1 = "Hello World";
String str2 = new String("Hello World");
System.out.println(str1 == str2);
String str1 = "Hello World";
String str2 = new String("Hello World");
System.out.println(str1.equals(str2));
3.字符串常量(“ ”)是String的匿名对象
System.out.println(“hello”.equals(str2));(推荐)
String str1 = null;
System.out.println("Hello World".equals(str1));
System.out.println(str2.equals(“hello”)); 如果str为空,则会出现空指针异常
String str1 = null;
System.out.println(str1.equals("Hello World"));
小 tips:如果要判断用户输入的字符串是否等同与特定字符串,一定要将特定字符串(String常量)写在前面,避免NULLPointerException。
4.String类的设计采用了共享设计模式
JVM底层会自动维护一个字符串的对象池(对象数组),如果现在采用直接赋值的形式进行String的对象实例化,该对象会自动保存在这个对象池中。如果下次继续使用直接赋值的模式声明String对象,此时对象池中若有指定内容,则直接引用;如果没有,则开辟新的对空间后将其保存在对象池中供下次使用。
手工入池(本地方法): public native String intern(); 成员方法
a.采用直接赋值法
String str1 = "hello";
String str2 = "hello";
String str3 = "hello";
System.out.println(str1 == str2);
System.out.println(str2 == str3);
System.out.println(str1 == str3);
b.采用构造方法
String str1 = new String("hello");
String str = "hello";
String str1 = new String("hello").intern();
System.out.println(str == str1);
5.字符串常量不可变更
字符串一旦定义后不可改变;字符串的加操作一定少产生,最多不超过3次
String str = "hello";
str += "world";
str += "!!!";
System.out.println(str);
可以发现字符串上没有发生任何变化,但是字符串对象的引用一直在改变,而且会形成大量的垃圾空间,所以以下代码不应该出现:
String str = "hello";
for(int i=0; i<1000; i++){
str += x;
}
System.out.println(str);
原则:
a.字符串使用就采用直接赋值
b.字符串比较就使用equals()实现
c.字符串别改变太多
6.字符与字符串的相互转换(非常重要)
a.将字符数组转为字符串
public String (char[ ] value)-> new String (char[ ])
char[] data = new char[]{'h','e','l','l','o'};
String str = new String(data);
System.out.println(str);
public String(char[ ] value, int offest【开始位置】, int count【数量】)
char[] data = new char[]{'h','e','l','l','o'};
String str = new String(data,0,2);
System.out.println(str);
b.将字符串转为单个字符
public char charAt (int index【下标】) 【如果索引超出范围会越界异常】
String str = "hello";
char c = str.charAt(0);
System.out.println(c);
c.将字符串转为字符数组 String->char[ ]
public char[ ] tocharArray();
public class test{
public static void main(String[] args){
String str = "helloworld";
char[] data = str.toCharArray();
for(int i=0; i<data.length; i++){
System.out.print(data[i]+" ");
}
}
}
取得字符串的长度:public int length(); 字符串是方法,数组是属性。
例:判断一个字符串是否由数字组成
public class test{
public static void main(String[] args){
System.out.println(isNumber("123a"));
}
public static boolean isNumber(String str){
//1.String->data[]
char[] data = str.toCharArray();
//2.判断是否为数字
for(int i=0; i<data.length; i++){
if(data[i]<'0' || data[i]>'9'){
return false;
}
}
return true;
}
}
7.字节(byte)与字符串的转换
a.将字节数组转为字符串(重要) byte[ ] -> String
public String (byte[ ] value);
public String (byte[ ] value, int offest, int count)
byte[] data = new byte[]{1,3,5,7,9};
String str = new String(data);
System.out.println(str);
b.将字符串转为字符数组(数组) String->byte[ ]
public byte[ ] getBytes();
String str = new String("hello");
byte[] data = str.getBytes();
for(byte b:data){
System.out.print(b+" ");
}
c.将字符串按照指定编码转为字节数组
public byte[ ] getBytes(String charsetName);
String str = new String("我想睡觉");
byte[] data = str.getBytes("GBK");
for(byte b:data){
System.out.print(b+" ");
}
8.字符串比较
a.不区分大小写相等比较
public boolean equalsIgnoreCase(String anotherString);
System.out.println("hello".equalsIgnoreCase("Hello"));
b.比较两个字符串大小
public int compareTo(String anotherString);
返回大于0,表示大于比较对象;
返回等于0,表示等于比较对象;
返回小于0,表示小于比较对象;
System.out.println("hello".compareTo("Hello"));
9.字符串查找
a.判断str在本字符串中是否存在
public boolean contains (String str);
String str = "hello";
System.out.println(str.contains("he"));
b.判断是否以指定字符串开头
public boolean startsWith(String str);
String str = "hello";
System.out.println(str.contains("el"));
从指定位置开始判断是否以指定字符串开头 public boolean startsWith(String str,int index);
String str = "hello";
System.out.println(str.startsWith("el",1));
c.判断是否以指定的字符串结尾
public boolean endsWith(String str);
String str = "hello";
System.out.println(str.endsWith("lo"));
10.字符串替换
public String replaceAll(String regex,String replacement):替换所有内容
String str = "hello";
System.out.println(str.replaceAll("l","_"));
public String replaceFirst(String regex,String replacement):替换首个内容
String str = "hello";
System.out.println(str.replaceFirst("l","_"));
11.字符串拆分
public String【】 split(String regex):将字符串按照指定格式全部拆分
String str = "I am a student";
String[] result = str.split(" ");
for(String str1:result){
System.out.println(str1);
}
public String【】 split(String regex,int limit):将字符串部分拆分,数组长度为limit
注意:如果给定长度小于字符串的长度,先按照给定长度进行分配,最后剩余全为最后一个字符串
如果出现特殊字符,一定要用转义字符将其转为普通字符。
String str = "I am a student";
String[] result = str.split(" ",2);
for(String str1:result){
System.out.println(str1);
}
String str = "192.168.11.2";
String[] result = str.split("\\.");
for(String str1 : result){
System.out.println(str1);
}
12.字符串截取
public String substring(int beginIndex)【; 从指定位置截取到字符串结尾
String str = "hello";
System.out.println(str.substring(2));
public String substring (int beginIndex, int endIndex)【);截取部分内容
String str = "hello";
System.out.println(str.substring(0,2));
13.String类的其他方法
a.去掉左右空格,中间的保留
public String trim();
String str = " hello ";
System.out.println(str.trim());
b.转大小写
public String toLowerCase(); 转小写
public String toUpperCase(); 转大写
String str = "helloWORD";
System.out.println(str.toLowerCase());
System.out.println(str.toUpperCase());
c.判断字符串是否为空(只能判断是否为空字符串,而不是null)
public boolean isEmpty();
String str = "";
System.out.println(str.isEmpty());
完整的判断字符串为空: if(str == null || str.isEmpty())
例:将一个字符串首字母大写
public class test{
public static void main(String[] args) throws Exception{
System.out.println(firstCase("helloworld"));
}
public static String firstCase(String str){
return str.substring(0,1).toUpperCase()+str.substring(1);
}
}
14.两只sb(面试)-方便进行字符串的修改-java.long
StringBuffer与StringBuilder使用没有区别
String和两只sb的区别:
I.String的内容不可修改,而两只sb可以改内容(append)
II.StringBuffer采用同步处理,线程安全,效率较低
StringBuilder采用异步处理,线程不安全,效率较高,String的“+”操作底层会将String->Stringbuilder
a.字符串修改
public StringBuffer append(各种数据类型)
StringBuffer sb = new StringBuffer();
sb.append("hello").append("world");
System.out.println(sb);
b.StringBuffer与String的相互转化
I.String->StringBuffer 调用StringBuffer的构造方法或append()
StringBuffer sb = new StringBuffer("hello");
II.StringBuffer->String StringBuffer.toString();
StringBuffer sb = new StringBuffer("hello");
String result = sb.toString();
System.out.println(result.isEmpty());
c.字符串反转
public StringBuffer reverse();
StringBuffer sb = new StringBuffer("hello");
sb.reverse();
System.out.println(sb);
d.删除指定范围的数据
public StringBuffer delete(int start,int end);
StringBuffer sb = new StringBuffer("hello world");
System.out.println(sb.delete(0, 6));
e.插入数据
public StringBuffer insert(int offset,各种数据类型)
StringBuffer sb = new StringBuffer("hello");
System.out.println(sb.insert(5, "真好"));