云沙

博客园 首页 联系 订阅 管理

String的常用方法:
1. public char charAt(int index)
  返回字符串中的index个字符
2. public int length()
  返回字符串长度
3. public int indexOf(String str)
  返回字符串中出现str的第一个位置
4. public boolean equalsIgnoreCase(String another)
  比较字符串与another是否相同(忽略大小写)
5. public String replace(char oldChar, char newChar)
  在字符串中用newChar替换oldChar
6. public boolean startsWith(String prefix)
  判断字符串是否以prefix开头
7. public boolean endsWith(String suffix)
  判断字符串是否以suffix结束
8. public String toUpperCase()
  返回该字符串的大写形式
9. public String toLowerCase()
  返回该字符串的小写形式
10. public String substring(int beginIndex)
  返回该字符串从beginIndex开始到结尾的子串
11. public String substring(int beginIndex,int endIndex)
  返回该字符串从beginIndex开始到endIndex结尾的子串
13. public String trim()

  去除字符串头尾的空格

 

 

示例如下:

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

 

 

posted on 2010-10-16 14:54  fragrancloud  阅读(1783)  评论(0编辑  收藏  举报