String类的常用方法
2017-12-31 16:00
363 查看
1.将字符串变为一个字符数组
@Test public void toCharArray(){ char[] arr = str.toCharArray(); for (int i = 0; i < arr.length; i++) { System.out.println("字符数组为:"+arr[i]); } }
2.从字符串中取出指定位置的字符
@Test public void toCharAt(){ char c = str.charAt(2); System.out.println("字符串制定位置的字符为:"+c); }
3.将字符串变成一个byte数组
@Test public void toByte(){ byte arr[] = str.getBytes(); /* for (int j = 0; j < arr.length; j++) { System.out.println("Byte数组为:"+arr[j]); }*/ System.out.println("Byte数组为:"+new String(arr)); }
4.获取字符串长度
@Test public void getLength(){ int length = str.length(); System.out.println("字符串的长度为:"+length); }
5.查找一个制定的字符串是否存在,返回的是字符串的位置;如果不存在,返回-1
@Test public void toIndexOf(){ int a = str.indexOf("i"); int b = str.indexOf("o", 2); System.out.println(a); System.out.println(b); }
6.去除字符串两头的空格
@Test public void toTrim(){ String str = " hello "; System.out.println("字符串去除两头空格结果:"+str.trim()); }
7.字符串截取
@Test public void toSubstring(){ //截取0-3位置的内容(包头不包尾) System.out.println(str.substring(0, 3)); //从第三个位置开始截取(包括起始位置) System.out.println(str.substring(2)); }
8.按照指定的字符拆分字符串,拆分的数据将以字符串数组的形式返回
@Test public void toSplit(){ String[] s = str.split("o"); for (int i = 0; i < s.length; i++) { System.out.println("拆分后的字符串结果:"+s[i]); } }
9.将字符串进行大小写转换
@Test public void toChange(){ System.out.println("将字符串转换为大写:"+str.toUpperCase()); String str = "MILOGENIUS"; System.out.println("将字符串转换为小写:"+str.toLowerCase()); }
10.判断是否以指定的字符串开头或者结尾
@Test public void toStartWithOrEndWith(){ if (str.startsWith("mi")) { System.out.println("字符串是以mi开头的"); } if (str.endsWith("s")) { System.out.println("字符串是以s结尾的"); } }
11.String类型比较
@Test public void toEquals(){ String str1 = "milogenius"; if (str.equals(str1)) { System.out.println("两个字符串类型的值相等"); } else { System.out.println("两个字符串类型的值不相等"); } }
12.两个字符串不区分大小写进行比较
@Test public void toEqualsIgnoreCase(){ String str1 = "MILOgenius"; if (str.equalsIgnoreCase(str1)) { System.out.println("两个字符串不区分大小写的类型相等"); } }
13.将一个制定的字符串替换成其他的字符串
@Test public void toReplaceAll(){ String newStr = str.replaceAll("o", "new"); System.out.println(newStr); }
14.将制定字符串拼接到该字符串结尾
@Test public void toConcat(){ String str1 = "-好帅的名字!!"; System.out.println(str.concat(str1)); }
全部代码:
package com.milogenius.string; import org.junit.Test; /** * * <p>Title:String常用方法</p> * @author lixb * @date 2017年12月29日下午4:42:16 */ public class StringTest { public String str = "milogenius"; /** * 1.将字符串变为一个字符数组 */ @Test public void toCharArray(){ char[] arr = str.toCharArray(); for (int i = 0; i < arr.length; i++) { System.out.println("字符数组为:"+arr[i]); } } /** * 2.从字符串中取出指定位置的字符 */ @Test public void toCharAt(){ char c = str.charAt(2); System.out.println("字符串制定位置的字符为:"+c); } /** * 3.将字符串变成一个byte数组 */ @Test public void toByte(){ byte arr[] = str.getBytes(); /* for (int j = 0; j < arr.length; j++) { System.out.println("Byte数组为:"+arr[j]); }*/ System.out.println("Byte数组为:"+new String(arr)); } /** * 4.获取字符串长度 */ @Test public void getLength(){ int length = str.length(); System.out.println("字符串的长度为:"+length); } /** * 5.查找一个制定的字符串是否存在,返回的是字符串的位置;如果不存在,返回-1 */ @Test public void toIndexOf(){ int a = str.indexOf("i"); int b = str.indexOf("o", 2); System.out.println(a); System.out.println(b); } /** * 6.去除字符串两头的空格 */ @Test public void toTrim(){ String str = " hello "; System.out.println("字符串去除两头空格结果:"+str.trim()); } /** * 7.字符串截取 */ @Test public void toSubstring(){ //截取0-3位置的内容(包头不包尾) System.out.println(str.substring(0, 3)); //从第三个位置开始截取(包括起始位置) System.out.println(str.substring(2)); } /** * 8.按照指定的字符拆分字符串,拆分的数据将以字符串数组的形式返回 */ @Test public void toSplit(){ String[] s = str.split("o"); for (int i = 0; i < s.length; i++) { System.out.println("拆分后的字符串结果:"+s[i]); } } /** * 9.将字符串进行大小写转换 */ @Test public void toChange(){ System.out.println("将字符串转换为大写:"+str.toUpperCase()); String str = "MILOGENIUS"; System.out.println("将字符串转换为小写:"+str.toLowerCase()); } /** * 10.判断是否以指定的字符串开头或者结尾 */ @Test public void toStartWithOrEndWith(){ if (str.startsWith("mi")) { System.out.println("字符串是以mi开头的"); } if (str.endsWith("s")) { System.out.println("字符串是以s结尾的"); } } /** * 11.String类型比较 */ @Test public void toEquals(){ String str1 = "milogenius"; if (str.equals(str1)) { System.out.println("两个字符串类型的值相等"); } else { System.out.println("两个字符串类型的值不相等"); } } /** * 12.两个字符串不区分大小写进行比较 */ @Test public void toEqualsIgnoreCase(){ String str1 = "MILOgenius"; if (str.equalsIgnoreCase(str1)) { System.out.println("两个字符串不区分大小写的类型相等"); } } /** * 13.将一个制定的字符串替换成其他的字符串 */ @Test public void toReplaceAll(){ String newStr = str.replaceAll("o", "new"); System.out.println(newStr); } /** * 14.将制定字符串拼接到该字符串结尾 */ @Test public void toConcat(){ String str1 = "-好帅的名字!!"; System.out.println(str.concat(str1)); } }
相关文章推荐
- String类以及常用的方法
- String类常用的成员方法length()和charAt()
- String类常用方法1
- String类中常用方法源码解析
- Java String类的常用方法(字符串分割)
- String类常用方法之charAt()、codePointAt()示例
- java例程练习(String类的一些常用方法)
- String类对象的常用操作及方法
- Java String类的常用方法
- Java-String类的常用方法总结
- String类的常用方法
- 12-6java面向对象之String类常用方法的总结
- [Java]String类常用方法
- string类的常用属性和方法
- String类常用方法总结
- java中String类的常用方法
- Java String类的常用方法(字符串替换)
- String类中常用方法汇总整理(常用API)
- String类的常用方法总结
- java例程练习(String类的一些常用方法)