黑马程序员——Java基础——集合(一)

------ Java培训、Android培训、iOS培训、.Net培训、期待与您交流! -------
String类:
package cn.fu._01string;
/**
 * String StringBuffer StringBuilder
 * String类的特点:
 * 字符串对象一旦被初始化就不会改变.
 */

public class StringDemo1 {
	public static void main(String[] args) {
		String s = "abc";
		String s1 = "abc";
		String s2 = "abc";
		String s3 = new String("abc");
		s = "nba";
		System.out.println("s =  "+s);
		//"abc"字符串对象并没有改变,只是引用变量s指向了新创建的字符串对象"nba".
		System.out.println(s1==s2);
		//字符串创建的时候,有一个字符串常量池,s1创建后,"abc"放入其中
		//s2创建时,只是把s1的"abc"引用赋给了s2,故s1==s2.
		System.out.println(s1==s3);
		//s1是一个对象,s2是一个对象,不是同一个对象地址不同,所以false;
		System.out.println(s1.equals(s3));
		//String类复写了Object中的equals方法,建立了Sring类最积极的判断字符串对象是否相同的依据,
		//只比较字符串内容,不比较地址.

	}

}
运行结果:
s =  nba
true
false
true
package cn.fu._01string;
/**
 * String类的构造函数
 * 构造函数:String(bytes[] bytes) 
 * 构造函数:String(char[] value)
 * 构造函数:String(char[] value,int offset,int count)
 */

public class StringDemo2 {

	public static void main(String[] args) {
		StringDemo2();
	}
	public static void StringDemo2(){
		String s = new String();//等效于String s = "";不等效于String s = null;
		byte[] arr = {65,66,67,68};
		String s1 = new String(arr);
		System.out.println("s1 = " + s1);//s1 = ABCD
		char[] ch = {'w','a','p','q','x'};
		String s2 = new String(ch);
		String s3 = new String(ch,1,3);
		System.out.println("s2 = "+s2);//s2 = wapqx
		System.out.println("s3 = "+s3);//s3 = apq
	}

}
运行结果:
s1 = ABCD
s2 = wapqx
s3 = apq

获取:
package cn.fu._01string;
/*
 *String类部分方法
 *1.获取
 *获取字符串中字符的长度(个数) 
 *int length(); 
 *
 *根据位置获取字符 
 *char charAt(int index);
 *
 *根据字符获取字符串中的位置
 *int indexOf(int ch);
 *
 *从指定位置开始查找ch第一出现的位置
 *int indexOf(int ch,int formIndex);
 *int indexOf(String str);
 *int indexof(String str,int formIndex);
 *
 *根据字符串获取在字符串中倒数第一次出现的位置
 *int lastIndexOf(int ch);
 *int lastIndexOf(int ch,int fromIndex);
 *int lastIndexOf(String str);
 *int lastIndexOf(String str,int forIndex)
 *
 *获取字符串中的一部分字符串,也叫字串
 *String substring(int beginIndex);
 *String substring(int beginIndex,int endIndex);
 */
public class StringDemo3 {
	public static void main(String[] args) {
		method();
	}
	public static void method(){
		String s = new String("alskjdlajsdlfjsdgj");
							// 012345678901234567
		System.out.println(s.length());//18
		char c = s.charAt(2); 
		System.out.println(c);//s
		System.out.println(s.indexOf('k'));//3
		System.out.println(s.indexOf('j',5));//8
		System.out.println(s.indexOf("js"));//8
		System.out.println(s.indexOf("js",9));//13
		System.out.println(s.lastIndexOf('s'));//14
		System.out.println(s.lastIndexOf("dl"));//10
		System.out.println(s.lastIndexOf("dl", 9));//5
		String sub1 = s.substring(5);
		String sub2 = s.substring(1,3);
		System.out.println(sub1);//dlajsdlfjsdgj
		System.out.println(sub2);//ls
		//(1,3)包头不包尾
		//如果查找不到,返回-1
		
	}
}
运行结果:
18
s
3
8
8
13
14
10
5
dlajsdlfjsdgj
ls

转换:
package cn.fu._01string;
/**
 * 2.转换
 * 将字符串变成字符数组(字符串切割)
 * String[] Split(String regex);涉及正则表达式.
 * 
 * 将字符串变成字符数组
 * char[] toCharArray();
 * 
 * 将字符串变成字节数组
 * byte[] getBytes();
 * 
 * 将字符串字母转变成大小写
 * String toUpperCase();大写
 * String toLowerCase();小写
 * 
 * 将字符串中的内容进行替换
 * String replace(char oldCh,char newCh);
 * String replace(String s1,String s2);
 * 
 * 去除字符串两端的空格
 * String trim();
 * 
 * 将字符串经行连接
 * String concat(String str);
 * 
 * 其他类型数据转换成字符串
 */

public class StringDemo4 {

	public static void main(String[] args) {
		method();
	}
	public static void method(){
		String s = "张三,李四,王五";
		String[] arr = s.split(",");
		for (int i = 0; i < arr.length; i++) {
			System.out.println(arr[i]);
		}
		System.out.println("----------------------------");
		String s1 = "张三.李四.王五";
		String[] arr1 = s1.split("\\.");
		//P.S. 点在正则表达式中是特殊符号,需要转义
		for (int j = 0; j < arr1.length; j++) {
			System.out.println(arr1[j]);
		}
		System.out.println("----------------------------");
		String s2 = "abcABC123";
		char[] arr2 = s2.toCharArray();
		for (int i = 0; i < arr2.length; i++) {
			System.out.println(arr2[i]);
		}
		System.out.println("----------------------------");
		byte[] arr3 = s2.getBytes();
		for (int i = 0; i < arr3.length; i++) {
			System.out.println(arr3[i]);
		}
		System.out.println("----------------------------");
		String s4 = s2.toUpperCase();//转大写
		String s5 = s2.toLowerCase();//转小写
		System.out.println(s4);
		System.out.println(s5);
		System.out.println("----------------------------");
		String s6 = "abcABCabcABC";
		String s7 = s6.replace('a', 'd');//所有的'a'都会被替换
		String s8 = s6.replace("abc", "def");
		String s9 = s6.replace('2', '3');
		System.out.println(s7);
		System.out.println(s8);
		System.out.println(s9);//找不到要替换的字符串,返回原字符串
		System.out.println(s9==s6);//找不到时,s9引用指向s6
		System.out.println("----------------------------");
		String s10 = "   lkjsladkjla  sdasd   ";
		System.out.println(s10.trim());
		System.out.println("----------------------------");
		System.out.println(s6.concat("game"));//连接
		//P.S. concat()效果与"+"连接符一致
		System.out.println("----------------------------");
		System.out.println(String.valueOf(5)+1);//将基本数据类型转换为String
		System.out.println(""+4+1);
		
	}
}
输出结果:
张三
李四
王五
----------------------------
张三
李四
王五
----------------------------
a
b
c
A
B
C
1
2
3
----------------------------
97
98
99
65
66
67
49
50
51
----------------------------
ABCABC123
abcabc123
----------------------------
dbcABCdbcABC
defABCdefABC
abcABCabcABC
true
----------------------------
lkjsladkjla  sdasd
----------------------------
abcABCabcABCgame
----------------------------
51
41

判断:
package cn.fu._01string;
/**
 * 3.判断
 * 判断两个字符串内容是否相同
 * boolean equals(Object obj);
 * boolean equalsIgnoreCase(String str);忽略大小写比较字符串内容.
 * 
 * 判断字符串中是否保存指定字符串
 * booleac contains(String str);
 * 
 * 判断字符串是否以指定字符串开头,是否以指定字符串结尾
 * boolean startWith(String str);
 * boolean endsWith(String str);
 * 
 * 4.比较
 * Int compareTo(String str);
 * 如果参数字符串等于此字符,则返回0;
 * 如果此字符串按照字典顺序小鱼字符串参数,则返回一个小于0的值;
 * 如果此字符串按照字典顺序大于字符串参数,则返回一个大于0的值;
 * 
 * 5.返回字符串对象的规范化表达形式
 * String intern();
 * 当调用intern方法时,如果池已经包含一个等于此String对象的字符串(用
 * equals(Object obj)方法确定),则返回池中的字符串,否则,将此Stirng对象
 * 添加到池中,并返回此String对象的引用.
 */

public class StringDemo5 {

	public static void main(String[] args) {
		method();
	}
	public static void method(){
		String s = "abc";
		System.out.println(s.equals("abc"));//true
		System.out.println(s.equalsIgnoreCase("ABC"));//true
		System.out.println(s.contains("ab"));//true
		String s1 = "独孤九剑.剑谱";
		System.out.println(s1.startsWith("独孤"));//true
		System.out.println(s1.endsWith(".剑谱"));//true
		System.out.println("----------------------------");
		String s3 = "abc";
		String s4 = "aqz";
		System.out.println(s3.compareTo(s4));
		//P.S."abc"和"aqz"两个字符串比较,'a'和'a'相等,'b'-'q'=-15,
		//'c'和'z'就没有比较的必要,直接返回-15,即s3-s4从左到右一个一个对比.	
		System.out.println("----------------------------");
		String s5 = new String("123");
		String s6 = s5.intern();
		System.out.println(s6);
	}
}
输出结果:
true
true
true
true
true
----------------------------
-15
----------------------------
123

练习:
package cn.fu._02stringtest;
/**
 * 练习
 * 1、给定一个字符串数组,按照字典顺序进行从小到大的排序。
 * {"nba","abc","cba","zz","qq","haha"}
 * 
 * 思路:
 * 1.字符串数组排序 选择排序或者冒泡排序
 * 2.遍历获取
 * 3.字典排序,可以使用compareTo()方法
 */

public class StringTest1 {
	public static void main(String[] args) {
		String[] str = {"nba","abc","cba","zz","qq","haha"};
		printStr(str);//{nba,abc,cba,zz,qq,haha}
		sort(str);
		printStr(str);//{abc,cba,haha,nba,qq,zz}
		
	}
	public static void sort(String[] str){ 
		for (int i = 0; i < str.length-1; i++) {
			for (int j = i+1; j < str.length; j++) {
				if(str[i].compareTo(str[j])>0){
					String s = "";
					s = str[i];
					str[i] = str[j];
					str[j] = s;
				}
			}
		}
	}
	public static void printStr(String[] str){
		System.out.print("[");
		for (int i = 0; i < str.length; i++) {
			if(i<str.length-1){
				System.out.print(str[i]+",");
			}else{
				System.out.println(str[i]+"]");
			}
		}
	}
}

输出结果:
[nba,abc,cba,zz,qq,haha]
[abc,cba,haha,nba,qq,zz]

package cn.fu._02stringtest;
/**
 * 2、一个子串在整串中出现的次数 "nbaernbatynbauinbaopnba"
 * 
 * 思路:
 * 1.需要计数器count;
 * 2.可以采用indexOf(String str,int fromIndex);来寻找子串
 * 3.可以用length()来获取字串的长度,然后叠加到fromIndex中
 * 4.找不到就-1结束;
 * 
 */

public class StringTest2 {

	public static void main(String[] args) {
		String s1 = "nbaernbatynbauinbaopnba";
		String s2 = "ba";
		int a = foundString(s1,s2);
		System.out.println(a);
	}
	public static int foundString(String s1,String s2){
		int count = 0;
		int index = 0;
		while(index!=-1){
			 index = s1.indexOf(s2,index);
			 if(index!=-1){
				 index = index + s2.length();
				 count++;
			 }else{
				 break;
			 }
		}
		return count;
	}
}
输出结果:
5
package cn.fu._02stringtest;
/**
 * 3、两个字符串中最大相同的子串
 * "qwerabcdtyuiop"
 * "xcabcdvbn"
 * 
 * 思路:
 * 1.先比较两个字符串的长度,确定包含与被包含的关系;
 * 2.先判断小的字符串,是否被大的包含;
 * 3.小的字符串长度减一,从左自右组合后接着判断包含关系,直到长度为0停止.
 * xcabcdvb...
 * 01234567                  |a-b,即s1.constains(s2.subString(a,b+1))是否为true
 * 0123456 1234567           |0-6,1-7
 * 012345 123456 234567      |0-5,1-6,2-7
 * 01234 12345 23456 34567   |0-4,1-5,2-6,3-7
 * ...                       |0-3,1-4,2-5,3-6,4-7...类似于乘法表
 */

public class StringTest3 {

	public static void main(String[] args) {
		String s1 = "qwerabcdtyuiop";
		String s2 = "xcabcdvbn";
		String ss1;
		String ss2;
		if(s1.length()>s2.length()){
			ss1 = s1;
			ss2 = s2;
		}else{
			ss1 = s2;
			ss2 = s1;
		}
		System.out.println(ss1);
		System.out.println(ss2);
		String minsub = method(ss1,ss2);
		System.out.println(minsub);
	}
	public static String method(String ss1,String ss2){
		for(int i = 0 ; i< ss2.length();i++){
			for(int a = 0 ,b = ss2.length()-i;b!=ss2.length()+1;a++,b++){
				String sub = ss2.substring(a,b);
				if(ss1.contains(sub)){
					return sub;
				}
			}
		}
		return null;
	}
}
输出结果:
qwerabcdtyuiop
xcabcdvbn
abcd

package cn.fu._02stringtest;
/**
 * 4、模拟一个trim功能方法,去除字符串两端的空白。
 * 思路:
 * 1.定义角标,判断一个字符串头尾是否为空格;
 * 2,是空格,前面++,后面--;
 * 3,用substring截取中间非空格部分
 */

public class StringTest4 {

	public static void main(String[] args) {
		String s = "   sdfa    d    ";
		System.out.println("-"+myTrim(s)+"-");
		
	}
	public static String myTrim(String s){
		int start = 0;
		int end = s.length()-1;
		while(start<=end&&s.charAt(start)==' '){//避免全部为空格时出现异常
			start++;
		}
		while(start<=end&&s.charAt(end)==' '){
			end--;
		}
		return s.substring(start, end+1);
	}
}
输出结果:
-sdfa    d-





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值