Java的String类中常用方法举例

1. 字符串与字符数组.

一个字符串可以转化为字符数组,同样的是,字符数组也可以转化为字符串,而在String类中提供了以下的方法:

(1)将字符串转化为字符数组:

public char[] toCharArray();
举例:
public class StringApi {
 public static void main(String[] args) {
     String str = "nishuibaichuan";
     char[] c = str.toCharArray();
     for (int i = 0; i < c.length; i++) {
        System.out.print(c[i]+"\t");
      }
   }
}
运行结果:
n i s h u i b a i c h u a n 

(2)将字符数组转化为字符串
public String(char[] value);//全部转换为字符串
public String(char[] value,int offset,int count);//部分转化

public class StringApi1 {
     public static void main(String[] args) {
        char[] c = {'n','i','s','h','u','i', 'b','a','i','c','h','u','a','n' };
        String str1 = new String(c);//将全部字符数组转化为字符串
        String str2 = new String(c,0,3);//将部分字符数组转化为字符串
        System.out.println(str1);
        System.out.println(str2);
   }
}
运行结果:
nishuibaichuan
nis
2. 从字符串中取出指定位置的字符
public char charAt(int index)
举例:
public class CharAt {
 public static void main(String[] args) {
    String str = "nishuibaichuan";//定义String对象
    System.out.println(str.charAt(3));
 }
}
运行结果:  h
3.字符串和byte数组的转换
byte数组又称为字节数组,一般在IO流的操作中经常使用到.
(1)将字符串变为字节数组:public byte[] getBytes();
举例: public class Bytes {
       public static void main(String[] args) {
           String str = "nishuibaichuan";
           byte[] bytes = str.getBytes();//将字符串转化为字节数组
           for (int i = 0; i < bytes.length; i++) {
                System.out.print(bytes[i]+",");
           }
      } 
}

(2)将字节数组变为字符串数组:public String(byte[] bytes);
                         public String(byte[] bytes,int offset,int lenght);
举例:
public class Bytes {
     public static void main(String[] args) {
          String str = "nishuibaichuan";
          byte[] bytes = str.getBytes();//将字符串转化为字节数组 
          String str1 = new String(bytes);//将全部字节数组转换为字符串
          String str2 = new String(bytes,1,3);//将部分字节数组转换为字符串
          System.out.println(str1);
          System.out.println(str2);
     } 
}
运行结果:
nishuibaichuan
ish

4.取得字符串长度
public int length();
举例:
public class Length {
  public static void main(String[] args) {
    String str = "nishuibaichuan";
    System.out.println(str.length());
  }
}
运行结果: 14

5.查找指定的字符串是否存在.
(1)从头开始查找:public int indexOf(String str);

(2)从指定位置开始查找:public int indexOf(String str,int fromIndex);
查找的时候方法的返回值是一个int类型,此数据表示的是一个字符串的具体数据,如果没有查找到此字符串,则返回"-1".
举例:
public class IndexOf {
    public static void main(String[] args) {
      String str = "nishuibaichuan" ;
      System.out.println(str.indexOf("i"));
      System.out.println(str.indexOf("i",3));
    }
}
运行结果:1  5

6. 去掉空格
public String trim();只能去掉头尾空格,而不能去掉中间的空格,这多用于接受用户传递过来的信息;
举例:
public class Trim {
   public static void main(String[] args) {
      String str = " nishuibaichuan " ;
      System.out.println(str.trim());
   }
}
运行结果:nishuibaichuan

7.字符截取,比较常用
从一个字符串中截取指定的部分字符串内容
(1)public String substring(int beginIndex);返回一个新的字符串,它是此字符串的一个子字符串。该子字符串从指定索引处的字符开始,直到此字符串末尾。
(2)public String substring(int beginIndex,int endIndex)返回一个新字符串,它是此字符串的一个子字符串。该子字符串从指定的 beginIndex 处开始,直到索引 endIndex - 1 处的字符。因此,该子字符串的长度为 endIndex-beginIndex。
举例:
public class SubString {
    public static void main(String[] args) {
       String str = "nishuibaichuan" ;
       System.out.println(str.substring(6));//(下标从0开始的)从第6个开始,到最后一个结束 
       System.out.println(str.substring(6,9));//从第6个开始,到第9个结束,但不含第9个 
    } 
} 
运行结果: 
baichuan 
bai 

8.拆分字符串
如果要把一个字符串按照指定的字符串去拆分的话,就用到:public String[] split(String regex);
举例:
public class Split {
     public static void main(String[] args) {
         String str = "nishui baichuan" ;
         String[] strs = str.split(" ");//俺空格进行字符串的拆分
         for (int i = 0; i < strs.length; i++) {
            System.out.println(strs[i]);
         } 
    }
}

9.大小写转换
(1)小写变大写:public StringString toUpperCase();
(2)大写变小写:public StringString toLowerCase();
public class ToUpperLower {
    public static void main(String[] args) {
        String str1 = "NISHUIBAICHUAN";
        String str2 = "nishuibaichuan";
        System.out.println(str1.toLowerCase());//大写变小写
        System.out.println(str2.toUpperCase());//小写变大写
   }
}

10.判断是否以指定的字符串开头或结尾
(1)测试此字符串是否以指定的字符串开始:public boolean startsWith(String prefix);
(2)测试此字符串是否以指定的字符串结束:public booleanString endsWith(String suffix);
举例:
public class StartEnds {
   public static void main(String[] args) {
      if(str1.startsWith("@@")){
          System.out.println("str1以@@开头");
      }
      if(str2.endsWith("@@")){
          System.out.println("str2以@@结尾");
      }
   }
}
运行结果:
str1以@@开头
str2以@@结尾

11. 不区分大小写的比较
在前面笔者怎提到过equals()的用法,他是比较两个字符串内容是否相等的方法,但是他有一个条件就是比较的字符串必须是大写或者为小写.现在我们就来看看不区分大小写的比较吧:public boolean equalsIgnoreCase(String anotherString):将此 String 与另一个 String 比较,不考虑大小写。如果两个字符串的长度相同,并且其中的相应字符都相等(忽略大小写),则认为这两个字符串是相等的;
举例:
public class Equals {
    public static void main(String[] args) {
       String str1 = "NISHUIBAICHUAN";
       String str2 = "nishuibaichuan";
       System.out.println(str1.equals(str2));//区分大小写
       System.out.println(str1.equalsIgnoreCase(str2));//不区分大小写
    }
}
运行结果:false  true

12.字符串替换功能,常见
public String replaceAll(String regex, String replacement)使用给定的 replacement 替换此字符串所有匹配给定的正则表达式的子字符串。
举例:
public class ReplaceAll { 
    public static void main(String[] args) {
       String str = "nishuibaichuan";
       String newString = str.replaceAll("i", "I");//将字符串中所有的i替换为大写I
       System.out.println(newString);
     }
}
运行结果:
nIshuIbaIchuan

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值