JAVA学习【知】String类

String类是JAVA里常用的类

String对象是不可变的,对于学习String类来说最主要的方法就是查API。

API里详细介绍了String类的构造方法和方法。

String的方法有很多 比如

 

charcharAt(int index)
          返回指定索引处的 char 值。
 intcodePointAt(int index)
          返回指定索引处的字符(Unicode 代码点)。
 intcodePointBefore(int index)
          返回指定索引之前的字符(Unicode 代码点)。
 intcodePointCount(int beginIndex, int endIndex)
          返回此 String 的指定文本范围中的 Unicode 代码点数。
 intcompareTo(String anotherString)
          按字典顺序比较两个字符串。
 intcompareToIgnoreCase(String str)
          不考虑大小写,按字典顺序比较两个字符串。
 Stringconcat(String str)
          将指定字符串联到此字符串的结尾。
 booleancontains(CharSequence s)
          当且仅当此字符串包含 char 值的指定序列时,才返回 true。
 booleancontentEquals(CharSequence cs)
          当且仅当此 String 表示与指定序列相同的 char 值时,才返回 true
 booleancontentEquals(StringBuffer sb)
          当且仅当此 String 表示与指定的 StringBuffer 相同的字符序列时,才返回 true
static StringcopyValueOf(char[] data)
          返回指定数组中表示该字符序列的字符串。
static StringcopyValueOf(char[] data, int offset, int count)
          返回指定数组中表示该字符序列的字符串。
 booleanendsWith(String suffix)
          测试此字符串是否以指定的后缀结束。
 booleanequals(Object anObject)
          比较此字符串与指定的对象。
 booleanequalsIgnoreCase(String anotherString)
          将此 String 与另一个 String 进行比较,不考虑大小写。
static Stringformat(Locale l, String format, Object... args)
          使用指定的语言环境、格式字符串和参数返回一个格式化字符串。
static Stringformat(String format, Object... args)
          使用指定的格式字符串和参数返回一个格式化字符串。
 byte[]getBytes()

 

 

 

等等在这里我们介绍一些常用的方法

 

1.charAt(int index)

 

 

  1. //将字符串“fghjdEQEyteqwDQJWeqwEeqw$%^&*568321”分类 求出 分类后的大写,小写字母以及其他字符的个数;
  2. public class Zimu {
  3.  public static void main(String[] args) {
  4.   String s="fghjdEQEyteqwDQJWeqwEeqw$%^&*568321"//定义字符串
  5.   int lcount=0,ucount=0,ocount=0;     //小写,大写,其他个数初始为0
  6.   for(int i=0;i<s.length();i++){
  7.    char c = s.charAt(i);      //字符串转换成字符
  8.    if(c>'a'&&c<'z'){
  9.     lcount++;
  10.    }
  11.    else if(c>'A'&&c<'Z'){
  12.     ucount++;
  13.    }
  14.    else 
  15.     ocount++;
  16.   }
  17.   System.out.println(lcount+" "+ucount+" "+ocount);
  18.  }
  19. }

 

 

 

2.split(String regex)

 

 

  1. //将"My,name,is,Split" 以逗号为拆分符号进行拆分 最后的结果为MynameisSplit
  2. public class Split {
  3.     public static void main(String[] args) {
  4.         String s="My,name,is,Split";
  5.         String[] split=s.split(",");    //split方法拆分字符串
  6.         for(int i=0;i<split.length;i++){
  7.             System.out.print(split[i]);
  8.         }
  9.     }
  10. }

 

 

3.valueOf(char c)

 

  1. //计算345678为几位数字
  2. public class ValueOf {
  3.     
  4.     public static void main(String[] args) {
  5.         int i=345678;
  6.         String value =String.valueOf(i);    //将int型转换为字符串型
  7.         System.out.println("i 是"+value.length()+"位数");  //用length求出位数
  8.     }
  9. }

 

如果想弄清楚String类 那么经常去查API弄清楚里面的方法

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值