一. String类简介
String类在java.lang包中, java使用String类创建一个字符串变量, 字符串变量属于对象.
String类对象创建后不能修改.
二. 实例化方式及其区别
1. 直接赋值
String str = "hello";
System.out.println(str);
2. 传统方法: 构造法
String str = new String("hello");
System.out.println(str);
3. 两种方式的比较
1) 直接赋值: 只开辟一块堆内存空间, 并且会自动入池, 不会产生垃圾
2) 构造方法: 会开辟两块堆内存空间, 其中一块内存空间会变成垃圾被系统回收, 而且不能够自动入池, 需要通过public String intern(); 方法进行手工入池
开发过程中不会采用构造方法进行字符串的实例化
三. String常用的方法
1. 字符串比较
字符串相等比较: 见文章 https://mp.csdn.net/postedit/86633654, 除了这个方法之外, String类还提供其他比较操作:
方法名称 | 类型 | 描述 |
public boolean equals(Object obj) | 普通 | 区分大小写的比较 |
public boolean equalsIgnoreCase(String anotherString) | 普通 | 不区分大小写的比较 |
public int compareTo(String anotherString) | 普通 | 比较两个字符串大小关系 |
例子: 不区分大小写比较
String str1 = "Hello";
String str2 = "hello";
System.out.println(str1.equals(str2));//false
System.out.println(str1.equalsIgnoreCase(str2));//true
在String类中compareTo()方法是一个非常重要的方法, 该方法返回一个整型, 该数据会根据大小关系返回三类内容:
- 相等: 返回0
- 小于: 返回内容小于0
- 大于: 返回内容大于0
例子: compareTo()比较
System.out.println("A".compareTo("a"));//-32
System.out.println("a".compareTo("A"));//32
System.out.println("A".compareTo("A"));//0
System.out.println("AB".compareTo("AC"));//-1
compareTo()是一个可以区分大小关系的方法, 是String方法中非常重要的方法
2. 字符串查找
方法名称 | 类型 | 描述 |
public boolean contains(CharSequence s) | 普通 | 判断一个子字符串是否存在 |
public int indexOf(String str) | 普通 | 从头开始查找指定字符串的位置, 查到了返回位置的开始索引, 如果查不到返回-1 |
public int indexOf(String str, int fromIndex) | 普通 | 从指定位置开始查找子字符串位置 |
public int lastIndexOf(String str) | 普通 | 由后向前查找字符串位置 |
public int lastIndexOf(String str, int fromIndex) | 普通 | 从指定位置由后向前查找 |
public boolean startsWith(String prefix) | 普通 | 判断是否以指定字符串开头 |
public boolean startsWith(String prefix, int toffset) | 普通 | 从指定位置开始判断是否以指定字符串开头 |
public boolean endsWith(String suffix) | 普通 | 判断是否以指定字符串结尾 |
例子: 字符串查找最好用的就是contains() , 该方法是JDK1.5后开始追加的
String string = "helloworld";
System.out.println(string.contains("world"));//true
例子: 使用indexOf()方法进行位置查找
String str3 = "helloworld";
System.out.println(str3.indexOf("world"));//5
System.out.println(str3.indexOf("compute"));//-1, 没有找到
if(str3.indexOf("hello")!= -1){
System.out.println("可以找到指定字符串!");
}
注意: 1) 使用indexOf(), 如果内容重复, 他只能返回查找的第一个位置;
2) 在进行查找的时候往往会判断开头或结尾;
3)很多时候用标记做参数处理, 就要用startsWith()与endsWith()来判断
例子:
String str4 = "helloworld";
System.out.println(str4.indexOf("1"));//2
System.out.println(str4.indexOf("1",5));//8
System.out.println(str4.lastIndexOf("1"));//8
String str5 = "**@@helloworld!!";
System.out.println(str5.startsWith("**"));//true
System.out.println(str5.startsWith("@@",2));//true
System.out.println(str5.endsWith("!!"));//true
3. 字符串替换
方法名称 | 类型 | 描述 |
public String replaceAll(String regex, String replacement) | 普通 | 替换所有的指定内容 |
public String replaceFirst(String regex, String replacement) | 普通 | 替换首个内容 |
例子: 字符串的替换处理
String str6 = "helloworld_";
System.out.println(str6.replaceAll("1","_"));
System.out.println(str6.replaceFirst("1",""));
4. 字符串拆分
方法名称 | 类型 | 描述 |
public String[] split(String regex) | 普通 | 将字符串全部拆分 |
public String[] split(String regex, int limit) | 普通 | 将字符串部分拆分, 该数组长度就是limit极限 |
例子: 字符串的拆分处理
String str7 = "hello world hello compute";
String[] result = str7.split(" ");//按照空格拆分
for(String s:result){
System.out.println(s);
}
字符串部分拆分
String str8 = "hello world hello compute";
String[] result1 = str8.split(" ",2);//按照空格拆分
for(String s:result1){
System.out.println(s);
}
多次拆分
String str9 = "compute:123|c:12";
String[]result2 = str9.split("\\|");
for(int i = 0; i<result.length;i++){
String[]temp = result2[i].split(":");
System.out.println(temp[0]+"="+temp[1]);
}
5. 字符串截取
方法名称 | 类型 | 描述 |
public String substring(int beginIndex) | 普通 | 从指定索引截取到结尾 |
public String substring(int beginIndex, int endIndex) | 普通 | 截取部分内容 |
例子: 字符串截取
String str11 = "helloworld";
System.out.println(str11.substring(5));
System.out.println(str11.substring(0,5));
}
四. 其他操作方法
方法名称 | 类型 | 描述 |
public String trim() | 普通 | 去掉字符串中的左右空格, 保留中间空格 |
public String toUpperCase() | 普通 | 字符串转大写 |
public String toLowerCase() | 普通 | 字符串转小写 |
public native String intern() | 普通 | 字符串入池操作 |
public String concat(String str) | 普通 | 字符串连接, 等同于+, 不入池 |
public int length() | 普通 | 取得字符串长度 |
public boolean isEmpty() | 普通 | 判断是否为空字符串, 但不是null, 而是长度为0 |
trim()方法的使用
String string = "hello world";
System.out.println("["+string+"]");
System.out.println("["+string.trim()+"]");
大小写转换(这个函数只转换字母)
String string1 = "hello&&**#$%world哈哈哈";
System.out.println(string1.toUpperCase());
System.out.println(string1.toLowerCase());
字符串length()
String string1 = "hello&&**#$%world哈哈哈";
System.out.println(string1.length());//20
注意: 数组长度使用数组名称.length属性, 而String中使用的是length()方法
isEmpty()方法
System.out.println("hello".isEmpty());
System.out.println("".isEmpty());
System.out.println(new String().isEmpty());
首字母大写
public static void main(String[] args) {
System.out.println(fistUpper("compute"));
System.out.println(fistUpper(""));
System.out.println(fistUpper("a"));
}
public static String fistUpper(String str){
if("".equals(str)||str==null){
return str;
}
if(str.length()>1){
return str.substring(0,1).toUpperCase()+str.substring(1);
}
return str.toUpperCase();
}