String类中的常用方法(Java)

方法摘要 
1.char charAt(int index)  返回指定索引处的 char 值。
 索引范围为从 0length() - 1。序列的第一个 char 值位于索引 0 处,第二个位于索引 1 处,依此类推,这类似于数组索引。 

参数:
index - char 值的索引。
返回:
此字符串指定索引处的 char 值。第一个 char 值位于索引 0 处。
抛出:
IndexOutOfBoundsException - 如果 index 参数为负或小于此字符串的长度。

eg:

    String str="abc";
    System.out.println(str.charAt(1));

2.int compareTo(String anotherString)   按字典顺序比较两个字符串。

 

参数:
anotherString - 要比较的 String
返回:
如果参数字符串等于此字符串,则返回值 0;如果此字符串按字典顺序小于字符串参数,则返回一个小于 0 的值;如果此字符串按字典顺序大于字符串参数,则返回一个大于 0 的值。

eg:

        String insert="banana";
        int index=after.length-1;
       //查找插入位置
        for(int i=0;i<after.length-1;i++){
        if(after[i].compareTo(insert)>0){
        index=i;
        break;
      }
  }

3.concat(String str)   将指定字符串连接到此字符串的结尾。

如果参数字符串的长度为 0,则返回此 String 对象。否则,创建一个新的 String 对象,用来表示由此 String 对象表示的字符序列和参数字符串表示的字符序列连接而成的字符序列。

示例:

 "cares".concat("s") returns "caress"
 "to".concat("get").concat("her") returns "together"
参数:str - 连接到此 String 结尾的 String
返回:一个字符串,它表示在此对象字符后连接字符串参数字符而成的字符。

eg:

         String str="abc";
     str=str.concat("cde");//字符串引用,必须重新赋值给字符串
     System.out.println(str);

4.boolean endsWith(String suffix)    

参数:
suffix - 后缀。
返回:
如果参数表示的字符序列是此对象表示的字符序列的后缀,则返回 true;否则返回 false。注意,如果参数是空字符串,或者等于此 String 对象(用 equals(Object) 方法确定),则结果为 true

eg:

        //png
        String file="header.jpg";
        if(file.endsWith(".png")){
        System.out.println("上传成功!");
        }else{
       System.out.println("后缀名必须为png!");
       }

5.int indexOf(int ch)   返回指定字符在此字符串中第一次出现处的索引。  

参数:ch - 一个字符(Unicode 代码点)。
返回:在此对象表示的字符序列中第一次出现该字符的索引;如果未出现该字符,则返回 -1

eg:  

/**
* 4. 输入一个字符串,然后在该字符串中查找一个子字符串,
* 并把子字符串第一次出现的位置显示出来
*/
public void string(){
Scanner input=new Scanner(System.in);
System.out.println("请输入一个字符串:");

String str=input.next();
System.out.println("请输入一个字符:");
String chr=input.next();
for(int i=0;i<str.length();i++){
if(chr.equals(str.substring(i, i+1))){
System.out.println(str.indexOf(chr));
}else{
System.out.println("字符串中不包括该子字符");
   }
  }
}

 

6.String[] split(String regex) 根据给定正则表达式的匹配拆分此字符串。

参数:regex - 定界正则表达式

返回:字符串数组,它是根据给定正则表达式的匹配拆分此字符串确定的

抛出:PatternSyntaxException - 如果正则表达式的语法无效

eg:

/**
* 从键盘上输入一个姓名,分别输出姓和名,假定姓都是单姓没有复姓
*/
public void input(){
Scanner input=new Scanner(System.in);
System.out.println("请输入您的姓名:");
String name=input.next();
System.out.println("姓:"+name.charAt(0));
System.out.println("名:"+name.substring(1));
//String str1=name.substring(0,1);
//String str2=name.substring(1);
//String[] split=name.split(",");
/*for(String string:split){
System.out.println("姓名为:"+string);
System.out.println("姓:"+str1);
System.out.println("名:"+str2);
}*/
}

7.int length()  返回此字符串的长度。 

 

指定者:接口 CharSequence 中的 length

 

返回:此对象表示的字符序列的长度。

 eg:  

/**
*从键盘上输入密码,验证密码长度必须在6-10位之间
* @return
*/
public boolean password(){
Scanner input=new Scanner(System.in);
System.out.println("请输入密码:");
password=input.next();
if(password.length()>=6&&password.length()<=10){
System.out.println("密码输入正确!");
return true;
}
return false;
}

8.String substring(int beginIndex)    返回一个新的字符串,它是此字符串的一个子字符串。
它是此字符串的一个子字符串。该子字符串从指定索引处的字符开始,直到此字符串末尾。

示例:

 

 "unhappy".substring(2) returns "happy"
 "Harbison".substring(3) returns "bison"
 "emptiness".substring(9) returns "" (an empty string)

 

 

参数:beginIndex - 起始索引(包括)。
返回:指定的子字符串。
抛出:IndexOutOfBoundsException - 如果 beginIndex 为负或大于此 String 对象的长度。

eg:

/**
* 输入一个字符串,统计该字符串中某个字符出现的次数,并输出该字符在字符串中出现的索引
我爱你我的祖国
*/
public void index(){
Scanner input=new Scanner(System.in);
String str="我爱你我的祖国";
System.out.println("请输入一个字符:");
char chr=input.next().charAt(0);
int count=0;
for(int i=0;i<str.length();i++){
//if(chr.equals(str.substring(i, i+1))){
if(str.charAt(i)==chr){
System.out.print(i);
count++;
}
}
System.out.println("\n字符次数:"+count);
}

      从中有什么不足或者错误,请大家纠正我,谢谢!

           
        

 

posted @ 2015-04-04 22:58  安子静  阅读(723)  评论(0编辑  收藏  举报