java中关于String的常用方法

常见的构造方式

        String s1="Hello World";

        String s2=new String("Hello World");
        
        char[] arr={'H','e','l','l','o'};
        String s3=new String(arr);

String类的常见方法

判断相等——String.equals()

对于int类型的变量我们一般会用“”来判断二者内容是否相等,但是String类型其实是一种引用类型,“”只能判断二者是否指向的是同一目标,不能直接比较指向对象的内容,因此,我们可以使用java自带的判断相等的方法——equals(),返回值类型为Boolean类型

System.out.println(s1.equals(s2));
//要比较的字符串s1.equals(字符串s2);

求字符串长度——String.length()

返回值类型为int

求字符串某一位置字符——String.charAt(int item)

item是字符串待求位置下标,返回值类型为char

提取子串——substring

该方法有两种常用参数:
1)String.substring(int beginIndex)
//该方法从beginIndex位置起,从当前字符串中取出剩余的字符作为一个新的字符串返回。
2)String.substring(int beginIndex, int endIndex)
//该方法从beginIndex位置起,从当前字符串中取出到endIndex-1位置的字符作为一个新的字符串返回。

String s1="Hello World";
System.out.println(s1.substring(2));   //llo World
System.out.println(s1.substring(2, 6));   //llo

字符串比较——compareTo

当前对象.compareTo(参数),相等返回0;大于返回正数;小于返回负数。

String str1 = new String("abc");
String str2 = new String("ABC");
int a = str1.compareTo(str2);  //a>0
int b = str1.compareTo(str2);  //b=0
boolean c = str1.equals(str2);  //c=false
boolean d = str1.equalsIgnoreCase(str2);  //d=true

字符串连接——concat

将参数中的字符串str连接到当前字符串的后面,效果等价于"+"。

String s2=new String("Hello World");
String s1="Hello World!";
String s3=s1.concat(s2);
System.out.println(s3);  //Hello World!Hello World

字符串中单个字符查找——indexOf

1)public int indexOf(int ch/String str)
//用于查找当前字符串中字符或子串,返回字符或子串在当前字符串中从左边起首次出现的位置,若没有出现则返回-1。
2)public int indexOf(int ch/String str, int fromIndex)
//与第一种类似,区别在于该方法从fromIndex位置向后查找。
3)public int lastIndexOf(int ch/String str)
//与第一种类似,区别在于该方法从字符串的末尾位置向前查找。
4)public int lastIndexOf(int ch/String str, int fromIndex)
//与第二种方法类似,区别于该方法从fromIndex位置向前查找。

String str = "I am a good student";
int a = str.indexOf('a');//a = 2
int b = str.indexOf("good");//b = 7
int c = str.indexOf("w",2);//c = -1
int d = str.lastIndexOf("a");//d = 5
int e = str.lastIndexOf("a",3);//e = 2

字符串中字符的大小写转换——toLowerCase(),toUpperCase()`

String str = new String("asDF");
String str1 = str.toLowerCase();//str1 = "asdf"
String str2 = str.toUpperCase();//str2 = "ASDF" 

字符串中字符的替换——replace

1)public String replace(char oldChar, char newChar)
//用字符newChar替换当前字符串中所有的oldChar字符,并返回一个新的字符串。
2)public String replaceFirst(String regex, String replacement)
//用字符replacement的内容替换当前字符串中遇到的第一个和字符串regex相匹配的子串,应将新的字符串返回。
3)public String replaceAll(String regex, String replacement)
//用字符replacement的内容替换当前字符串中遇到的所有和字符串regex相匹配的子串,应将新的字符串返回。

String str = "asdzxcasd";
String str1 = str.replace('a','g');//str1 = "gsdzxcgsd"
String str2 = str.replace("asd","fgh");//str2 = "fghzxcfgh"
String str3 = str.replaceFirst("asd","fgh");//str3 = "fghzxcasd"
String str4 = str.replaceAll("asd","fgh");//str4 = "fghzxcfgh" 
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值