1.字符与字符串
方法名 | 描述 |
---|---|
public String ( char value[ ] ) | 将字符数组的所有内容变为字符串 |
public String ( char value[ ] ,int offset,int count ) | 将字符数组的部分内容变为字符串(offset:偏移量;count:数量) |
public char charAt( int index) | 获取指定索引位置的字符 |
public char[] toCharArray( ) | 将字符串转为字符数组返回 |
代码示例:
String str = "helloworld" ;
// 将字符串变为字符数组
char[] data = str.toCharArray() ;
for (int i = 0; i < data.length; i++) {
System.out.print(data[i]+" ");
}
// 字符数组转为字符串
System.out.println(new String(data)); // 全部转换
System.out.println(new String(data,5,5)); // 部分转换
2.字节与字符串
方法名 | 描述 |
---|---|
public byte[] getBytes( ) | 将字符串以字节数组的形式返回 |
代码示例:
String str = "helloworld" ;
// String 转 byte[]
byte[] data = str.getBytes() ;
for (int i = 0; i < data.length; i++) {
System.out.print(data[i]+" ");
}
// byte[] 转 String
System.out.println(new String(data));
3.字符串比较
方法名 | 描述 |
---|---|
public boolean equals( Object anyObject ) | 区分大小写的比较 |
public boolean equalsIIgnoreCase( String str) | 不区分大小写的比较 |
public int compareTo( String str) | 比较两个字符串的大小关系 |
-
compareTo()是一个可以区分大小关系的方法,是String方法里是一个非常重要的方法。该数据会根据大小关系返回三类内容:1>. 相等:返回0;2>. 小于:返回内容小于0;3>. 大于:返回内容大于0。
-
字符串的比较大小规则, 总结成三个字 “字典序” 相当于判定两个字符串在一本词典的前面还是后面. 先比较第一个字符的大小(根据 unicode 的值来判定), 如果不分胜负, 就依次比较后面的内容.
代码示例:
String str1 = "hello" ;
String str2 = "Hello" ;
System.out.println(str1.equals(str2)); // false
System.out.println(str1.equalsIgnoreCase(str2)); // true
System.out.println("A".compareTo("a")); // -32
System.out.println("a".compareTo("A")); // 32
System.out.println("A".compareTo("A")); // 0
4.字符串查找
方法名 | 描述 |
---|---|
public boolean contians( String str ) | 判断一个子字符串是否存在 |
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 str ) | 判断是否以指定字符串开头 |
public boolean endsWith( String str ) | 判断是否以指定字符串结尾 |
代码示例:
String str = "helloworld" ;
System.out.println(str.contains("world")); // true
String str = "helloworld" ;
System.out.println(str.indexOf("world")); // 5,w开始的索引
System.out.println(str.indexOf("bit")); // -1,没有查到
if (str.indexOf("hello") != -1) {
System.out.println("可以查到指定字符串!");
}
String str = "**@@helloworld!!" ;
System.out.println(str.startsWith("**")); // true
System.out.println(str.startsWith("@@",2)); // ture
System.out.println(str.endsWith("!!")); // true
5.字符串替换
方法名 | 描述 |
---|---|
public String replaceAll( String regex,String replacement) | 替换所有指定的内容 |
public String replace( String oldstr,String newstr) | 将老字符串替换新字符串 |
public String replaceFirst( String regex,String replacement) | 替换首个内容 |
代码示例:
String str = "helloworld" ;
System.out.println(str.replaceAll("l", "_"));
System.out.println(str.replaceFirst("l", "_"));
String str1=str.replace("l","*");
System.out.println(str1);//str不改变
【注意事项】: 由于字符串是不可变对象, 替换不修改当前字符串, 而是产生一个新的字符串.
6.字符串拆分
方法名 | 描述 |
---|---|
public String[] split( String regex) | 按照regex字符串将原字符串拆分 |
public String[] split( String regex,int limit) | 按照regex字符串将原字符串拆分为limit组 |
代码示例:
String str = "hello world hello bit" ;
String[] result = str.split(" ") ; // 按照空格拆分
for(String s: result) {
System.out.println(s);
}
// 按照空格拆分,并分为两组
String str = "hello world hello bit" ;
String[] result = str.split(" ",2) ;
for(String s: result) {
System.out.println(s);
}
拆分是特别常用的操作. 一定要重点掌握. 另外有些特殊字符作为分割符可能无法正确切分, 需要加上转义.
代码示例: 拆分IP地址
String str = "192.168.1.1" ;
String[] result = str.split("\\.") ;
for(String s: result) {
System.out.println(s);
}
注意事项:
- 字符"|","*","+“都得加上转义字符,前面加上”".
- 而如果是"",那么就得写成"\".
- 如果一个字符串中有多个分隔符,可以用"|"作为连字符.
7.字符串截取
方法名 | 描述 |
---|---|
public String[] substring( int beginIndex) | 从指定索引截取到结尾 |
public String[] substring( int beginIndex,int endIndex) | 截取一定区间的内容(左闭右开) |
代码示例:
String str = "helloworld" ;
System.out.println(str.substring(5));
System.out.println(str.substring(0, 5));
注意事项:
- 索引从0开始
- 注意前闭后开区间的写法, substring(0, 5) 表示包含 0 号下标的字符, 不包含 5 号下标
8.字符串其他操作
方法名 | 描述 |
---|---|
public String trim( ) | 去掉字符串左右两边的空格,保留中间空格 |
public String toUpperCase( ) | 字符串转大写 |
public String toLowerCase( ) | 字符串转小写 |
public native String intern( ) | 将字符串放入常量池操作 |
public String concat( String str) | 字符串拼接,等同于“+”,不入池 |
public boolean isEmpty( ) | 判断字符串是否为空串,但不是null,而是长度为0 |
public int length( ) | 获取字符串长度 |
代码示例:
//trim()方法的使用:trim 会去掉字符串开头和结尾的空白字符(空格, 换行, 制表符等).
String str = " hello world " ;
System.out.println("["+str+"]");
System.out.println("["+str.trim()+"]");
//大小写转换
String str = " hello%$$%@#$%world 哈哈哈 " ;
System.out.println(str.toUpperCase());
System.out.println(str.toLowerCase());
//字符串length()
String str = " hello%$$%@#$%world 哈哈哈 " ;
System.out.println(str.length());
//isEmpty()方法:
System.out.println("hello".isEmpty());
System.out.println("".isEmpty());
System.out.println(new String().isEmpty());
String类并没有提供首字母大写操作,需要自己实现.
代码示例: 首字母大写
public static void main(String[] args) {
System.out.println(fistUpper("yuisama"));
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() ;
}