JavaでのStringクラスの一般的な使い方
Stringクラスの基本的な使い方:
public class String {
public static void main(String arg[]){
System.out.println("------ ---------");
String s01 = new String("hello");
System.out.println("------ ---------");
byte[] byteArray = {
97,98,99};
String s02 = new String(byteArray);
System.out.println(s02);
System.out.println("------ ---------");
char[] charArray1 = {
'a','b','c'};
String s03 = new String(charArray1);
System.out.println(s03);
System.out.println("------ ---------");
char[] charArray2 = {
'a','b','c','d','e'};
//charArray2 2 ,1
String s04 = new String(charArray2,2,1);
System.out.println(s04);
System.out.println("------length ---------");
String s = "abc";
System.out.println(s.length()); //3
System.out.println("------ ---------");
char[] arr=s03.toCharArray();
System.out.println(arr);
for(char ch:arr) {
System.out.println(ch);
}
System.out.println("------ ---------");
byte[] bytes = s02.getBytes();
for(byte b:bytes) {
System.out.println(b);
}
//
String s05 = "hello";
String upperCase = s05.toUpperCase();
//
String s06 = "ABC";
String lowerCase = s06.toLowerCase();
System.out.println("------ "+"---------");
int age= 6;
String s6="He is" + age + "years old.";
//s
System.out.println(s6);
System.out.println("------trim ---------");
//trim ( )
String s7 = " xs ";
String newString = s7.trim();
System.out.println("------ ---------");
String s07 = "asd";
String s08 = "ASD";
//
System.out.println(s07.equals(s08)); //false
// ( )
System.out.println(s07.equalsIgnoreCase(s08)); //true
String s09 = "abcdef";
String s10 = "abc";
// s09 s10
System.out.println(s09.startsWith(s10)); //true
// s09 s10
System.out.println(s09.endsWith(s10)); //false
// s11
String s11 = " ";
System.out.println(s11.isEmpty()); //false
System.out.println("------ ---------");
String s15 = "asdfghj";
//
char ch = s15.charAt(2); //d
String s16 = "zxcvxbnxm";
// , -1
int index = s16.indexOf('v'); //3
index = s16.indexOf(2); // -1
// , -1
index = s16.lastIndexOf('x'); //7
index = s16.lastIndexOf(3); //-1
System.out.println("------ ---------");
String s18 = "zabjkl";
String reS18 = s18.replace('a','v');
System.out.println(reS18); // zvbjkl
String s19 = " ";
String reS19 = s19.replace(" ", " ");
System.out.println(reS19); //
System.out.println("------ ---------");
String s20 = " ";
// 4
s20.substring(4); //
// 2 , 3
s20.substring(2, 4); //
System.out.println("------ ---------");
//
// , 0
// , 0
// , 0
String s21 = "abd";
String s22 = new String("abc");
int result = s21.compareTo(s22);
System.out.println(result);
System.out.println("------ , ---------");
result = s21.compareToIgnoreCase(s22);
System.out.println(result);
System.out.println("------ ---------");
String s23 = "asd-dfg-gjh-jkl";
//
//
String[] splitArray = s23.split("-");
for(String str:splitArray) {
System.out.println(str);
}
System.out.println("------ ---------");
// ,
char[] chs = {
'a','c','n'};
System.out.println(String.valueOf(chs));
System.out.println("-------Java split -------");
String str="good good study, day day up";
String[] strarray=str.split(" ");
for (int i = 0; i < strarray.length; i++)
System.out.println(strarray[i]);
}
}