java回顾之String详解

1.String类的构造方法

public String();空构造
②public String(byte[] byte);//把字节数组转换成字符串
例:
	byte[] b={
    
    97,98,99};
	String s=new String(b);
	System.out.println(s);
	输出为:abc
③ public String(byte[] bytes,int index,int length)//把字节数组的一部分转成字符串
例:
	byte[] b={
    
    97,98,99,100};
	String s=new String(b,1,3);
	System.out.println(s);
	输出为:bcd
④public String(char[] value):把字符数组转成字符串
例:
	char[] b={
    
    'q','w','a'};
	String s=new String(b);
	System.out.println(s);
	输出为:qwa
⑤public String(char[] value,int index,int count):把字符数组的一部分转成字符串
例:
	char[] b={
    
    'q','w','a','s','d'};
    String s=new String(b,2,3);
    System.out.println(s);
    输出为:asd
⑥public String(String original):把字符串常量值转成字符串
	String s=new String("hello");
    System.out.println(s);
    输出为:hello

2.String类的判断功能

boolean equals(Object obj):比较字符串的内容是否相同,区分大小写
例:
	String a="hello";
    String b="nihao";
    String c="hello";
    System.out.println(a.equals(b));
    System.out.println(a.equals(c));
    输出:false
		 trueboolean equalsIgnoreCase(String str):比较字符串的内容是否相同,忽略大小写
例:
	String a="hello";
    String b="nihao";
    String c="HEllo";
    System.out.println(a.equalsIgnoreCase(b));
    System.out.println(a.equalsIgnoreCase(c));
    输出:false
		 trueboolean contains(String str):判断大字符串中是否包含小字符串
例:
	String a="hello word";
    String b="word";
    String c="nihao";
    System.out.println(a.contains(b));
    System.out.println(a.contains(c));
    输出:true
    	 falseboolean startsWith(String str):判断字符串是否以某个指定的字符串开头
例:
	String a="hello word";
    String b="he";
    String c="wor";
    System.out.println(a.startsWith(b));
    System.out.println(a.startsWith(c));
    输出:true
    	 falseboolean endsWith(String str):判断字符串是否以某个指定的字符串结尾
例:
	String a="hello word";
    String b="word";
    String c="he";
    System.out.println(a.endsWith(b));
    System.out.println(a.endsWith(c));
    输出:true
    	 falseboolean isEmpty():判断字符串是否为空。
例:
	String s="";
    String b="hello";
    System.out.println(s.isEmpty());
    System.out.println(b.isEmpty());
    输出:true
    	 false

3.String类的获取功能

* int length():获取字符串的长度。
 	String a="hello";
    System.out.println(a.length());
  	输出:5
* char charAt(int index):获取指定索引位置的字符
	String a="hello";
	System.out.println(a.charAt(3));
	输出:l
* int indexOf(char ch):返回指定字符在此字符串中第一次出现处的索引。
	String a="hello";
	System.out.println(a.indexOf('l'));
	输出:2
* int indexOf(String str):返回指定字符串在此字符串中第一次出现处的索引。
	String b="nihao hello";
    System.out.println(b.indexOf("hello"));
    输出:6
* int indexOf(char ch,int fromIndex):返回指定字符在此字符串中从指定位置后第一次出现处的索引。
	String b="nihao hello nihao";
	System.out.println(b.indexOf('n',5));
	输出:12
* int indexOf(String str,int fromIndex):返回指定字符串在此字符串中从指定位置后第一次出现处的索引。
	String b="nihao hello nihao";
	System.out.println(b.indexOf("nihao",5));
	输出:12
* lastIndexOf:和indexOf类似不再给出
* String substring(int start):从指定位置开始截取字符串,默认到末尾。
	String b="nihao hello nihao";
	System.out.println(b.substring(4));
	输出:o hello nihao
* String substring(int start,int end):从指定位置开始到指定位置结束截取字符串。
	String b="nihao hello nihao";
	System.out.println(b.substring(4,10));
	输出:o hell

4.String类的转换功能

* byte[] getBytes():把字符串转换为字节数组。
* char[] toCharArray():把字符串转换为字符数组。
* static String valueOf(char[] chs):把字符数组转成字符串。
* static String valueOf(int i):int类型的数据转成字符串。
		* 注意:String类的valueOf方法可以把任意类型的数据转成字符串

* String toLowerCase():把字符串转成小写。
* String toUpperCase():把字符串转成大写。
* String concat(String str):把字符串拼接。

5.String类的其他功能

替换功能:
* String replace(char old,char new)
* String replace(String old,String new)
去除字符串两空格
* String trim()
按字典顺序比较两个字符串
* int compareTo(String str)
* int compareToIgnoreCase(String str)

6.String和int的相互转换

int->String
1.int i=123;
String s=i+"";
2.String s=String.valueOf(i);
String->int
String s="123";
int i=Integer.parseInt(s);

猜你喜欢

转载自blog.csdn.net/weixin_42856363/article/details/104348320