java中String类中的基本方法的使用

java中String类的常用方法

下面开始记录
我们知道对字符串的操作涉及到 ,这些基本操作,下面先来总结这些基本操作

  • 在字符串中连接其它字符可以可以使用+,concat方法
    例代码
  //字符串直接用+号连接或者使用concat方法
  String string = "assddeerrttyy";
  string = string + "I love you";
  System.out.println(string);//assddeerrttyyI love you
  //或者
  System.out.println(string.concat("I love you"));//也是同样的效果
  //concat也可以连续使用
  System.out.println("caree".concat(" needs ").concat(" passion "));//return "caree needs passion"

删,改

  • 删除字符串的某个字符或者替换某个字符,或者删除某个字符串,替换某个字符串,可以使用replace方法,这是一个功能比较强大的方法
    例代码
 String str1 = "assddeerrttyy";
 System.out.println(str1);
 /*替换某个字符*/
 str1 = str1.replace('a',' ');//替换字符串中的a改为空字符
 System.out.println(str1);//" ssddeerrttyy"
 /*替换对应字符串*/
 str1 =  str1.replace("eerr", "所有伤痛");//替换字符串"eerr"为"所有伤痛"
 System.out.println(str1);//" ssdd所有伤痛ttyy"
 /*删除某个字符串*/
 str1 = str1.replace("ttyy", "");// 删除ttyy
 System.out.println(str1);// " ssdd所有伤痛";

所以如果你要删除某个字符或者某段字符串,直接使用replace方法将对应字符串替换为空就好了

  • 查找字符串的某个字符的位置或者某段字符的特定位置,有如下两种方法indexOf,lastIndexOf,查找对应位置的字符方法charAt,使用例代码,
 //接上一代码块中的str1变量
 System.out.println(str1.charAt(2));//s 取字符串第二个位置的字符 
 int rt =  str1.indexOf("所有伤痛");//查找子串的位置,返回子串中第一个字符在字符串中的位置,如果没有找到,则返回-1
 System.out.println(rt);//5
 rt = str1.lastIndexOf("所有伤痛");// 从后往前查找
 System.out.println(rt);//5
 /*查找单个字符*/
 String stre = "abababaccdeaa";
 System.out.println(stre.indexOf('a'));//0,从左往右查找,返回第一次出现的索引
 System.out.println(stre.lastIndexOf('a'));//12,从右往左找
 //indexOf 和 lastIndexOf 也可以指定第二个参数
 System.out.println(stre.indexOf('b',2));//3,表示从字符串第二个位置的字符开始查找
 System.out.println(stre.lastIndexOf('a',10));//6,表示从字符串第十个位置的字符从右往左开始查找

lastIndexOf() 和 IndexOf() 在没有查找到的情况下,都返回-1

相应基本操作总结完毕
下面来总结一些也比较常用的字符串方法

  • isEmpty,isBlank
  /*
  isBlank方法当字符串为空或者字符串中全是空白符的时候返回true,其他返回false
  isEmpty方法当当且仅当字符串为空(length()值为0)返回true,其余返回false
  */
  String str2 = " ";
  System.out.println(str2.isEmpty());//false
  System.out.println(str2.isBlank());//true
  str2 = "";
  System.out.println(str2.isEmpty());//true;
  System.out,println(str2.isBlank());//false;
  • toUpperCase,toLowerCase
/*toUpperCase方法将字符全变为大写
  toLowerCase方法将字符全变为小写
*/
 String str3 = "abvCDtyhfIOhj";
 System.out.println(str3.toUpperCase());//ABVCDTYHFIOHJ
 System.out.println(str3.toLowerCase());//abvcdtyhfiohj
  • contains
/*contains检查一个特定的字符或者子串是否在这一个字符串对象中
 如果在则返回true,不在返回false
*/
 String str4 = "I love you!";
 System.out.println(str4.contains("love"));//true,子串love在串中
 System.out.println(str4.contains("v"));//true 可以理解为字符v在字符串中
 System.out.println(str4.contains("ssf"));//false,该子串不在串中

注意:contains方法的参数只能是字符串对象

  • startsWith,endsWith
/*
startsWith()判断字符串是否某个特定的字符开头
endsWith()判断字符串是否以某个特定的字符结尾
*/
 String str5 = "ss kopol ssedsdgh";
 System.out.println(str5.startsWith("ss"));//true,以ss开头
 System.out.println(str5.startsWith("bv"));//false
 System.out.println(str5.endsWith("dgh"));//true;以dgh 结尾
 System.out.println(str5.endsWith("dgk"));//false
 /*与 indexOf()和lastIndexOf()一样,startsWith一样可以指定第二个参数(整形),
 表示从这个位置开始,是否以特定的字符串开头,注意endsWith()不能指定第二个参数
 */
 System.out.println(str5.startsWith("ss",5));//false;从5开始应该是一ol开头
 System.out.println(str5.startsWith("ol",5));//true;
  • substring
/*截取子串的操作,参数为整形,表示范围*/
 String str6 = "jre sssd ffssdfe";
 System.out.println(str6.substring(1));//"re sssd ffssdfe" 第一个参数必须要指定,表示开始位置,第二个参数可以不指定,默认取到字符串的最后一位
 System.out.println(str6.substring(1,5));//"re s",区间为左闭右开,左边可以取到,右边无法取到,总共可以取到4个字符

注意substring的参数不能为负,如果第一个参数大于字符串长度,或者第一(二)个参数为负,或者第一个参数大于第二个参数,系统都会抛出异常IndexOutOfBoundsException,终止程序的执行


1.但是只有一个参数,参数等于字符串长度的时候,则返回空串"";
2.有两个参数,且这两个参数相等,也会返回空串"";

  • strip,stripLeading,stripTrailing
/**
strip删除字符串前导空格,
stripLeading删除左边空格
stripTrailing删除右边空格
**/
 String str7 = new String("     useless    ");
 System.out.println(str7.strip());//useless;
 System.out.println(str7.stripLeading());//"useless   " 删去前导空格
 System.out.println(str7.stripTrailing());//"    useless"删除后面空格
  • 格式化输出printf,String.format()
 /*常用于保留精度使用*/
 //格式化输出
 System.out.println(String.format("%.3f", 1.4567888));//1.467
 System.out.printf("%.3f\n", 1.4567888);//1.467;
  • compareTo
/*
  compare比较两个字符串的的字典序
  public int compareTo(String anotherString)
按字典顺序比较两个字符串。比较基于字符串中每个字符的Unicode值。此String对象表示的字符序列按字典顺序与参数字符串表示的字符序列进行比较。如果此String对象按字典顺序位于参数字符串之前,则结果为负整数。如果此String对象按字典顺序跟随参数字符串,则结果为正整数。如果字符串相等,结果为零; compareTall在equals(Object)方法返回true时完全返回0。
这是词典排序的定义。如果两个字符串不同,则它们在某个索引处具有不同的字符,这两个字符串是两个字符串的有效索引,或者它们的长度不同,或两者都有。如果它们在一个或多个索引位置具有不同的字符,则令k为最小的索引;然后是位置k处的字符具有较小值的字符串,通过使用<运算符确定,按字典顺序排在另一个字符串之前。在这种情况下,compareTo返回两个字符串中位置k处两个字符值的差值 - 即值:

 this.charAt(k)的-anotherString.charAt(k)的
 
如果没有它们不同的索引位置,那么较短的字符串按字典顺序排在较长的字符串之前。在这种情况下,compareTo返回字符串长度的差异 - 即值:
 this.length()处 -  anotherString.length()
 
有关细粒度的字符串比较,请参阅Collat​​or。

具体说明:
compareTo在接口Comparable <String>中
参数:
anotherString  - 要比较的String。
返回:
如果参数字符串等于此字符串,则值为0;如果此字符串按字典顺序小于字符串参数,则小于0的值;如果此字符串按字典顺序大于字符串参数,则值大于0。
*/
 String str5 = "niss",str6 = "Niss";
 System.out.println(str5.compareTo(str6));//32,Unicode的减法,n-N(串长相同);
 System.out.println(str5.compareTo("nisttrr"));//this.length()-nisst.length();

总之:compareTo:
str1.compareTo(str2),
1.如果str1大于str2,返回正整数
2.如果str1==str2,返回0
3.如果str1<str2,返回负整数

  • equals
/* public boolean equals(Object anObject)
将此字符串与指定的对象进行比较。 
当且仅当参数不为null并且是表示与此对象相同的字符序列的String对象时,结果才为真。
有关细粒度的字符串比较,请参阅Collator。

覆盖:
在类Object中等于
参数:
anObject  - 要比较此String的对象
返回:
如果给定对象表示与此字符串等效的String,则返回true,否则返回false
也可以看看:
compareTo(String),equalsIgnoreCase(String)*/
 String str1 = "I love you";
 String str2 = new String("I love you");
 System.out.ptintln(str1.equals(str2));//true,值相同
总结了基本的String的相关用法,下次继续加油
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值