String的一些总结(JAVA)

equals:

String s1=new String("Hello");

String s2=new String("Hello");

System.out.println(s1==s2);//比较的是是否是同一对象

System.out.println(s1.equals(s2));//比较的是是否是同一内容

charAt()

charAt()在括号里填入数字,返回指定索引位置的char值,索引范围为0~length()-1,比如“MapleStory”中charAt(1)中返回的是“a”

Length()

String s="";

int i=s.length();

i 结果为0.
若String s=null;

int i=s.length();

编译没错,但是运行会报错

getchar() 

getChars(int begin,int end,char dst[],int dstBegin)将字符从此字符串复制到目标字符数组。
要复制的第一个字符在索引 begin 处;要复制的最后一个字符在索引 end 处。要复制到 dst 子数组的字符从索引 dstBegin 处开始,并结束于索引.

例如
String str = "asdwegddxv";
Char[] ch = new char[10];
str.getChars(2,5,ch,2);
就是从str的第三个字母开始一直复制到第六个,一共是3个字符,从ch的第三个开始放入字母。

 

toUpperCase()

把字符串转换为大写,所有小写字符全部转换为大写字符

String  str = “Hello World”.toUpperCase();
其结果为HELLO WORLD

toLowerCase() 

把字符串转换为小写,所有大写字符全部转换为小写字符

String  str = “Hello World”.toLowerCase();
其结果为hello world!

replace()

replace(char old, char  new) ,用new替换old。

public class TestReplace

{      

    public static void  main(String[] args)

   {    

   String s = "This is my original  string ,it is very good!";    

   String r =  "at";   

   s =  s.replace("is",r);

   System.out.println(s);   

   }

 }

就将is 换成 at

 

trim()函数:

去掉首尾空格,用法为trim(" A B C D  ") 等于 "A B C D"

 

toCharArray()函数:
字符串转换为字符数组。

String str = "ABCD"; char ch[] = str.toCharArray(); 这个b保存的信息就是['A','B','C','D']。

 

 

 

  

转载于:https://www.cnblogs.com/maplely/p/4909877.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值