String的常用方法演示
package day09;
import org.junit.jupiter.api.Test;
/**
* java基础:String类
* Author:知而无涯
* Description:String类常用方法
* Date: 2017-10-31 11:04
*/
public class StringDemo04 {
//1:String类的判断功能
/**
* boolean equals(Object obj) 比较两个字符串的内容是否相同,区分大小写
* //boolean equalsIgnoreCase(String str) 比较两个字符串,不区分大小写
* //boolean contains(String str) 判断大字符串是否包含小字符串,必须连在一起哦
* // boolean startsWith(String str) 判断字符串是否以某个字符开头
* //boolean endsWith(String str) 判断字符串是否以某个字符结尾
* // boolean isEmpty() 判断字符串为空
*/
@Test
public void Test1() {
String s1 = "helloworld";
String s2 = "HelloWorld";
String s3 = "helloworld";
long starTime = System.currentTimeMillis();
System.out.println(starTime);
System.out.println("equals方法:" + s1.equals(s2)); //false
System.out.println("equals方法:" + s1.equals(s3)); //true
System.out.println("*******************");
System.out.println("equalsIgnoreCase忽视大小写:" + s1.equalsIgnoreCase(s2));//true
System.out.println("*******************");
System.out.println("contains大小字符串是否包含小字符串:" + s1.contains("hello"));//true
System.out.println("contains大小字符串是否包含小字符串:" + s1.contains("ho"));//flase
System.out.println("*******************");
System.out.println("startsWith是否以某个字母开头:" + s2.startsWith("H"));//true
System.out.println("startsWith是否以某个字母开头:" + s2.startsWith("Hello"));//true
System.out.println("*******************");
System.out.println("endsWith判断是否以某个字符结尾:" + s3.endsWith("d"));
System.out.println("endsWith判断是否以某个字符结尾:" + s3.endsWith("world"));
System.out.println("*******************");
System.out.println("isEmpty判断字符串是否为空:" + s3.isEmpty());
String s4 = "";
String s5 = null;
System.out.println("isEmpty判断字符串是否为空:" + s4.isEmpty());//true
// System.out.println("isEmpty判断字符串是否为空:"+s5.isEmpty());//java.lang.NullPointerException
long endTime = System.currentTimeMillis();
System.out.println(endTime);
long Time = endTime - starTime;
System.out.println(Time);
}
//2:string类的获取功能
/**
* int length() //获取字符串的长度
* char charAt(int index) //获取指定索引位置的字符
* int indexOf(int ch) //返回指定字符在此字符串中第一次出现处的索引
* 为什么是int而不是char哩,因为'a'和97都可以代表a
* int indexOf(String str) //返回指定字符串在此字符串中第一次出现的索引
* int indexOf(int ch,int fromIndex) //返回指定字符在此字符中从指定位置最后一次出现处的索引
* int indexOf(String str,int fromIndex) //返回指定字符串在此字符串中从指定位置后第一次出现的索引
* String substring(int beginIndex) //从指定位置开始截取字符串,默认到末尾
* String substring(int start,int endIndex) //从指定位置开始到指定位置结束截取字符串
*/
@Test
public void Test2() {
String s = "yangjishun20171031yang";
//int length() 获取字符串的长度
System.out.println("s.length()=" + s.length()); //22
//char charAt(int index) 获取指定索引位置的字符
System.out.println("s.charAt(5)=" + s.charAt(5)); //i
//int indexOf(int ch) 返回指定字符在此字符串中第一次出现处的索引
System.out.println("s.indexOf(3)=" + s.indexOf("0")); //11
// int indexOf(String str) 返回指定字符串在此字符串中第一次出现的索引
System.out.println("s.indexOf(3)=" + s.indexOf("2")); //10
// int indexOf(int ch,int fromIndex) 返回指定字符在此字符中从指定位置第一次出现处的索引
System.out.println("s.indexOf(1,5)=" + s.indexOf('1', 5)); //12
//int indexOf(String str,int fromIndex) 返回指定字符串在此字符串中从指定位置后第一次出现的索引
System.out.println("s.indexOf(5)=" + s.indexOf("y", 5)); //18
// String substring(int beginIndex) 从指定位置开始截取字符串,默认到末尾
System.out.println("s.substring(5)=" + s.substring(15)); //031yang
// String substring(int start,int endIndex) 从指定位置开始到指定位置结束截取字符串
System.out.println("s.substring(3,5)=" + s.substring(3, 5)); //gj
}
//字符串的转换功能
/**
* byte[] getBytes() 把字符串转换为字节数组
* char[] toCharArray() 把字符串转换为字符数组
* static String valueOf(char[] chs) 把字符数组转换成字符串
* static String valueOf(int i) 把int类型的数据转换成字符串
* 注意:String类的valueOf方法可以把任意类型的数据转成字符串
* String toLowerCase() 把字符串转成小写
* String toUpperCase() 把字符串转成大写
* String concat(String str) 拼接字符串
*/
@Test
public void Test3() {
String s = "woaijava";
//byte[] getBytes() 把字符串转换为字节数组
byte[] bytes = s.getBytes();
System.out.println(bytes);//输出的只是地址值,需要遍历
System.out.println("测试1:byte[] getBytes() 把字符串转换为字节数组");
System.out.print("[");
for (int i = 0; i < bytes.length; i++) {
// byte aByte = bytes[i];
if (i == bytes.length - 1) {
System.out.print(bytes[i]);
} else {
System.out.print(bytes[i] + ",");
}
}
System.out.println("]"); //[119,111,97,105,106,97,118,97],输出的是ASILL码值
System.out.println("测试2:char[] toCharArray() 把字符串转换为字符数组");
//char[] toCharArray() 把字符串转换为字符数组
System.out.print("[");
char[] chars = s.toCharArray();
for (int i = 0; i < chars.length; i++) {
char aChar = chars[i];
if (i==chars.length-1){
System.out.print(aChar);
}else{
System.out.print(aChar+",");
}
}
System.out.print("]"); //[w,o,a,i,j,a,v,a]
System.out.println();
//static String valueOf(char[] chs) 把字符数组转换成字符串
String s1=s.valueOf(chars);
System.out.println("测试3:static String valueOf(char[] chs)把字符数组转换成字符串");
System.out.println(s1); //woaijava
//static String valueOf(int i) 把int类型的数据转换成字符串
int a=100;
String s3=String.valueOf(a);
//String toLowerCase() 把字符串转成小写
String s4="ABCDEFGH";
System.out.println("s4.toLowerCase()="+s4.toLowerCase()); //abcdefgh
//String toUpperCase() 把字符串转成大写
String s5="abcde";
System.out.println("s5.toUpperCase()="+s5.toUpperCase()); //ABCDE
//String concat(String str) 拼接字符串
String s6="abc";
String s7="def";
System.out.println("s6.concat(s7)="+s6.concat(s7)); //abcdef
}
//字符串的替换功能
/*
String replace(char old,char new)
String replace(String old,String new)
*/
@Test
public void Test4(){
String s8="helloworld";
// String replace(char old,char new)
String s9=s8.replace('l','A');
System.out.println("s8.replace('l','A')="+s9); //heAAoworAd
//String replace(String old,String new)
String s10=s8.replaceFirst("hello","java"); //javaworld
System.out.println(s10);
}
//去除字符串两边的空格 String trim()
@Test
public void Test5(){
String s11=" woaijava ";
String s12=s11.trim();
System.out.println(s11); // woaijava
System.out.println(s12); //woaijava
}
//字符串的遍历 concat
@Test
public void Test6(){
String s13="javapythonphp";
for (int i = 0; i < s13.length(); i++) {
char chars= s13.charAt(i); //通过索引遍历得到每一个字符
System.out.print(chars+"\t");//j a v a p y t h o n p h p
}
}
}
还没有评论,来说两句吧...