Java中的String类

字符串的长度

length

使用length()可以获取一个字符串的长度

String str="hello world!";
int len=str.length();
System.out.println(len);//12
String str = null;
int len=str.length();//这个是错误的
null字符串不能使用length()

注意: 字符串用length(),数组用length

字符串的拼接

concat

java中可以使用+来拼接字符串,还可以使用concat()方法

String str1="hello";
String str2="world!";
String str=str1.concat(str2);
System.out.println(str1+str2);//helloworld!
System.out.println(str);//helloworld!

字符串的比较

equals

方法原型:

public boolean equals(String str)

功能:判断两个字符串内容是否相同
注意:对字符串对象进行比较使用比较运算符==,此时比较的是两个字符串的地址是否相同.

 String str1="hello";
 String str2="world!";
 boolean ret=str1.equals(str2);
 System.out.println(ret);//false

equalsIgnoreCase

功能:判断两个字符串是否相等,忽略大小写

String str1="hello";
String str2="Hello";
boolean ret=str1.equalsIgnoreCase(str2);
System.out.println(ret);//true

compareTo

方法原型:

public int compareTo(String str)

功能:用来比较两个字符串,若当前字符串与参数str相同则返回0,大于则返回正值,小于则返回负数.

String str1="hello";
String str2="hehe";
int ret=str1.compareTo(str2);//str1>str2
System.out.println(ret);//4

compareToIgnoreCase

功能:和compareTo()方法一样,唯一的区别是compareToIgnoreCase()方法忽略大小写

String str1="hello";
String str2="HeLLo";
int ret=str1.compareToIgnoreCase(str2);
System.out.println(ret);//0

字符串的检索

indexOf

方法原型:

public int indexOf(String str)

功能:从当前字符串首字符开始检索字符串str,返回首次出现str的位置
如果没有检索到字符串str,则返回-1

public static void main(String[] args) {
        String str1="hello worlod";
        int ret=str1.indexOf("lo");
        System.out.println(ret);//3
        System.out.println(str1.indexOf("w"));//6
        System.out.println(str1.indexOf("HA"));//-1
    }

方法原型:

public int indexOf(String str,int fromIndex)

返回从fromIndex位置开始检索字符串str在当前字符串第一次出现的位置,如果检索不到字符串str则返回-1

 public static void main(String[] args) {
        String str1="hello worlod";
        int ret=str1.indexOf("lo",6);//9
        System.out.println(ret);//3
    }

lastIndexOf

方法原型:

public int lastIndexOf(String str)

功能:从当前字符串首字符开始检索字符串str,并返回最后一次出现字符串str的位置,若没有检索到字符串str则返回-1

 public static void main(String[] args) {
        String str1="hello worlod";
        int ret=str1.lastIndexOf("lo");
        System.out.println(ret);//9
        System.out.println(str1.lastIndexOf("HA"));//-1
    }

说明:如果lastIndexOf()方法中的参数是空字符串""(注意没有空格),则返回的结果与调用length()方法的返回结果相同.(因此也可以用lastIndexOf()的方法来求字符串的长度)

//用两种方式来判断字符串的长度
String str="We are students";
int len=str.lastIndexOf("");
System.out.println("空字符串在字符串str中的索引位置:"+len);
System.out.println("字符串str的长度:"+str.length());
/*空字符串在字符串str中的索引位置:15
字符串str的长度:15*/

字符串的截取

substring

方法原型:

public String substring(int beginIndex)
/*从当前字符串下标索引为beginIndex处开始,截取到最后一个字符,
最后返回截取到的子串*/

或:

public String substring(int beginIndex,int endIndex)
/*从当前字符串下标索引为beginIndex处开始,
截取到下标索引为endIndex处(不包括endIndex处的字符)*/
public static void main(String[] args) {
        String str1="hello world";
        String sub=str1.substring(6);
        System.out.println(sub);//world
        System.out.println(str1.substring(0,3));//hel
    }

字符串转换大小写

toLowerCase

方法原型:

public String toLowerCase()

功能:将当前字符串全部大写字母都转换成小写

public static void main(String[] args) {
        String str1="HELLO world";
        String toL=str1.toLowerCase();
        System.out.println(toL);//hello world
    }

toUpperCase

方法原型:

public String toUpperCase()

功能:将当前字符串全部小写字母都转换成大写

public static void main(String[] args) {
        String str1="HELLO world";
        String toU=str1.toUpperCase();
        System.out.println(toL);//HELLO WORLD
    }

字符的替换

replace

方法原型:

public String replace(char oldChar,char newChar)

功能:将当前字符串中的所有oldChar旧字符替换成newChar新字符.

public static void main(String[] args) {
        String str1="hello world";
        String rep=str1.replace('l','x');
        System.out.println(rep);//hexxo worxd
	}

字符串替换

replace和replaceAll

这两个方法都是全部替换

public String replace(CharSequence target,CharSequence replacement)
// 将旧字符序列target换成新字符序列replacement(字符序列:字符或字符串)
public String replaceAll(String regex,String replacement)
//将旧字符串或正则表达式内容,替换成新字符串                                              
String str = "hello wlorld";
String rep=str.replace("lo","hh");
System.out.println(rep);//helhh whhrld

replaceFirst

只替换第一个(不是全部替换)
方法原型:

public String replaceFirst(String regex,String replacement)
//将旧字符串或正则表达式替换成新字符串                          
String str = "hello wlorld";
String rep=str.replaceFirst("lo","hh");//helhh wlorld

trim

方法原型:

public String trim()

功能:去掉字符串左右两边的空格

 public static void main(String[] args) {
        String str1=" hello worl d ";
        String str2=str1.trim();
        System.out.println(str2);//hello worl d 即("hello worl d")
	}

数值转换为字符串

valueOf

方法原型:

public static String valueOf(double d)

valueOf()方法参数可以是byte,short,int,long,float,double类型的数据,valueOf()方法将这些数值转换成字符串.

public static void main(String[] args) {
        String str=String.valueOf(234);
        System.out.println(str);//234
	 }

我们可以使用Integer类中的方法public static int parseInt(String str)将数字格式的字符串转化成int类型的数据

public static void main(String[] args) {
        int x=Integer.parseInt("1234");
        System.out.println(x);//1234
	 }

charAt

方法原型:

public char charAt(int index)

功能:返回指定索引值的字符

public static void main(String[] args) {
        String str="helloworld";
        char c=str.charAt(4);
        System.out.println(c);//o
        }

endsWith

方法原型:

public boolean endsWith(String suffix)
//suffix----后缀

功能:检查当前字符串是否是以字符串suffix结尾的,一般用来检查图片是否是以.jpgpng等结尾的

public static void main(String[] args) {
        String str="helloworld";
        boolean c=str.endsWith("ld");
        System.out.println(c);//true
        }

startsWith

方法原型:

public boolean startsWith(String prefix)
//prefix----前缀

功能:检查当前字符串是否是以字符串prefix开头的

public static void main(String[] args) {
        String str="helloworld";
        boolean c=str.startsWith("he");
        System.out.println(c);//true
        }

方法原型:

public boolean startsWith(String prefix,toffset)
//prefix----前缀

功能:检查当前字符串偏移量为2的位置是否是以字符串prefix开头.

public static void main(String[] args) {
        String str="helloworld";
        boolean c=str.startsWith("ll",2);
        System.out.println(c);//true
        }

字符串的切割

split

方法原型:

public String[] split(String regex)

regex代表分隔符,返回一个String数组,它的元素是分割的每个字符串

 public static void main(String[] args) {
        String str1="hello:world:very:good";
        String [] str2=str1.split(":");
        System.out.println(Arrays.toString(str2));
        //[hello, world, very, good]
        }
String str = "192.168.0.1";
String[] firstArray = str.split("\\.");
String[] secondArray = str.split("\\.", 2);
System.out.println(Arrays.toString(firstArray));//[192, 168, 0, 1]
System.out.println(Arrays.toString(secondArray));//[192, 168.0.1]

将字符串变成字符数组

toCharArray

方法原型:

public char[] toCharArray()
String str = "这是一个字符串";
char[] ch = str.toCharArray();
System.out.println(Arrays.toString(ch));//[这, 是, 一, 个, 字, 符,串]

判断子字符串是否存在

contains

方法原型:

public boolean contains(CharSequence s)
//字符序列s:要查找的子字符串
String str = "hello world";
boolean ret=str.contains("wo");
System.out.println(ret);//true
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值