Java 语言中,String 是一个特殊的类,它有好多方法。我们列出部分方法,如下:
方法定义 | 类型 | 描述 |
---|---|---|
public String(char[] value) | 构造 | 直接将一个字符数组变为字符串 |
public String(char[] value,int offset,int count) | 构造 | 将一个指定范围的字符数组变为字符串 |
public String(byte[] bytes) | 构造 | 将一个 byte 数组全部变为字符串 |
public String(byte[] bytes,int offset, int length) | 构造 | 将指定范围的 byte 数组变为字符串 |
public char[] toCharArray() | 普通 | 将一个字符串变为字符数组 |
public char charAt(int index) | 普通 | 从一个字符串中取出指定位置的字符 |
public byte[] getBytes() | 普通 | 将一个字符串变为 byte 数组 |
public int length() | 普通 | 取得字符串长度 |
public int indexOf(String str) | 普通 | 从头开始查找指定的字符串位置,找不到返回 -1 |
public int indexOf(String str,int fromIndex) | 普通 | 从指定位置开始查找指定的字符串位置 |
public String trim() | 普通 | 清除左、右两端的空格 |
public String substring(int beginIndex) | 普通 | 从指定位置开始,一直到尾进行字符串截取 |
public String substring(int begin,int end) | 指定截取字符串的开始点和结束点 | |
public String[] split(String regex) | 普通 | 按照指定的字符串对字符串进行拆分 |
public String toUpperCase() | 普通 | 将一个字符串全部变为大写字母 |
public String toLowerCase() | 普通 | 将一个字符串全部变为小写字母 |
public boolean startsWith(String prefix) | 普通 | 判断字符串是不是以指定对字符串开始 |
public boolean endWith(String suffix) | 普通 | 判断字符串是不是以指定字符串结尾 |
public boolean equals(String str) | 普通 | 判断两个字符串是不是内容相等 |
public boolean quealsIgnoreCase(String str) | 普通 | 不区分大小写比较两个字符串是否相等 |
public String replaceAll(String regex,String replacement) | 普通 | 字符串替换 |
package com.haicoder.net.str;
public class StringTest {
public static void main(String[] args) {
System.out.println("嗨客网(www.haicoder.net)");
//定义一个字符串,两边有空格
String str = " www.haicoder.net ";
System.out.println("定义定字符串为: " + str);
//将字符串变为字符数组
char c[] = str.toCharArray();
for (int i = 0; i < c.length; i++) {
System.out.print(c[i] + "\t"); //这边打印输出定时候,不是换行
}
System.out.println("");
//将字符数组转换为String
String str1 = new String(c);
System.out.println("str1 : " + str1);
//固定截取字符数组变成字符串
String str3 = new String(c, 0, 3);
System.out.println("str3 : " + str3);
//取出 str 字符串里面定第 5 个字符
char charAt = str.charAt(5);
System.out.println("str charAt : " + charAt);
//将字符串变为 byte 数组
byte b[] = str.getBytes();
System.out.println(new String(b));
//输出数组中起始位置是 1 长度为 4 的字符串
System.out.println(new String(b, 1, 4));
//计算字符串长度
int strLength = str.length();
System.out.println("str 字符串长度 : " + strLength);
//全字符串查找,从 0 位置开始
int startIndex = str.indexOf("h");
//从某个位置开始往后找
int startIndex2 = str.indexOf("w", 3);
//字符串不存在
int startIndex3 = str.indexOf("p");
System.out.println("startIndex : " + startIndex);
System.out.println("startIndex2 : " + startIndex2);
System.out.println("startIndex3 : " + startIndex3);
//字符串两边的空格被清除掉
String strTrim = str.trim();
System.out.println("string trim : " + strTrim);
//字符串从第 4 位开始截取
String subStr1 = str.substring(4);
//字符串截图 0 ~ 4 个位置的内容
String subStr2 = str.substring(0, 4);
System.out.println("subStr1 : " + subStr1);
System.out.println("subStr2 : " + subStr2);
//按照 . 来拆分字符串
String s[] = str.split("\\.");
for (int i = 0; i < s.length; i++) {
System.out.println("string split : " + s[i]);
}
//字符串变大写
String strUpper = str.toUpperCase();
//字符串变小写
String strLower = str.toLowerCase();
System.out.println("upper : " + strUpper);
System.out.println("lower : " + strLower);
//判断字符串以 " " 开始
System.out.println(str.startsWith(" "));
//判断字符串以 " " 结尾
System.out.println(str.endsWith(" "));
//字符串不区分大小写比较
String compare1 = "haicoder";
String compare2 = "HAICODER";
System.out.println("equals : " + compare1.equals(compare2));
System.out.println("equalsIgnoreCase : " + compare1.equalsIgnoreCase(compare2));
//字符串替换
String replaceStr = str.replaceAll("\\."," "); //将 . 替换为 " "
System.out.println("replace " + replaceStr);
}
}
运行结果如下:
在实际的编程过程中,我们经常会对 String 字符串做一些特殊处理,所以我们要记得上面的方法,在实际编程过程中熟练的使用。