Stingクラスの基本的な方法
2828 ワード
package demo;
/* 2015-10-7 21:24:44
*
* String P122 《Java 》
*
* : , ,
*/
public class StringDemo {
public static void main(String[] args) {
// 1.
String str1 = "hello"; //
char ch[] = str1.toCharArray(); //
for (int i = 0; i < ch.length; i++) { //
System.out.print(ch[i] + " ");
}
System.out.println(); //
String str2 = new String(ch);
// 1 1 ,2 2
String str3 = new String(ch,1,2);
System.out.println(str2);
System.out.println(str3);
// 2、
// str.charAt() 4 ,
System.out.println(str1.charAt(3)); // l
// 3. byte
byte by[] = str1.getBytes();
System.out.println(by);// [B@659e0bfd
// byte
System.out.println(new String(by));
// byte ,
System.out.println(new String(by,1,3));
// 4.
str1 = "hello fanfan";
System.out.println("\"" + str1 + "\"" + " :" + str1.length()); //12
// 5.
str1 = "abcjdefghijklmn"; // j
int a = str1.indexOf("j"); // , int , int a
int b = str1.indexOf("j",5); // , 6 , int , int b
int c = str1.indexOf("q"); // -1
System.out.println(a);//3
System.out.println(b);//10
System.out.println(c);//-1
// 6.
str1 = " hello ";
System.out.println(str1.trim());// String hello,
// 7.
str1 = "hello world";
System.out.println(str1.substring(3)); //lo world
System.out.println(str1.substring(3, 9)); //lo wor
// 8.
str1 = "hello world";
String str[] = str1.split("l");// l
for (int i = 0; i < str.length; i++) {
System.out.println(str[i]);
// :
// he
// o wor
// d
}
// 9.
String str4 = str1.toUpperCase();
System.out.println(str4); //HELLO WORLD
String str5 = str4.toLowerCase();
System.out.println(str5); //hello world
// 10.
str1 = "**HELLO";
str2 = "HELLO**";
if(str1.startsWith("**")){
System.out.println(" ** "); // **
}
if(str2.endsWith("**")){
System.out.println(" ** "); // **
}
// 11.
str1 = "hello";
str2 = "HELLO";
boolean bn1 = str1.equals(str2); // ,
boolean bn2 = str1.equalsIgnoreCase(str2); // , boolean
System.out.println(bn1); //false
System.out.println(bn2); //true
// 12.
str1 = "hello";
String str6 = str1.replace("l", "X");
System.out.println(str6); //heXXo
}
}