Java String类详解(二)

上一篇Java String类详解(一)讲了String类对象的两种实例化方式及其区别,字符串比较,以及String类的特点,我们接着来讲String类的常用方法。

一、字符串的常用方法 — 字符与字符串

很多编程语言利用了字符数组的概念来描述字符串的概念,在String类的方法上也有所体现。

1

一个例子:字符串和字符数组转换,完成一个小写字符串变为大写字符串的操作

public class StringDemo {
         public static void main(String args[]) {
                   String str = "helloworld" ;
                   char data [] = str.toCharArray() ;     // 字符串变为字符数组
                   for (int x = 0 ; x < data.length ; x ++) {
                            System.out.print(data[x] + "、") ;
                            data [x] -= 32 ;    // 变大写
                   }
                   System.out.println();
                   System.out.println("全部字符数组变为字符串:" + new String(data)) ;
                   System.out.println("部分字符数组变为字符串:" + new String(data,0,5)) ;
         }
}

运行结果:

h、e、l、l、o、w、o、r、l、d、
全部字符数组变为字符串:HELLOWORLD
部分字符数组变为字符串:HELLO

二、字符串的常用方法 — 字节与字符串

字节使用byte描述,字节一般用在数据的传输和进行编码转换的时候使用。String中也提供相应的方法,来进行数据传输和编码转换。

22

一个例子:完成一个小写字母变为大写字母的操作

public class StringDemo {
         public static void main(String args[]) {
                   String str = "helloworld" ;
                   byte data [] = str.getBytes() ;  // 字符串变为字节数组
                   for (int x = 0 ; x < data.length ; x ++) {
                            System.out.print(data[x] + "、") ;
                            data [x] -= 32 ;    // 变大写
                   }
                   System.out.println() ;
                   System.out.println("全部字节数组变为字符串:" + new String(data)) ;
                   System.out.println("部分字节数组变为字符串:" + new String(data,0,5)) ;
         }
}

运行结果:

104、101、108、108、111、119、111、114、108、100、
全部字节数组变为字符串:HELLOWORLD
部分字节数组变为字符串:HELLO

三、字符串的常用方法 — 字符串比较

3

一个例子:

public class StringDemo {
         public static void main(String args[]) {
                   String str1 = "helloworld" ;
                   String str2 = "HELLOWORLD" ;
                   System.out.println(str1.equals(str2)) ;
                   System.out.println(str1.equalsIgnoreCase(str2)) ;
         }
}

运行结果:

false
true

四、字符串的常用方法 — 字符串查找

4

一个例子:判断开头和结尾操作

public class StringDemo {
         public static void main(String args[]) {
                   String str = "**@@hello##" ;
                   System.out.println(str.startsWith("**")) ;
                   System.out.println(str.startsWith("@@",2)) ;
                   System.out.println(str.endsWith("##")) ;
         }
}

运行结果:

true
true
true

另外一个例子:使用contains()方法查找字符串是否存在,直接返回boolean,用于各种的执行判断

public class StringDemo {
         public static void main(String args[]) {
                   String str = "helloworld" ;
                   System.out.println(str.contains("hello")) ;
                   System.out.println(str.contains("xx")) ;
         }
}

运行结果:

true
false

五、字符串的常用方法 — 字符串替换

5

一个例子:

public class StringDemo {
         public static void main(String args[]) {
                   String str = "Hello World ." ;
                   System.out.println(str.replaceAll("l","_")) ;
                   System.out.println(str.replaceFirst("l","_")) ;
         }
}

运行结果:

He__o Wor_d .
He_lo World .

六、字符串的常用方法 — 字符串截取

6

例子:

public class StringDemo {
         public static void main(String args[]) {
                   String str = "Hello World ." ;
                   System.out.println(str.substring(6)) ;       
                   System.out.println(str.substring(0,5));    
         }
}

运行结果:

World .
Hello

七、字符串的常用方法 — 字符串拆分

7

例子:

public class StringDemo {
         public static void main(String args[]) {
                   String str = "Hello World !!!" ;
                   String result [] = str.split(" ") ;
                   for (int x = 0 ; x < result.length ; x ++) {
                            System.out.println(result[x]) ;
                   }
         }
}

运行结果:

Hello
World
!!!

八、字符串的常用方法 — 其他方法

8

例子1:取得字符串长度和是否为空

public class StringDemo {
         public static void main(String args[]) {
                   String str = "hello" ;
                   System.out.println(str.isEmpty()) ;
                   System.out.println("".isEmpty()) ;
                   System.out.println(str.length()) ;
                   System.out.println("    Hello     ".length()) ;       // 空格也计算
         }
}

运行结果:

false
true
5
14

例子2:

public class StringDemo {
         public static void main(String args[]) {
                   String str = "Hello World !~!!" ;
                   System.out.println(str.toUpperCase());
                   System.out.println(str.toLowerCase());
                   System.out.println("Hello ".concat("World ."));          // +也可以
         }
}

运行结果:

HELLO WORLD !~!!
hello world !~!!
Hello World .
  • 14
    点赞
  • 56
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值