黑马程序员——Java基础--常见对象总结(二)

-----------android培训java培训、java学习型技术博客、期待与您交流!------------

第二讲 常见使用方法及介绍

3、String类的判断功能及案例演示

我就总结了几个常用的:

package cn.itcast.stringmethod;
/*
 * String类的判断功能
		public boolean equals(Object obj):				比较字符串的内容是否相同,区分大小写
		public boolean equalsIgnoreCase(String str):	比较字符串的内容是否相同,忽略大小写
		public boolean contains(String str):			判断字符串中是否包含传递进来的字符串
		public boolean startsWith(String str):			判断字符串是否以传递进来的字符串开头
		public boolean endsWith(String str):			判断字符串是否以传递进来的字符串结尾
		public boolean isEmpty():						判断字符串的内容是否为空。
 */
public class StringDemo {
	public static void main(String[] args) {
		String s = "helloworld";//创建对象
		String s1 = "helloworld";//创建对象
		boolean b = s.equals(s1);//比较字符串的内容是否相同,区分大小写
		System.out.println(b);
		System.out.println("------------------");
		boolean b1 = s.equalsIgnoreCase("HelloWorld");//比较字符串的内容是否相同,忽略大小写
		System.out.println(b1);
		System.out.println("------------------");
		boolean b2 = s.contains("hello");//判断字符串中是否包含传递进来的字符串
		System.out.println(b2);
		System.out.println("------------------");
		System.out.println(s.startsWith("he"));//判断字符串是否以传递进来的字符串结尾
		System.out.println("------------------");
		System.out.println(s.endsWith("d"));//判断字符串是否以传递进来的字符串结尾
		System.out.println("------------------");
		System.out.println(s.isEmpty());
	}
}

结果为:

true
------------------
true
------------------
true
------------------
true
------------------
true
------------------
false
4、String类的获取功能及案例演示

也是常见的几个

package cn.itcast.stringmethod;
/*
 * String类的获取功能
		public int length():							获取字符串的长度。
		public char charAt(int index):					获取指定索引位置的字符
		public int indexOf(int ch):						返回指定字符在此字符串中第一次出现处的索引。
		public int indexOf(String str):					返回指定字符串在此字符串中第一次出现处的索引。
		public int indexOf(int ch,int fromIndex):		返回指定字符在此字符串中从指定位置后第一次出现处的索引。
		public int indexOf(String str,int fromIndex):	返回指定字符串在此字符串中从指定位置后第一次出现处的索引。
			可以顺带提一下lastIndexOf系列
		public String substring(int start):				从指定位置开始截取字符串,默认到末尾。
		public String substring(int start,int end):		从指定位置开始到指定位置结束截取字符串。
 */
public class StringDemo2 {
	public static void main(String[] args) {
		String s = "helloworld";//创建字符串对象
		System.out.println("字符串的长度:"+s.length());//获取字符串的长度
		System.out.println("----------------------");
		System.out.println("指定索引位置的字符:"+s.charAt(2));//获取指定索引位置的字符
		System.out.println("----------------------");
		System.out.println("索引:"+s.indexOf("l"));//返回指定字符在此字符串中第一次出现处的索引
		System.out.println("----------------------");
		System.out.println("索引:"+s.indexOf("world"));//返回指定字符串在此字符串中第一次出现处的索引
		System.out.println("----------------------");
		System.out.println("索引:"+s.indexOf("w", 8));//返回指定字符在此字符串中从指定位置后第一次出现处的索引
		System.out.println("----------------------");
		System.out.println("索引:"+s.indexOf("world", 8));//返回指定字符串在此字符串中从指定位置后第一次出现处的索引
		System.out.println("----------------------");
		System.out.println("新字符串:"+s.substring(5));//从指定位置开始截取字符串,默认到末尾
		System.out.println("----------------------");
		System.out.println("新字符串:"+s.substring(5, 6));//从指定位置开始到指定位置结束截取字符串
	}
}

结果为:

字符串的长度:10
----------------------
指定索引位置的字符:l
----------------------
索引:2
----------------------
索引:5
----------------------
索引:-1
----------------------
索引:-1
----------------------
新字符串:world
----------------------
新字符串:w
5、String的转换功能及案例演示

package cn.itcast.stringmethod;
/*
 * String的转换功能:
		public byte[] getBytes():						把字符串转换为字节数组。
		public char[] toCharArray():					把字符串转换为字符数组。
		public static String valueOf(char[] chs):		把字符数组转成字符串。
		public static String valueOf(int i):			把int类型的数据转成字符串。
			注意:String类的valueOf方法可以把任意类型的数据转成字符串。
		public String toLowerCase():					把字符串转成小写。
		public String toUpperCase():					把字符串转成大写。
		public String concat(String str):				把字符串拼接。
 */
public class StringDemo3 {
	public static void main(String[] args) {
		String s = "abcd";//创建对象
		byte[] bytes = s.getBytes();//把字符串转换为字节数组
		// 遍历
		for (int i = 0; i < bytes.length; i++) {
			System.out.print(bytes[i]+" ");
		}
		System.out.println();
		char[] charArray = s.toCharArray();//把字符串转换为字符数组
		// 遍历
		for (int i = 0; i < charArray.length; i++) {
			System.out.print(charArray[i]+" ");
		}
		System.out.println();
		//定义一个字符数组
		char[] ch = {'a' , 'b' , 'c'};
		String valueOf = String.valueOf(ch);//把字符数组转成字符串
		System.out.println(valueOf);
		System.out.println(String.valueOf(98));//把int类型的数据转成字符串
		String s1 = "AbCd";
		System.out.println(s1.toLowerCase());//把字符串转成小写
		System.out.println(s1.toUpperCase());//把字符串转成大写
		System.out.println(s1.concat("woaini"));//把字符串拼接
	}
}


结果为:

97 98 99 100
a b c d
abc
98
abcd
ABCD
AbCdwoaini

6、String的替换功能及案例演示

package cn.itcast.stringmethod;
/*
 * String的替换功能及案例演示
		public String replace(char old,char new)			将指定字符进行互换
		public String replace(String old,String new)		将指定字符串进行互换
		public String trim()								去除两端空格
		public int compareTo(String str)					按字典顺序比较两个字符串
		public int compareToIgnoreCase(String str)			按字典顺序比较两个字符串,不区分大小写
 */
public class StringDemo4 {
	public static void main(String[] args) {
		//定义一个字符串
		String s = "abcde";
		String replace = s.replace('a','A');//将指定字符进行互换
		System.out.println(replace);
		String replace2 = s.replace("ab", "cd");
		System.out.println(replace2);
		String s1 = " a  bc d e ";
		System.out.println(s1.trim());//去除两端空格
		String s2 = "abcd";
		String s3 = "abcd";
		System.out.println(s2.compareTo(s3));//按字典顺序比较两个字符串
		String s4 = "Abcd";
		System.out.println(s2.compareToIgnoreCase(s4));//按字典顺序比较两个字符串,不区分大小写
	}
}


结果为:

Abcde
cdcde
a  bc d e
0
0

-----------android培训java培训、java学习型技术博客、期待与您交流!------------

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值