String s = “HelloWorld”;
String s1 = “HelloWorld”;
System.out.println(s.equals(s1)); // true
System.out.println(s.equalsIgnoreCase(“helloworld”));//不区分大小写equals
System.out.println(s.contentEquals(“HelloWorld”));
System.out.println(s.indexOf(“H”));
System.out.println(s.indexOf(“o”,5));// return "o"字符的index 从index5开始
System.out.println(s.substring(2)); // truncate从index 2 到结尾
System.out.println(s.charAt(3)); // return index 3的字符
System.out.println(s.substring(1, 5)); // truncate从index 1到5
遍历字符串统计大小写及数字个数
public class StringTest3 {
public static void main(String[] args) {
String s = “HHHello1234wOrlD”;
int bigcount = 0;
int smallcount = 0;
int count = 0;
for (int i = 0; i < s.length(); i++) {
char ch = s.charAt(i);
if (ch <= ‘z’ && ch >= ‘a’) {
smallcount++;
}else if (ch >= ‘A’ && ch <= ‘Z’) {
bigcount++;
}else if (ch >= ‘0’ && ch <= ‘9’) {
count++;
}
}
System.out.println(“bigcount:” + bigcount + " smallcount:" + smallcount + " count:" + count);
System.out.println();
}
}
String类的转换功能
byte[] getBytes() // 把字符串转换成字节数组 String s = “abc”; byte[] b = s.getBytes()
char[] toCharArray() // 把字符串转换成字符数组 String s = “abc”; char[] b = s.toCharArray();
static String valueOf(char[] chs) // 把字符数组转换成字符串 String.valueOf(new char[]{‘a’,‘b’,‘c’});
static String valueOF(int i) // 把int数据类型转换成字符串 String.valueOf(123);
String toLowerCase() // 字符串变小写 String s = “ABCD”.toLowerCase();
String toUpperCase()// 字符串变大写 String s = “abc”.toUpperCase();
String concat(String str)// String s = “abc”.concat(“def”);
String s = “JavaSE”;
char[] c = s.toCharArray(); // 字符串转换成字符数组
for(int i = 0;i<c.length;i++) {
System.out.println(c[i]);
}
System.out.println("-------------");
byte[] b = s.getBytes(); // 字符串转换成字节数组
for(int i = 0;i<b.length;i++) {
System.out.println(b[i]);
}
System.out.println("-------------");
s = String.valueOf(123456); //int数据转换成字符串
System.out.println(s);
System.out.println("-------------");
s = String.valueOf(new char[] {‘a’,‘b’,‘c’}); // 字符数组转换成字符串
System.out.println(s);
System.out.println(“JavaSE”.toUpperCase());//字符串转大写
System.out.println(“JavaSE”.toLowerCase());//字符串转小写
System.out.println(“Java”.concat(“SE”));//拼接
String类的其他功能
替换功能
String replace(char old,char new) // String s = “abc”; s.replace(b,c); 结果"acc"
String replace(String old,String new) // String s = “abcd”; s.replace(“ab”,“cd”); 结果"cdcd"
去除字符串两空格
String trim()// String s = " abc “; s.trim(); 结果"abc”
按字典顺序比较两个字符串
int compareTo(String str)// “abc”.compareTo(“abc”); 全相同返回0,不同比第一个ascii码相减的值"abc".compareTo(“bcd”);结果是-1,前面一样长度不同返回length-length
int compareToIgnoreCase(String str)// 同上不区分大小写
把数组中的数据按照指定格式拼接成一个字符串
int[] arr = {1,2,3,4,5,6,7};
输出结果:
[1, 2, 3, 4, 5, 6, 7]
例:
public class StringTest7 {
public static void main(String[] args) {
int[] arr = {1,2,3,4,5,6,7};
System.out.println(arrayToString(arr));
}
public static String arrayToString(int[] arr) {
String s = "";
s += "[";
for (int i = 0; i < arr.length; i++) {
if(i == arr.length-1) {
s += arr[i];
s += "]";
}else {
s += arr[i];
s += ", ";
}
}
return s;
}
}
字符串翻转案例
public class StringTest8 {
public static void main(String[] args) {
String s = “abcdefg”;
String res = myReverse(s);
System.out.println(res);
}
public static String myReverse(String s) {
char[] ch = s.toCharArray();
String res = "";
for (int i = ch.length-1; i >= 0 ; i--) {
res += ch[i];
}
return res;
}
}