05_整理String类的Length()、charAt()、 getChars()、replace()、 toUpperCase()、 toLowerCase()、trim()、toCharArra...

Question:

整理String类的Length()、charAt()、 getChars()、replace()、 toUpperCase()、 toLowerCase()、trim()、toCharArray()使用说明。

 

Answer:

 

Length():public int length()//求字符串长度,返回字符串长度

         String s=”dwfsdfwfsadf”;

         System.out.println(s.length());

 

 

 

charAt():public char charAt(int index)//返回字符串中指定位置的字符

        String s=”Hello”;

        System.out.println(s.charAt(3));

       注意:字符串中第一个字符的下标是 0。如果参数 index 不在 0 与 string.length 之间,
该方法将返回一个空字符串。

 

 

 

getChars():public void getChars(int srcBegin,int srcEnd,char[] dst,int dstBegin)//将字符从此字符串复制到目标字符数组。 

     要复制的第一个字符在索引 srcBegin 处;

     要复制的最后一个字符在索引 srcEnd-1 处(因此要复制的字符总数是 srcEnd-srcBegin)。

     要复制到 dst 子数组的字符从索引 dstBegin 处开始,并结束于索引.

     例如:

     String str = "abcdefghikl";
     Char[] ch = new char[8];
     str.getChars(2,5,ch,0);
     就是从str的第二个字母开始一直复制到第五个,一共是3个字符,从ch的第一个开始接受。

 

 

replace():public String replace(char oldChar,char newChar)//替换字符串

        返回一个新的字符串,它是通过用 newChar 替换此字符串中出现的所有 oldChar 而生成的。

    如 果 oldChar 在此 String 对象表示的字符序列中没有出现,则返回对此 String 对象的引用。
   否则,创建一个新的 String 对象,用来表示与此 String 对象表示的字符序列相等的字符序列,
    除了每个出现的 oldChar 都被一个 newChar 替换之外。

   注:也就是说调用该函数的字符串是不被改变的,只是返回了一个新的对象,这个新对象可能是对原

字符串的引用,也可能是一个替换之后的新的字符串(和原字符串不同的,地址内容都不同)

 

 

 

toUpperCase():public String toUpperCase()//将字符串全部转换成大写

例如:

         System.out.println(new String(“hello”).toUpperCase());

 

 

 

toLowerCse():public String toLowerCase()//将字符串全部转换成小写

例如:

         System.out.println(new String(“HELLO”).toLowerCase());

 

 

 

trim():public String trim()//去除字符串两边的空格

         String x=”   ax  c   ”;

         System.out.println(x.trim());//只去除字符串两边的空格,中间的空格是不受影响的。

 

 

 

toCharArray(): public char[] toCharArray()// 将字符串转换为一个字符数组

        例如:

     String x=”abcd”          

     char myChar[]=x.toCharArray();

     System.out.println(“myChar[1]”+myChar[1]);

 

转载于:https://www.cnblogs.com/6354-aa/p/5998661.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值