JAVA中String的用法

您所在的位置:网站首页 java中的参数有什么用 JAVA中String的用法

JAVA中String的用法

2023-03-15 14:06| 来源: 网络整理| 查看: 265

一、String类

String类在java.lang包中,java使用String类创建一个字符串变量,字符串变量属于对象。java把String类声明的final类,不能有类。String类对象创建后不能修改,由0或多个字符组成,包含在一对双引号之间。

二、String类对象的创建

1.String str = "Hello";

2.String str = new String("Hello");

三、String类常用方法

1.求字符串的长度

public int length() // 返回该字符串的长度

String str = "Hello";

int strlength = str.length(); // 5

2、求字符串某一位置字符

public char charAt(int index) // 返回字符串中指定位置的字符;注意字符串中第一个字符索引是0,最后一个是length()-1。

String str = "Hello";

char ch = str.charAt(4);//ch = o

3、提取子串

用String类的substring方法可以提取字符串中的子串,该方法有两种常用参数:

1)public String substring(int beginIndex) // 该方法从beginIndex位置起,从当前字符串中取出剩余的字符作为一个新的字符串返回。

2)public String substring(int beginIndex, int endIndex) // 该方法从beginIndex位置起,从当前字符串中取出到endIndex-1位置的字符作为一个新的字符串返回。

String str1 = "Hello";

String str2 = str1.substring(2); // str2 = "llo"

String str3 = str1.substring(2,4); //str3 = "ll"

4、字符串比较

1)public int compareTo(String anotherString)//该方法是对字符串内容按字典顺序进行大小比较,通过返回的整数值指明当前字符串与参数字符串的大小关系。若当前对象比参数大则返回正整数,反之返回负整数,相等返回0。

2)public int compareToIgnore(String anotherString)//与compareTo方法相似,但忽略大小写。

3)public boolean equals(Object anotherObject)//比较当前字符串和参数字符串,在两个字符串相等的时候返回true,否则返回false。

4)public boolean equalsIgnoreCase(String anotherString)//与equals方法相似,但忽略大小写。

String str1 = "abc";

String str2 = "ABC";

int a = str1.compareTo(str2); //a>0

int b = str1.compareToIgnoreCase(str2); //b=0

boolean c = str1.equals(str2); //c=false

boolean d = str1.equalsIgnoreCase(str2); //d=true

5、字符串连接

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

String str = "Hello".concat("World").concat("!"); // HelloWorld!

6、字符串中单个字符或字符串的查找

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

7、字符串中字符的大小写转换

1)public String toLowerCase()//返回将当前字符串中所有字符转换成小写后的新串

2)public String toUpperCase()//返回将当前字符串中所有字符转换成大写后的新串

String str = new String("Hello");

String str1 = str.toLowerCase();//str1 = "hello"

String str2 = str.toUpperCase();//str2 = "HELLO"

8、字符串中字符的替换

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 = "Hello";

String str1 = str.replace('e','E');//str1 = "HEllo"

String str2 = str.replaceFirst("He","qw");//str2 = "qwllo"

String str3 = str.replaceAll("He","df");//str3 = "dfllo"

9.其他方法

1)String trim //忽略字符串前导和尾部的空格,返回字符串

String s = " Hello ";

System.out.println(s.trim()); // s = "Hello"

2)isEmpty() // 判断字符串是否为空。

String s = " Hello ";

System.out.println(s.isEmpty()); // s = "false"

3) contains() //判断是否包含指定的字符串

String s = " Hello ";

System.out.println(s.contains("ll")); // s = "true"

4) char[] toCharArray() // 将此字符串转换为一个新的字符数组。

5)boolean startsWith(String prefix) // 测试此字符串是否以指定的子串开始。

6) boolean endsWith(String prefix) // 测试此字符串是否以指定的子串结束。

7)String[] split(String regex,int limit) // 根据匹配给定的正则表达式来拆分此字符串



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3