String 类的常用方法都有哪些?

a.indexOf():返回指定字符的索引。

public class Main {
    public static void main(String args[]) {
        String string = "aaa456ac";  
        //查找指定字符是在字符串中的下标。在则返回所在字符串下标;不在则返回-1.  
        System.out.println(string.indexOf("b")); // indexOf(String str); 返回结果:-1,"b"不存在  
 
        // 从第四个字符位置开始往后继续查找,包含当前位置  
        System.out.println(string.indexOf("a",3));//indexOf(String str, int fromIndex); 返回结果:6  
 
        //(与之前的差别:上面的参数是 String 类型,下面的参数是 int 类型)参考数据:a-97,b-98,c-99  
 
        // 从头开始查找是否存在指定的字符  
        System.out.println(string.indexOf(99));//indexOf(int ch);返回结果:7  
        System.out.println(string.indexOf('c'));//indexOf(int ch);返回结果:7  
 
        //从fromIndex查找ch,这个是字符型变量,不是字符串。字符a对应的数字就是97。  
        System.out.println(string.indexOf(97,3));//indexOf(int ch, int fromIndex); 返回结果:6  
        System.out.println(string.indexOf('a',3));//indexOf(int ch, int fromIndex); 返回结果:6  
    }
}

b、charAt():返回指定索引处的字符。

public class Test {
    public static void main(String args[]) {
        String s = "www.runoob.com";
        char result = s.charAt(6);
        System.out.println(result);//执行结果为n
    }
}

c、replace():字符串替换。

public class Main {
    public static void main(String args[]) {
        String Str = new String("Runoob");

        System.out.print("返回值 :" );
        System.out.println(Str.replace('o', 'T'));//执行结果为RunTTb

        System.out.print("返回值 :" );
        System.out.println(Str.replace('u', 'D'));//执行结果为RDnoob
    }
}

d、trim():去除字符串两端空白。

public class Test {
    public static void main(String args[]) {
        String Str = new String("    www.runoob.com    ");
        System.out.print("原始值 :" );
        System.out.println( Str );

        System.out.print("删除头尾空白 :" );
        System.out.println( Str.trim() );
    }
}

e、split():分割字符串,返回一个分割后的字符串数组。

public class Test {
    public static void main(String args[]) {
        String str = new String("Welcome-to-Runoob");
 
        System.out.println("- 分隔符返回值 :" );
        for (String retval: str.split("-")){
            System.out.println(retval);
        }
 
        System.out.println("");
        System.out.println("- 分隔符设置分割份数返回值 :" );
        for (String retval: str.split("-", 2)){
            System.out.println(retval);
        }
 
        System.out.println("");
        String str2 = new String("www.runoob.com");
        System.out.println("转义字符返回值 :" );
        for (String retval: str2.split("\\.", 3)){
            System.out.println(retval);
        }
 
        System.out.println("");
        String str3 = new String("acount=? and uu =? or n=?");
        System.out.println("多个分隔符返回值 :" );
        for (String retval: str3.split("and|or")){
            System.out.println(retval);
        }
    }
}

f、getBytes():返回字符串的 byte 类型数组。

import java.io.*;
 
public class Test {
    public static void main(String args[]) {
        String Str1 = new String("runoob");
 
        try{
            byte[] Str2 = Str1.getBytes();
            System.out.println("返回值:" + Str2 );
            
            Str2 = Str1.getBytes( "UTF-8" );
            System.out.println("返回值:" + Str2 );
            
            Str2 = Str1.getBytes( "ISO-8859-1" );
            System.out.println("返回值:" + Str2 );
        } catch ( UnsupportedEncodingException e){
            System.out.println("不支持的字符集");
        }
    }
}

g、length():返回字符串长度。

h、toLowerCase():将字符串转成小写字母。

public class Test {
    public static void main(String args[]) {
        String Str = new String("WWW.RUNOOB.COM");

        System.out.print("返回值 :" );
        System.out.println( Str.toLowerCase() );//返回值 :www.runoob.com
    }
}

i、toUpperCase():将字符串转成大写字符。

public class Test {
    public static void main(String args[]) {
        String Str = new String("www.runoob.com");

        System.out.print("返回值 :" );
        System.out.println( Str.toUpperCase() );//返回值 :WWW.RUNOOB.COM
    }
}

g、substring():截取字符串。

public class RunoobTest {
    public static void main(String args[]) {
        String Str = new String("This is text");
 
        System.out.print("返回值 :" );
        System.out.println(Str.substring(4) );//返回值 : is text
 
        System.out.print("返回值 :" );
        System.out.println(Str.substring(4, 10) );//返回值 : is te
    }
}

k、equals():字符串比较

public class Test {
    public static void main(String args[]) {
        String Str1 = new String("runoob");
        String Str2 = Str1;
        String Str3 = new String("runoob");
        boolean retVal;

        retVal = Str1.equals( Str2 );
        System.out.println("返回值 = " + retVal );//返回值 = true

        retVal = Str1.equals( Str3 );
        System.out.println("返回值 = " + retVal );//返回值 = true
    }
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值