黑马程序员技术交流社区
标题:
字符串常用方法小结
[打印本页]
作者:
木木三
时间:
2015-9-20 23:01
标题:
字符串常用方法小结
字符串常用方法(做到能空手写操作代码)
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(int ch):返回的ch在字符串中第一次出现的位置。
int indexOf(String str): 返回的是str在字符串中第一次出现的位置。
int indexOf(String str, int fromIndex):从fromIndex指定位置开始,获取str在字符串中出现的位置。
int lastIndexOf(int ch):
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();
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);
特殊:字符串和字节数组在转换过程中,是可以指定编码表的。
4,替换
String replace(oldchar,newchar);
5,切割
String[] split(regex);
6,字串。获取字符串的一部分。
String substring(begin);
String substring(begin,end);
7,转换,去除空格,比较。
7.1 将字符串转成大写或小写。
String toUpperCase();
String toLowerCase();
7.2 将字符串两段的多个空格去除。
String trim();
7.3 将两个字符串进行自然顺序的比较。
int compareTo(string);
class StringMethod{
public static void main(String[] args){
//method_sub();
//method_split();
//method_replace();
//method_trans();
//method_is();
//method_get();
method_7();
}
public static void method_sub(){
String s = "abcdefg";
sop(s.substring(2));
sop(s.substring(2,4));
}
public static void method_split(){
String s = "zhangsan,lisi,wangwu,zhaoliu";
String[] arr = s.split(",");
printArray(arr);
}
public static void method_replace(){
String s = "Hello, world! Hello java! Hello python!";
String s1 = s.replace("Hello","Hola");
String s2 = s.replace("o","a");
sop("s1="+s1);
sop("s2="+s2);
}
public static void method_trans(){
char[] chs = {'a','b','c','d','e','f','g'};
String s = new String(chs);
sop(s);
char[] ch = s.toCharArray();
printArray(ch);
}
public static void method_is(){
String s = "ArrayDemo.java";
sop(s.startsWith("Array"));
sop(s.endsWith(".java"));
sop(s.contains("Demo"));
}
public static void method_get(){
String s = "abcdefgakjldfhne";
sop(s.length());
sop(s.charAt(4));
sop(s.indexOf('a'));
sop(s.indexOf('a',5));
sop(s.lastIndexOf('a'));
}
public static void method_7(){
String s = " HOLA, java! ";
sop(s.toUpperCase());
sop(s.toLowerCase());
sop(s.trim());
}
public static void printArray(String[] arr){
System.out.print("[");
for(int x=0; x<arr.length; x++){
if(x!=arr.length-1){
System.out.print(arr[x]+",");
}
else{
sop(arr[x]+"]");
}
}
}
public static void printArray(char[] arr){
System.out.print("[");
for(int x=0; x<arr.length; x++){
if(x!=arr.length-1){
System.out.print(arr[x]+",");
}
else{
sop(arr[x]+"]");
}
}
}
public static void sop(Object obj){
System.out.println(obj);
}
}
复制代码
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2