String类型总结
String类适用于描述字符串事物。 那么它就提供了多个方法对字符串进行操作。
常见的操作有哪些? "abcd"
1,获取。
1.1 字符串中的包含的字符数,也就是字符串的长度。
int length():获取长度。
1.2 根据位置获取位置上某个字符。
char charAt(int index):
1.3 根据字符获取该字符在字符串中位置。
int indexOf(int ch):返回的是ch在字符串中第一次出现的位置。
int indexOf(int ch, int fromIndex) :从fromIndex指定位置开始,获取ch在字符串中出现的位置。
int indexOf(String str):返回的是str在字符串中第一次出现的位置。
int indexOf(String str, int fromIndex) :从fromIndex指定位置开始,获取str在字符串中出现的位置。
int lastIndexOf(int ch) :从字符的最后一个算起读取字符ch的位置
1 package com.itheima.day13; 2 3 public class StringMethodDemo { 4 5 //定义一个方法打印值 6 public static void sop(Object obj){ 7 System.out.println(obj); 8 } 9 10 public static void main(String[] args) { 11 //定义一个字符串 12 String str="abbcd"; 13 sop(str.length()); 14 //根据位置获取位置上某个字符 15 sop(str.charAt(2)); 16 //根据字符获取该字符在字符串中位置。 17 sop(str.indexOf("d")); 18 //从fromIndex指定位置开始,获取ch在字符串中出现的位置。 19 sop(str.indexOf("d",2)); 20 //返回的是str在字符串中第一次出现的位置。 21 sop(str.indexOf("bb")); 22 //从fromIndex指定位置开始,获取str在字符串中出现的位置。 23 sop(str.indexOf("bb", 0)); 24 //获取从最后一个字符算起第一次出现的位置 25 sop(str.lastIndexOf('c')); 26 27 } 28 29 }
2,判断。
2.1 字符串中是否包含某一个子串。
boolean contains(str):
特殊之处:indexOf(str):可以索引str第一次出现位置,如果返回-1.表示该str不在字符串中存在。
所以,也可以用于对指定判断是否包含。 if(str.indexOf("aa")!=-1)
而且该方法即可以判断,有可以获取出现的位置。
2.2 字符中是否有内容。
boolean isEmpty(): 原理就是判断长度是否为0.
2.3 字符串是否是以指定内容开头。
boolean startsWith(str);
2.4 字符串是否是以指定内容结尾。
boolean endsWith(str);
2.5 判断字符串内容是否相同。复写了Object类中的equals方法。
boolean equals(str);
2.6 判断内容是否相同,并忽略大小写。
boolean equalsIgnoreCase();
1 package com.itheima.day13; 2 3 public class StringMethodDemo2 { 4 //定义一个打印的方法 5 public static void sop(Object obj){ 6 System.out.println(obj); 7 } 8 public static void main(String[] args) { 9 //定义一个字符串 10 String str="abbddcc"; 11 //字符串中是否包含某一个子串。 12 sop(str.contains("dd")); 13 //判断字符串中是否有内容 14 sop(str.isEmpty()); 15 //字符串是否是以指定内容开头。 16 sop(str.startsWith("abb")); 17 //字符串是否是以指定内容结尾。 18 sop(str.endsWith("cc")); 19 sop(str.equalsIgnoreCase("abbddcc")); 20 21 22 } 23 24 }
3,转换。
3.1 将字符数组转成字符串。
构造函数:String(char[])
String(char[],offset,count):将字符数组中的一部分转成字符串。
静态方法: static String copyValueOf(char[]);
static String copyValueOf(char[] data, int offset, int count)
static String valueOf(char[]):
3.2 将字符串转成字符数组。**
char[] toCharArray():
3.3 将字节数组转成字符串。
String(byte[])
String(byte[],offset,count):将字节数组中的一部分转成字符串。
3.4 将字符串转成字节数组。
byte[] getBytes():
3.5 将基本数据类型转成字符串。
static String valueOf(int)
static String valueOf(double)
//3+"";//String.valueOf(3);
特殊:字符串和字节数组在转换过程中,是可以指定编码表的。
package com.itheima.day13; public class StringMenthodDemo3 { //定义打印方法 public static void sop(Object obj){ System.out.println(obj); } //定义一个Menthod_get方法 实现转化 public static void Menthod_get(){ //定义一个字符串数组 char[] cn={'a','b','c','d','e'}; //将字符数组转成字符串 String str=new String(cn); //将字符数组中的一部分转成字符串 String str1=new String(cn,1,2); sop(str1); //静态方法 sop(str.copyValueOf(cn)); sop(str.copyValueOf(cn, 1, 2)); //将字符串转成字符数组 //定义一个字符串 String str2="ffsevd"; char[] cha=str2.toCharArray(); for(int x=0;x<str2.length();x++){ sop("char["+cha[x]+"]"); } // 将基本数据类型转成字符串。 sop(str2.valueOf(1234)); } public static void main(String[] args) { Menthod_get(); } }
4,替换
String replace(oldchar,newchar);
1 package com.itheima.day13; 2 3 public class StringMenthodDemo4 { 4 5 //定义一个打印方法 6 public static void sop(Object obj){ 7 System.out.println(obj); 8 } 9 //定义一个Menthod_conversion方法实现转换 10 public static void Menthod_conversion(){ 11 //定义一个字符串 12 String str="abdcede"; 13 sop(str.replace("ab", "dc")); 14 } 15 public static void main(String[] args) { 16 Menthod_conversion(); 17 } 18 19 }
5,切割
String[] split(regex);
1 package com.itheima.day13; 2 3 public class StringMenthodDemo5 { 4 5 /** 6 * @author zhangxuedong 7 */ 8 //定义一个打印方法 9 public static void sop(Object obj){ 10 System.out.println(obj); 11 } 12 //定义一个方法实现切割 13 public static void Menthod_split(){ 14 String str="zhangsan,lisi,wangwu"; 15 String[] ch=str.split(","); 16 for(int x=0;x<ch.length;x++){ 17 sop(ch[x]); 18 } 19 } 20 public static void main(String[] args) { 21 Menthod_split(); 22 } 23 24 }
6,子串。获取字符串中的一部分。
String substring(begin);
String substring(begin,end);
1 package com.itheima.day13; 2 3 public class StringMenthodDemo6 { 4 5 public static void sop(Object obj){ 6 System.out.println(obj); 7 } 8 public static void Menthod_get(){ 9 String str="abcdefg"; 10 sop(str.substring(2)); 11 sop(str.substring(1, 3)); 12 } 13 public static void main(String[] args) { 14 Menthod_get(); 15 16 } 17 18 }
7,转换,去除空格,比较。
7.1 将字符串转成大写或则小写。
String toUpperCase();
String toLowerCase();
7.2 将字符串两端的多个空格去除。
String trim();
7.3 对两个字符串进行自然顺序的比较。
int compareTo(string);
1 package com.itheima.day13; 2 3 public class StringMenthodDemo7 { 4 5 /** 7.1 将字符串转成大写或则小写。 6 7 String toUpperCase(); 8 9 String toLowerCase(); 10 11 7.2 将字符串两端的多个空格去除。 12 13 String trim(); 14 15 7.3 对两个字符串进行自然顺序的比较。 16 17 int compareTo(string); 18 19 */ 20 public static void sop(Object obj){ 21 System.out.println(obj); 22 23 } 24 public static void Menthod_7(){ 25 String str=" Welcom TO "; 26 //将字符串转成大写或则小写。 27 sop(str.toUpperCase()); 28 sop(str.toLowerCase()); 29 //将字符串两端的多个空格去除。 30 sop(str.trim()); 31 //对两个字符串进行自然顺序的比较 32 sop(str.compareToIgnoreCase("acc")); 33 34 } 35 public static void main(String[] args) { 36 Menthod_7(); 37 38 } 39 40 }