package stringUse;

public class StringUse {

public static void main(String[] args) {

//获取
//indexOf,charAt(i),length(),substring,
String a1 = "hello";
sop(a1.indexOf("o",2)); //4
sop(a1.indexOf("el")); //1
sop(a1.charAt(1)); //e
sop(a1.length()); //5
sop(a1.substring(1,4)); //ell

//判断
sop(a1.isEmpty()); //false
sop(a1.contains("llo")); //true
sop(a1.startsWith("he")); //true
sop(a1.equals("hello")); //true
sop(a1.equalsIgnoreCase("hELLo")); //true

//转换
String string1 = " hello c++ ";
sop(string1.toUpperCase()); // HELLO C++
sop(string1); // hello c++
sop(string1.trim()); //hello c++

//replace
String str1 = "hello,java";
String str2 = str1.replace("java","C++");
sop("str1="+str1); //str1=hello,java
sop("str2="+str2); //str2=hello,C++

//split
String sentence = "aBBBcedBfstBBgk";
String[] arr = sentence.split("B");
sop("arr.length="+arr.length); //7
for(int i = 0; i<arr.length; i++){
sop(arr[i]);
}

//trans,字符串和字符数组转换
String strArr = "abcdfeg";
char[] array = strArr.toCharArray();
for(int i = 0; i<array.length; i++){
sop(array[i]);
}

char[] array2 = {‘a‘,‘b‘,‘c‘,‘d‘,‘e‘,‘f‘};
String arrStr = new String(array2, 1, 5);
sop(arrStr); //bcdef

//compareTo
String dang1 = "esun";
String dang2 = "gsuz";
sop(dang1.compareTo(dang2)); //-2

//valueOf
int num = 12345;
String val = String.valueOf(num);
sop(val);
sop(val.length()); //5,判断是几位数

}
public static void sop(Object obj){
System.out.println(obj);
}

}

稿源:微信定制开发www .qixoo.com

04-30 02:06