您的位置:首页 > 职场人生

黑马程序员_11_String类的常用方法

2014-01-13 18:39 453 查看
 

----------------------
ASP.Net+Android+IOS开发、.Net培训、期待与您交流! ----------------------

 

***********************************************************1、获取*****************************************************************
1、获取
1.1、获取字符串的中字符的个数,即字符串的长度。
int length():
注意:不要和数组中的length属性混淆
1.2、根据位置获取位置上的字符
char charAt(int index)  返回指定索引处的 char 值。
1.3、根据字符获取字符在字符串中的位置
int indexOf(int ch):返回指定字符ch在此字符串中第一次出现的位置
int indexOf(int ch,int fromIndex):返回指定字符ch在此字符串中最后一次出现的位置
从指定索引位置开始

int indexOf(String str):返回指定字符串str在此字符串中最后一次出现的位置
int indexOf(String str,int fromIndex):返回指定字符串str在此字符串中最后一次出现的位置,
从指定索引位置开始

int lastIndexOf(int ch):返回指定字符ch在此字符串中最后一次出现的位置
int lastIndexOf(int ch,int fromIndex):返回指定字符ch在此字符串中最后一次出现的位置
从指定索引位置开始

int lastIndexOf(String str):返回指定字符串str在此字符串中最后一次出现的位置
int lastIndexOf(String str,int fromIndex):返回指定字符串str在此字符串中最后一次出现的位置,
从指定索引位置开始

public class StringDes130201{
public static void main(String[] args){
method_get();
}
public static void method_get(){
String s1="abcdeabcdfghggf";
sop(s1.length());
sop(s1.charAt(2));
sop(s1.indexOf(97));
sop(s1.indexOf(97,2));
sop(s1.indexOf("bc"));
sop(s1.indexOf("bc",2));
}
public static void sop(Object obj){
System.out.println(obj);
}
}




********************************************************************************************************************************************************************************

2、判断
2.1、判断字符串中是否包含某一个子串
boolean contains(String str):
特殊之处:indexOf(str)也可是根据返回值来判断是否包含子串str,如果返回值是-1,表示不包含;否则,表示包含。
2.2、判断字符串是否有内容
boolean isEmpty():原理就是判断长度是否为0

2.3、判断字符串是否以指定内容开始
boolean startsWith(String str);
2.4、判断字符串是否以指定内容结束
boolean endsWith(String str);

2.5、判断字符串内容是否相同,覆盖了Objecet类中的equals方法。
boolean equals(String str);
2.6、判断字符串的内容是否相同,忽略大小写
boolean equalsIgnoreCase(String str);

public class StringDes130201{
public static void main(String[] args){
method_is();
}
public static void method_is(){
String s="StringDemo.java";
sop(s.startsWith("String"));
sop(s.endsWith(".java"));
sop(s.contains("Demo"));
}
public static void sop(Object obj){
System.out.println(obj);
}
}
运行结果:



************************************************************************************************************************************************************************************

3、转换
3.1、将字符数组转换成字符串
构造函数:String(char[] ch);
String(char[] ch,int offset,int count);将字符数组中的一部分转换成字符串
静态方法 static copyValueOf(char[] ch);
static copeValueOf(char[] ch,int offset,int count);

static valueOf(char[] ch);

3.2、将字符串转换成字符数组
char[] toCharArray();

3.3、将字节数组转换成字符串
构造函数:String(byte[] );
String(byte[] bytes, int offset, int length) ;将字节数组中的一部分转换成字符串
3.4、将字符串转换成字节数组
btye[] getBytes();

3.5、将基本数据类型转换成字符串
static String valueOf(int);
static String valueOf(double);
static String valueOf(boolean);
......

public class StringDes130201{
public static void main(String[] args){
method_trans();
}
public static void method_trans(){
char[] chs={'a','b','c','d','e','f'};
String s=new String(chs,2,4);
sop("s="+s);

String s2="heiMaWoAiNi";
char[] ch2=s2.toCharArray();
for(char c:ch2){
sop("c="+c);
}
}
public static void sop(Object obj){
System.out.println(obj);
}
}

运行结果:



********************************************************************************************************************************************************************************
4、替换
String replace(char oldChar, char newChar) // 当被替换的字符不存在时,返回的是原字符串
String replace(CharSequence target, CharSequence replacement)
public class StringDes130201{
public static void main(String[] args){
method_replace();
}
public static void method_replace(){
String s="hello java";
String s2=s.replace('a','b');
sop("s2"+s2);
String s3=s.replace("java","word");
sop("s3"+s3);
}
public static void sop(Object obj){
System.out.println(obj);
}
}

运行结果:



********************************************************************************************************************************************************************************

5、切割
String[] split(String regex)

public class StringDes130201{
public static void main(String[] args){
method_split();
}
public static void method_split(){
String s="com。heima。hibernate。user。model";
String[] arr=s.split("。");
for(int i=0;i<arr.length;i++){
System.out.println("arr="+arr[i]);
}
}
public static void sop(Object obj){
System.out.println(obj);
}
}

运行结果:



******************************************************************************************************************************************************************************

6、子串
String substring(int beginIndex);	 //返回的是从beginIndex开始到结束的字符串,
//也可以用下面的方法完成此功能:subString(beginIndex,str.length);
String substring(int beginIndex,int endIndex);	//返回的是  包括头、不包括尾的字符串

public class StringDes130201{
public static void main(String[] args){
//method_substring();
}
public static void method_substring(){
String s="abcdefg";
String s2=s.substring(2);
String s3=s.substring(2,4);
sop("s2="+s2);
sop("s3="+s3);
}
public static void sop(Object obj){
System.out.println(obj);
}
}

运行结果:



******************************************************************************************************************************************************************************

7、转换、去除空格、比较
7.1将字符串转换成全大写或全小写
String toUpperCase();
String toLowerCase();
7.2、将字符串两端的空格去掉
String trim();
7.3、对两个字符串进行自然顺序的比较
int compareTo(String str);
String toLowerCase();
int compareToIgnoreCase(String str) ==>
boolean equals(String str);

public class StringDes130201{
public static void main(String[] args){
meethod_7();
}
public static void meethod_7(){
String s="   hello world   ";
String s2="abc";
String s3="acc";
String s4="aaa";
sop("s.toUpperCase()=="+s.toUpperCase());
sop("s.toLowerCase()=="+s.toLowerCase());
sop("s.trim()========="+s.trim());
sop("s2.compareTo(s4)="+s2.compareTo(s4));
sop("s3.compareTo(s4)="+s3.compareTo(s4));
}
public static void sop(Object obj){
System.out.println(obj);
}
}


运行结果:



----------------------
ASP.Net+Android+IOS开发、.Net培训、期待与您交流! ----------------------
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  string