JAVA——String

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/lanse_l/article/details/89383405

常用构造函数:

public String(); 空构造函数
String(String str);以字符串作为参数

public String (byte[] bytes):把字节数组转化为字符串
public String (byte[] byte,int offset,int length):从字节数组的第offset位将字符串的length个字节转化为字符串

public String (char [] value): 把字符数组转化为字符串
public String (char[] value,int offset,int length):从字符数组的第offset位将字符串的length个字节转化为字符串

常用操作:

基本类型转换为字符串:

static String valueOf(boolean b)  返回布尔参数的字符串表示形式。
static String valueOf(char c)返回char参数的字符串表示形式。
static String valueOf(char[] data)返回char数组参数的字符串表示形式。
static String valueOf(char[] data, int offset, int count)返回char数组参数的特定子数组的字符串表示形式。
static String valueOf(double d)返回双参数的字符串表示形式。
static String valueOf(float f)返回浮点参数的字符串表示形式。
static String valueOf(int i)返回int参数的字符串表示形式。
static String valueOf(long l)  返回长参数的字符串表示形式。
static String valueOf(Object obj)  返回对象参数的字符串表示形式。

连接:

s1 += s2; //直接以'+'连接
s1.concat(s2);//用函数concat连接

比较 :

s1 == s2;//直接用"=="比较
s1.compareTo(s2);//相同返回0
s1.compareToIgnoreCase(s2);//忽略大小写

 截取:

#返回一个新字符串,该字符串是该字符串的子字符串。
String substring(int beginIndex)
String substring(int beginIndex, int endIndex)

替换:

String replace(char oldChar, char newChar) 用newChar替换字符串中出现的所有oldChar。
String replaceAll(String regex, String replacement)用replacement替换符合regex正则表达式的每个子字符串。
String replaceFirst(String regex, String replacement)用replacement替换符合regex正则表达式的第一个子字符串。

搜索 :

int indexOf (int ch)返回指定字符第一次出现的字符串中的索引。
int indexOf(int ch, int fromIndex)返回此字符串中指定字符第一次出现的索引,从指定索引处开始搜索。

int indexOf (String str)返回指定子字符串第一次出现的字符串中的索引。
int indexOf(String str, int fromIndex)返回指定子字符串第一次出现的字符串中的索引,从指定的索引开始。

大小写转换:

String toLowerCase() //将字符串的字母全部转换为小写
String toUpperCase() //将字符串的字母全部转换为大写

 删除指定字符或子串:

public class Test {
    public static void main(String []args) {
		String s1 = "HelloWorld";
		String s2 = removeCharAt(s1,3);
		String s3 = removeString(s1,3,6);
		System.out.println(s2); 
		System.out.println(s3); 
	}
	public static String removeCharAt(String s, int pos) {      
		return s.substring(0, pos) + s.substring(pos + 1);   
	}
	public static String removeString(String s, int start,int end) {      
		return s.substring(0, start) + s.substring(end + 1);   
	}
}

练习使用:

import java.util.*;
public class Main {

	public static void main(String[] args) {
		String s1 = "Hello", s2 = "Hello";
		//字符串比较
		System.out.println(":" + s1==s2);  //false
		System.out.println("equals :" + s1.equals(s2)); //true
		System.out.println("compareTo :" + s1.compareTo(s2)); //0
		//字符串拼接
		s1 += s2;  //直接用" + "拼接   
		s1 = s1.concat(s2); //用函数concat拼接
		//截取
		s1 = s1.substring(5); //下标5与之后所有字符
		s1 = s1.substring(5,10); //左闭右开
		//替换
		s1 = s1.replace('e', 'O'); //把所有的'l'换成'B'
		s1 = s1.replaceFirst("[A-Z]", "a");//把第一个大写换成a
		s1 = s1.replaceAll("[A-Z]", "B");//把所有的大写都换成b
		//搜索
		System.out.println(s1); // abllo
		System.out.println(s1.indexOf("l")); //返回搜索到的第一个'l'下标,结果 2
		System.out.println(s1.indexOf("l",3));//从下标为3开始搜索  , 结果  3
		//大小写转换
		s1 = s1.toLowerCase();//全部转换为小写
		s1 = s1.toUpperCase();//全部转换为大写
		System.out.println(s1); //ABLLO
		//删除子串
		s1 = removeString(s1, 1, 4);//左闭右开   删除1,2,3
		System.out.println(s1); //结果 AO
		//整数 -- 》字符串,其他基础类型 一样的方法
		int x = 125;
		String s = String.valueOf(x);
		System.out.println(s); //125
		//包装类可以直接使用toString
		Integer x1 = 300;  //自动装箱
		s = x1.toString();
		System.out.println(s); //300
		//字符串 -- 》整数,其他基础类型 一样的方法
		int y = Integer.valueOf(s); //自动拆箱
		System.out.println(y); //300
		
	}
	public static String removeString(String s, int start,int end) {      
		return s.substring(0, start) + s.substring(end);   
	}
}

猜你喜欢

转载自blog.csdn.net/lanse_l/article/details/89383405