JAVA语言中String类的常用方法

String类的创建

String str = new String();// 创建
String str = new String("Hello World!");//初始化

"+"号运算符

既可以在String对象之间相加,也能加整形和浮点型数字(自动转换为String对象)。

//示例
String s1 = new String("abc");
String s2 = new String("def");
String s3 = s1 + s2;//String对象相加
String s4 = s1 + 1;//String对象与整型相加
String s5 = s2 + 1.00;///String对象与浮点型相加(默认保留一位小数)
System.out.println(s3+"\n"+s4+"\n"+s5);

常用方法

求字符串长度

public int length()

String str = new String("abcde");
System.out.println(str.length());//结果为5

求指定位置的字符

public char charAt(int index)

String str = new String("abcde");
System.out.println(str.charAt(2));//结果为c

提取子串

public int substring(int begin) //从begin到length()-1
public int substring(int begin, int end) //从begin到end-1

String str = new String("abcde");
System.out.println(str.substring(2));//结果为cde
System.out.println(str.substring(1,4));//结果为bcd

字符串比较

public boolean equals(String anotherString) //判断是否相等
public boolean equalsIgnoreCase(String anotherString) //判断是否相等,忽略大小写
public int compareTo(String anotherString)//比较,对象大则返回正数,等于则返回0,参数大则返回负数
public int compareToIgnoreCase(String anotherString)//比较,忽略大小写

String str = new String("abcde");
System.out.println(str.equal("Abcde"));//结果为false
System.out.println(str.equalIgnoreCase("Abcde"));//结果为true
System.out.println(str.compareTo("Abcde"));//结果为正数
System.out.println(str.compareToIgnoreCase("Abcde"));//结果为0

字符串连接(效果等同于"+")

public String concat(String str)

String str = new String("abcde");
System.out.println(str.concat("fg"));//结果为abcdefg

字符串查找

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

String str = new String("abcdefgabcd");
System.out.println(str.indexOf("cd"));//结果为2
System.out.println(str.indexOf("cd", 3));//结果为9
System.out.println(str.lastIndexOf("cd"));//结果为9
System.out.println(str.lastIndexOf("cd", 7));//结果为2

大小写转换

public String toLowerCase()//全部转小写
public String toUpperCase()//全部转大写

String str = new String("abCdeFgaBcd");
System.out.println(str.toLowerCase());//结果为abcdefgabcd
System.out.println(str.toUpperCase());//结果为ABCDEFGABCD

字符(串)替换

public String replace(char old, char new)//当前对象中所有的old被替换为new
public String replaceFirst(String oldstr, String newstr)//替换第一个oldstr为newstr
public String replaceAll(String oldstr, String newstr)//替换所有的oldstr为newstr

String str = new String("abcdefgabcd");
System.out.println(str.replace('a', '*'));//结果为*bcdefg*bcd
System.out.println(str.replaceFirst("abcd", "1234"));//结果为1234efgabcd
System.out.println(str.replaceAll("abcd", "1234"));//结果为1234efg1234

其他方法

public String trim()// 截去字符串两端的空格,中间空格的不处理。

String str = new String(" a b c ");
System.out.println(str.trim());//结果为a b c

public String split(String str)// 将str内的所有字符作为分隔符进行字符串分解。

String str = new String("ABC!JKLJIL$LJKJ%LJI$JJJ");
String[] result = str.split("!$%");
//result[0] = "ABC";
//result[1] = "JKLJIL";
//result[2] = "LJKJ";
//result[3] = "LJI";
//result[4] = "JJJ";

基本类型转换

字符串转其他类型:
public staric byte parseByte(String s)
public staric short parseShort(String s)
public staric int parseInt(String s)
public staric long parseLong(String s)
public staric float parseFloat(String s)
public staric double parseDouble(String s)

int n = Integer.parseInt("12");
float f = Float.parseFloat("12.34");
double d = Double.parseDouble("1.124");

其他类型转字符串:
public static String valueOf(type data)
//type可以是其他任意常见类型

1 String s1 = String.valueOf(12);
2 String s1 = String.valueOf(12.34);

猜你喜欢

转载自blog.csdn.net/weixin_42120714/article/details/83212074