Java字符串String常用的方法与详细使用

字符串String常用方法:

length()、equals()、startWith()、endsWith()、compareTo()、contains()、indexOf()、lastIndexOf()、substring()、trim();

一、public int length() 的使用:

通过该方法:

获取一个 String 对象的字符序列长度(字符个数);

如下代码↗:

// public int length() 计算字符串的长度,字符个数
	    String word = "我在学习JavaSE";
	    System.out.println(word.length());
	    System.out.println("JavaSE的长度:"+"JavaSE".length());

字符序列长度:是从 1 开始算起的,不是 0 ;

代码输出结果:

10

JavaSE的长度:6


二、public boolean equals( String s  ) 的使用:

通过该方法:

比较当前 String 对象的字符序列 是否与 s 的字符序列相等,不考虑它们的引用值,相等输出 true ,不相等输出 false ;

如下代码↗:

            String wordz = "ITM" + "ITM" ;
	    String wordx = words + worda ;
	    //equals只判断两个字符序列的常量是否相等,不考虑对象引用是否相等
	    if(wordz.equals(wordx)){
	    	System.out.println("hj == jh");
	    }else{
	    	System.out.println("hj != jh");
	    }
	    
	    // “ == ” 要判断两个字符序列的常量是否相等,也要判断对象引用是否相等,都相等才为true
	    if(wordz == wordx){
	    	System.out.println("hj == jh");
	    }else{
	    	System.out.println("hj != jh");
	    }

代码中, == 与 equals() ,的意义是不一样的;

代码输出结果:

hj == jh
hj != jh


三、public boolean startWith( String s ) 的使用:

通过该方法:

比较当前 String 对象的字符序列前缀 是否与 s 的字符序列相等,相等输出 true ,不相等输出 false ;

如下代码↗:

            String tom = "今天是个好天气";
	    System.out.println("判断前缀,startsWith:"+tom.startsWith("今天"));
	

代码输出结果:

true


四、public boolean endsWith( String s ) 的使用:

通过该方法:

比较当前 String 对象的字符序列后缀 是否与 s 的字符序列相等,相等输出 true ,不相等输出 false ;

如下代码↗:

            String toms = "明天要出太阳了";
	    System.out.println("判断后缀,endsWith:"+toms.endsWith("太了"));

代码输出结果:

false


五、public int compareTo( String s ) 的使用:

通过该方法:

比较两个字符串序列的大小,区分大小写 ,参照 Unicode标准字符表 进行比较大小 ;

1、当 String 对象的字符序列与 s 的 相同,则方法返回 0 ;

2、当 String 对象的字符序列比 s 的 大 ,则方法返回一个 正值 ;

3、当 String 对象的字符序列比 s 的 小 ,则方法返回一个 负值 ;

如下代码↗:

//当 String 对象的字符序列与 s 的 相同,则方法返回 0 ;
            String hellos = "abcd";
	    String hellox = "abcd";
	    String helloz = "ABCD";
	    System.out.println(hellos.compareTo(hellox));
	    System.out.println(hellos.compareTo(helloz));

//当 String 对象的字符序列比 s 的 大 ,则方法返回一个 正值 ;
            String hellos = "abcd";
	    String hellox = "abc";
	    System.out.println(hellos.compareTo(hellox));

//当 String 对象的字符序列比 s 的 小 ,则方法返回一个 负值 ;
            String hellos = "abc";
	    String hellox = "abcd";
	    System.out.println(hellos.compareTo(hellox));

代码输出结果:

0
32
1
-1


六、public boolean compareToIgnoreCase( String s ) 的使用:

该方法与 compareTo( String s )  方法是一样的,唯一不同是该方法不区分大小写;

如下代码↗:

//当 String 对象的字符序列与 s 的 相同,则方法返回 0 ,不区分大小写 ;
	    String hellos = "abcd";
	    String helloz = "ABCD";
	    System.out.println(hellos.compareToIgnoreCase(helloz));

代码输出结果:

0


七、public boolean contains( String s ) 的使用:

通过该方法:

判断当前 String 对象的字符序列 是否 包含参数 s 的字符序列 ,包含输出 true ,不包含输出 false;

如下代码↗:

            String Fa = "Hello you world";
	    System.out.println(Fa.contains("Hl"));
	    System.out.println(Fa.contains(" wo"));

代码输出结果:

false
true


八、public int indexOf( String s ) 、public int indexOf( String s , int startpoint )   的使用:

1、从 String 对象的字符序列 0 索引位置开始检索到首次出现 参数 s 的字符序列位置,并返回该位置;

2、当没有检索到对应的字符时,返回的值是 -1 ;

3、空格字符也算 ;

public int indexOf( String s ) 方法:

代码如下↗:

//用于判断字符最先出现的位置
            String cases = "hello are you,thank you";
	    System.out.println(cases.indexOf("ar"));     
	    System.out.println(cases.indexOf("ae"));      

代码输出结果:

6
-1


public int indexOf( String s , int startpoint )  方法:

代码如下↗:

            String cases = "hello are you,thank you";
	    System.out.println(cases.indexOf("llo",1));   //用于判断指定位置的字符
	    System.out.println(cases.indexOf("llo",11));   //用于判断指定位置的字符

代码输出结果:

2
-1


九、public int lastIndexOf( String s ) 的使用:

1、从 String 对象的字符序列 0 索引位置开始检索到最后出现 参数 s 的字符序列位置,并返回该位置;

如下代码↗:

            String cases = "hello are you,thank you";
	    System.out.println(cases.lastIndexOf("o"));  //用于判断字符最后出现的位置
	    System.out.println(cases.lastIndexOf("a"));  //用于判断字符最后出现的位置
	    
	    String casex = "https://fanyi.baidu.com//?aldtype=85&keyfrom";
	    System.out.println(casex.indexOf("//"));      //用于判断字符最先出现的位置
	    System.out.println(casex.lastIndexOf("//"));    //用于判断字符最后出现的位置

代码输出结果:

21
16
6
23


十、public boolean substring ( int startpoint ) 的使用:

通过该方法:

从 0 开始算起,复制 String 对象的字符序列中的 startpoint 位置 到 最后位置上的字符,得到一个新的 String 对象;

如下代码↗:

            String sub = "梦想着,怀念着,希望着" ;
	    String su = sub.substring(1);
	    System.out.println(su);  

代码输出结果:

想着,怀念着,希望着


十一、public boolean substring ( int start , int end ) 的使用:

通过该方法:

从 0 开始算起,复制 String 对象的字符序列中的 start 位置 到 end-1 位置上的字符,得到一个新的 String 对象;

如下代码↗:

            String sub = "梦想着,怀念着,希望着" ;
	    String ss = sub.substring(1, 5);   //5-1
	    System.out.println(ss); 
	    String sx = sub.substring(4, 6);   //6-1
	    System.out.println(sx);  

代码输出结果:

想着,怀
怀念


十二、public boolean trim() 的使用:

trim( ) 方法也是得到一个新 String 对象,但是这个新的 String 对象的字符序列是当前 String 对象的字符序列去掉前后空格后的字符序列;

如下代码↗:

            String wordd = " and you,go to eat fish " ;
	    System.out.println(wordd+",Yes");
	    String wordds = wordd.trim();
	    System.out.println(wordds+",Yes");

代码输出结果:

 and you,go to eat fish ,Yes
and you,go to eat fish,Yes

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值