[Java]String类常用方法

  • 字符串与字符数组的转换
    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);
}
}

run

  • 从字符串中取出指定位置的字符
    charAt()
public class StringAPIDemo02{
public static void main(String args[]){
String str1="hello";
System.out.println(str1.charAt(1));//取出第2个字符
}
}

run

  • 字符串与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));
}
}

run

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

run

  • 查找指定字符串是否存在
    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
}
}

run

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

run

  • 字符串截取
    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
}
}

run

  • 按照指定的字符串拆分字符串
    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);
}
}
} 

run

  • 字符串的大小写转换
    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());
}
}

run

  • 判断是否以指定的字符串开头或结尾
    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**)以**结尾");
}
}
}

run

  • 不区分大小写进行字符串比较
    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));//不区分大小写比较
}
}

run

  • 将指定字符串替换成其他字符串(注意:字符串的内容一旦声明则不可改变
    可使用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);
}
}

run

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

  • 格式化字符串。
    String.format(format,item1,item2,…)

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值