JAVA String类的常用方法

1  String类构造方法
public String()     无参构造方法,用来创建空字符串的String对象。
           String str = new String();
public String(String value)     用已知的字符串value创建一个String对象。
           String str1 = new String( "abcd" );
           String str2 = new String( str1 ); //abcd
public String(char[] value)     用字符数组value创建一个String对象。
           char text [] = { 'a' , 'b' , 'c' , 'd' };
           String str = new String( text ); //abcd
public String(char value[], int offset , int count)     用字符数组chars的offset开始的count个字符创建一个String对象。
            char text [] = { 'a' , 'b' , 'c' , 'd' };
           String str = new String( text , 1, 2); //bc
public String(byte[] values)     用比特数组values创建一个String对象。
            byte [] strb = new   byte [] { 65, 66 };
           String str = new String( strb ); //AB
2  .length() 字符串的长度       public int length()
            char text [] = { 'A' , 'B' , 'C' };
            String string = new String ( text );
             int S = string .length(); //3
3 .charAt() 截取一个字符       public char charAt( int index )
            char text [] = { 'A' , 'B' , 'C' };
           String string = new String( text );
            char S = string . charAt(1); //B
4 .getCharts()截取多个字符       将当前字符串从srcBegin开始到srcEnd-1位置上的字符复制到字符组dst中,并从dstBegin处开始存放。
   public void getChars( int srcBegin , int srcEnd , char dst [], int dstBegin )
           String string = "It is Sunday today" ;
            char str [] = new char [10];
            string .getChars(2, 8, str , 0); // is Su
5  .getBytes()   使用指定的字符集将字符串编码为 byte 序列,并将结果存储到一个新的 byte 数组中
    public byte [] getBytes(String charsetName )   charsetName  -- 支持的字符集名称
            String string = "It is Sunday today" ;
            byte str [] = string .getBytes( "UTF-8" ); //[B@2a139a55
6  .compareTo() 字符串与对象比较;两个字符串按字典顺序比较   public int compareTo(String anotherString )
           String str1 = "string" ;
           String str2 = "sTring" ;
           int result = str1 .compareTo( str2 ); //32
  . compareToIgnoreCase()   按字典顺序比较两个字符串(忽略大小写)      public int compareToIgnoreCase(String str )
           String str1 = "sTring" ;
           String str2 = "string" ;
           int result = str1 .compareToIgnoreCase( str2 ); //0
  .equals() 将字符串与指定的对象比较   public boolean equals(Object anObject )
           String str1 = "string" ;
           String str2 = "string" ;
            boolean result = str1 .equals( str2 ); //true
7  .indexOf()  指定字符在字符串第一次出现处的索引

      public int indexOf(int ch): 返回指定字符在字符串中第一次出现处的索引,如果此字符串中没有这样的字符,则返回 -1。

      public int indexOf(int ch, int fromIndex): 返回指定字符在字符串中第一次出现处的索引,如果此字符串中没有这样的字符,则返回 -1。

      int indexOf(String str): 返回指定字符在字符串中第一次出现处的索引,如果此字符串中没有这样的字符,则返回 -1。

      int indexOf(String str, int fromIndex): 返回指定字符在字符串中第一次出现处的索引,如果此字符串中没有这样的字符,则返回 -1。
           String str1 = "string" ;
           int result = str1 .indexOf( "tr" ,1); //1
  . lastIndexOf()  指定字符串在字符串最后一次出现的索引
     public int lastIndexOf(int ch): 返回指定字符在此字符串中最后一次出现处的索引,如果此字符串中没有这样的字符,则返回 -1。

     public int lastIndexOf(int ch, int fromIndex): 返返回指定字符在此字符串中最后一次出现处的索引,如果此字符串中没有这样的字符,则返回 -1。

     public int lastIndexOf(String str): 返回指定字符在此字符串中最后一次出现处的索引,如果此字符串中没有这样的字符,则返回 -1。

     public int lastIndexOf(String str, int fromIndex): 返回指定字符在此字符串中最后一次出现处的索引,如果字符串中没有这样的字符,则返回 -1。
           String str1 = "string" ;
           int result = str1 . lastIndexOf( "tr" ,1); //1
 8  .replace()    用 replacement字符替换字符串中出现的所有 target 字符,并返回替换后的新字符串
   public String replace(CharSequence target , CharSequence replacement )
           String str1 = "string" ;
           String result = str1 . replace( "t" , "T" ); //sTring
 .replaceAll() 用给定的参数 replacement 替换字符串所有匹配给定的正则表达式的子字符串。
    public String replaceAll(String regex , String replacement )
           String str1 = "string" ;
           String result = str1 .replaceAll( "tr" , "TR" ); //sTRing
 .replaceFirst()  用给定的参数 replacement 替换字符串第一个匹配给定的正则表达式的子字符串。
   public String replaceFirst(String regex , String replacement )
           String str1 = "sttring" ;
           String result = str1 .replaceFirst( "t" , "T" ); //sTtring
9  .substring()   方法返回字符串的子字符串      public String substring( int beginIndex , int endIndex )
           String string = new String( "abcdef" );
           String str = string .substring(2); //cdef
10 . split() 方法根据匹配给定的正则表达式来拆分字符串    
    public String[] split(String regex )
    public String[] split (String regex , int limit )
         注意: . 、 | 和 * 等转义字符,必须得加 \\
         注意:多个分隔符,可以用 | 作为连字符
         limit --分割的份数
     String str = new String( "ab-cd-ef" );
            for (String retval : str .split( "-" , 3)) {
                System. out .println( retval ); //ab <br> cd <br>ef
           }
11  .toLowerCase()  将字符串转换为小写    public String toLowerCase ()
           String str1 = "sTring" ;
           String result = str1 . toLowerCase();
       . toUpperCase()  将字符串转换为大写     public String toUpperCase()
           String str1 = "sTring" ;
           String result = str1 . toUpperCase();
12  .trim()   用于删除字符串的头尾空白符    public String trim ()
       String str = new String( " ab cd " );
       String relult = str .trim(); //ab cd
13  字符串与基本类型转换
     字符串转换为基本类型
public static byte parseByte(String s)
public static short parseShort(String s)
public static short parseInt(String s)
public static long parseLong(String s)
public static float parseFloat(String s)
public static double parseDouble(String s)
           String s = "12" ;
            int Str = Integer. parseInt( s );
           System. out .println( Str ); //12

      基本类型转换为字符串类型
static String valueOf(char data[])
static String valueOf(char data[], int offset, int count)
static String valueOf(boolean b)
static String valueOf(char c)
static String valueOf(int i)
static String valueOf(long l)
static String valueOf(float f)
static String valueOf(double d)
            int s = 12;
           String Str = String.valueOf( s );
           System. out .println( Str ); //12
进制转换
Long.toBinaryString(long l)
Long.toOctalString(long l)
Long.toHexString(long l)
Long.toString(long l, int p)//p作为任意进制
            int s = 12;
           String Str = Integer. toBinaryString( s ); //1100














































评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值