您的位置:首页 > 编程语言 > Java开发

[Java]String类常用方法

2016-04-12 16:24 597 查看
字符串与字符数组的转换

toCharArray()<—>String(char[] c)..

public class StringAPIDemo01{
public static void main(String args[]){
String str1="hello";
char c[]=str1.toCharArray();//字符串变字符数组
for(int i=0;i<c.length;i++){
System.out.print(c[i]+"\t");
}

System.out.println();
String str2=new String(c);//字符数组变字符串
String str3=new String(c,0,3);
System.out.println(str2);
System.out.println(str3);
}
}




从字符串中取出指定位置的字符

charAt()

public class StringAPIDemo02{
public static void main(String args[]){
String str1="hello";
System.out.println(str1.charAt(1));//取出第2个字符
}
}




字符串与byte数组转换

getBytes()<—>String(byte[] b)..

public class StringAPIDemo03{
public static void main(String args[]){
String str1="hello";
byte b[]=str1.getBytes();//String-->byte[]
for(int i=0;i<b.length;i++){
System.out.print(b[i]+"\t");
}
System.out.println(new String(b));//byte[]-->String
System.out.println(new String(b,1,3));
}
}




取得字符串长度

length()

public class StringAPIDemo04{
public static void main(String args[]){
String str1="hello Lixinghua";
System.out.println("\""+str1+"\"的长度为:"+str1.length());
}
}




查找指定字符串是否存在

indexOf()

public class StringAPIDemo05{
public static void main(String args[]){
String str1="abcdefgcgh";
System.out.println(str1.indexOf("c"));//返回查找位置
System.out.println(str1.indexOf("c",3));//从第4位开始找
System.out.println(str1.indexOf("x"));//未找到返回-1
}
}




去掉左右空格

trim()

public class StringAPIDemo06{
public static void main(String args[]){
String str1="   hell o    ";
System.out.println(str1.trim());//去掉左右空格
}
}




字符串截取

substring()..

public class StringAPIDemo07{
public static void main(String args[]){
String str1="hello world";
System.out.println(str1.substring(6));//从第7位开始截取
System.out.println(str1.substring(0,5));//截取0-5
}
}




按照指定的字符串拆分字符串

split()

public class StringAPIDemo08{
public static void main(String args[]){
String str1="hello world";
String s[]=str1.split(" "|"\t");//按空格或制表符拆分
for(String str:s){
System.out.println(str);
}
}
}




字符串的大小写转换

toUpperCase()<—>toLowerCase()

public class StringAPIDemo09{
public static void main(String args[]){
System.out.println("将\"hello world\"转成大写:"+"hello world".toUpperCase());
System.out.println("将\"HELLO WORLD\"转成小写:"+"HELLO WORLD".toLowerCase());
}
}




判断是否以指定的字符串开头或结尾

startsWith()<—>endsWith()

public class StringAPIDemo10{
public static void main(String args[]){
String str1="**HELLO";
String str2="Hello**";
if(str1.startsWith("**")){//判断是否以**开头
System.out.println("(**HELLO)以**开头");
}
if(str2.endsWith("**")){//判断是否以**结尾
System.out.println("(Hello**)以**结尾");
}
}
}




不区分大小写进行字符串比较

equalsIgnoreCase()

public class StringAPIDemo11{
public static void main(String args[]){
String str1="HELLO";
String str2="hello";
System.out.println("\"HELLO\" equals \"hello\""+str1.equals(str2));//区分大小写比较
System.out.println("\"HELLO\" equalsIgnoreCase \"hello\""+str1.equalsIgnoreCase(str2));//不区分大小写比较
}
}




将指定字符串替换成其他字符串(注意:字符串的内容一旦声明则不可改变

可使用replaceAll(“xx”,”[这里无空格]”)清除某字符。

replaceAll()

public class StringAPIDemo12{
public static void main(String args[]){
String str="hello";
//str.replaceAll("l","x");
//System.out.println(str);//**仍然输出“hello”**
String newStr=str.replaceAll("l","x");//将所有l换成x
System.out.println("替换之后的结果为:"+newStr);
}
}




从头开始查找指定的字符串位置,返回值不为-1表示找到了查询内容。

int indexOf(String str)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: