Java中String类的常用方法示例
时间:2010-10-16 来源:fragrancloud
去除字符串头尾的空格
示例如下:
1 package dg;
2
3
4 /**
5 *
6 * @author DUGANG
7 */
8 public class StringMethod {
9 public static void main(String[] args){
10 String str = "Hello World!!!";
11 System.out.println("示例字符串为:" + str);
12 //public char charAt(int index)方法
13 System.out.println(str + "中第四个字符为:" + str.charAt(4));
14 //public int length()方法
15 System.out.println(str + "的字符长度为:" + str.length());
16 //public int indexOf(String str)方法
17 System.out.println(str + "中第一个“l”出现在字符串的第" + str.indexOf("l") + "位");
18 //public boolean equalsIgnoreCase(String another)方法
19 System.out.println(str + "与hello world!!!相同:" + str.equalsIgnoreCase("hello world!!!"));
20 //public String replace(char oldChar, char newChar)方法
21 System.out.println(str + "用‘L’替换‘l’后为:" + str.replace("l", "L"));
22 //public boolean startsWith(String prefix)方法
23 System.out.println(str + "以字母‘H’开头:" + str.startsWith("H"));
24 //public boolean endsWith(String suffix)方法
25 System.out.println(str + "以‘!’结尾:" + str.endsWith("!"));
26 //public String toUpperCase()方法
27 System.out.println(str + "转换为大写:" + str.toUpperCase());
28 //public String toLowerCase()fangfa
29 System.out.println("JAVA转换为小写:" + "JAVA".toLowerCase());
30 //public String substring(int beginIndex)
31 System.out.println(str + "从第六个字符开始截取子串为:" + str.substring(6));
32 //public String trim()方法
33 System.out.println(" hello " + "去掉开头和结尾的空格为:" + " hello ".trim());
34 }
35
36 }
package dg; /** * * @author DUGANG */ public class StringMethod { public static void main(String[] args){ String str = "Hello World!!!"; System.out.println("示例字符串为:" + str); //public char charAt(int index)方法 System.out.println(str + "中第四个字符为:" + str.charAt(4)); //public int length()方法 System.out.println(str + "的字符长度为:" + str.length()); //public int indexOf(String str)方法 System.out.println(str + "中第一个“l”出现在字符串的第" + str.indexOf("l") + "位"); //public boolean equalsIgnoreCase(String another)方法 System.out.println(str + "与hello world!!!相同:" + str.equalsIgnoreCase("hello world!!!")); //public String replace(char oldChar, char newChar)方法 System.out.println(str + "用‘L’替换‘l’后为:" + str.replace("l", "L")); //public boolean startsWith(String prefix)方法 System.out.println(str + "以字母‘H’开头:" + str.startsWith("H")); //public boolean endsWith(String suffix)方法 System.out.println(str + "以‘!’结尾:" + str.endsWith("!")); //public String toUpperCase()方法 System.out.println(str + "转换为大写:" + str.toUpperCase()); //public String toLowerCase()fangfa System.out.println("JAVA转换为小写:" + "JAVA".toLowerCase()); //public String substring(int beginIndex) System.out.println(str + "从第六个字符开始截取子串为:" + str.substring(6)); //public String trim()方法 System.out.println(" hello " + "去掉开头和结尾的空格为:" + " hello ".trim()); } }
结果:
示例字符串为:Hello World!!!
Hello World!!!中第四个字符为:o
Hello World!!!的字符长度为:14
Hello World!!!中第一个“l”出现在字符串的第2位
Hello World!!!与hello world!!!相同:true
Hello World!!!用‘L’替换‘l’后为:HeLLo WorLd!!!
Hello World!!!以字母‘H’开头:true
Hello World!!!以‘!’结尾:true
Hello World!!!转换为大写:HELLO WORLD!!!
JAVA转换为小写:java
Hello World!!!从第六个字符开始截取子串为:World!!!
hello 去掉开头和结尾的空格为:hello
相关阅读 更多 +