Java复习第五天---常用类

//public char charAt(int index)
		String str = "hello csdn";
		//返回字符串中指定位置的字符
		char charAt = str.charAt(4);
		System.out.println(charAt);
		for(int i = 0; i < str.length(); i++){
			System.out.println(str.charAt(i));
		}

<span style="white-space:pre">	</span>@Test
	public void testString02(){
		//public int indexOf(String s)
		//返回字符串s在调用者字符串中第一次出现的位置
		String str = "good morning csdn";
		int indexOf = str.indexOf("n");
		System.out.println(indexOf);
		//public int indexOf(String s ,int startpoint)
		//返回从startpoint指定的位置开始,在后面的字符串中,字符串s第一次出现的位置
		int indexOf2 = str.indexOf("n", 9);
		System.out.println(indexOf2);
		//*如果传入参数为整型,则将整型转为对应ASCII码的字符,然后再查找其在字符串中的位置
		int indexOf3 = str.indexOf(99);
		System.out.println(indexOf3);
		//*返回从指定位置开始的后面字符串中整型参数对应的字符第一次出现的位置
		int indexOf4 = str.indexOf(100, 4);
		System.out.println(indexOf4);
	}

<span style="white-space:pre">	</span>@Test
	public void testString03(){
		String str = "good morning csdn";
		//public int lastIndexOf(String s)
		//返回指定字符串在调用者字符串中最后一次出现的位置的索引值
		int lastIndexOf = str.lastIndexOf("o");
		System.out.println(lastIndexOf);
		//返回指定字符串在从开始到指定位置中最后一次出现的位置的索引值
		int lastIndexOf2 = str.lastIndexOf("n", 9);
		System.out.println(lastIndexOf2);
	}

<span style="white-space:pre">	</span>@Test
	public void testString04(){
		//public boolean startsWith(String prefix)
		String str = "csdnAAAgood";
		//判断调用者字符串是否是以指定字符串开始的
		boolean startsWith = str.startsWith("csdn");
		System.out.println(startsWith);
		//判断指定偏移量后调用者字符串是否是以指定字符串开始的
		boolean startsWith2 = str.startsWith("csdn", 4);
		System.out.println(startsWith2);
		//public boolean endsWith(String suffix)
		//判断调用者字符串是否是以指定字符串结尾的
		boolean endsWith = str.endsWith("good");
		System.out.println(endsWith);
	}

<span style="white-space:pre">	</span>@Test
	public void testString05(){
		//public boolean regionMatches(int firstStart,
		//String other,int otherStart ,int length)
		String str = "CSDN Hello EveryOne";
		//从调用者字符串中取从firstStart开始的字符串
		//从other字符串中从otherStart开始length个字符串
		//二者进行比较
		boolean regionMatches = str.regionMatches(5, "Hello", 0, 5);
		System.out.println(regionMatches);
	}

<span style="white-space:pre">	</span>@Test
	public void testString04(){
		//public String concat(String str)
		//将传入的字符串连接到调用者字符串后面,在内存中创建了新的字符串
		String str = "good ";
		String concat = str.concat("morning");
		System.out.println(concat);
		System.out.println(str == concat);
	}
	
	@Test
	public void testString03(){
		String str = "    N   N    ";
		System.out.println("--"+str+"--");
		//public String trim()
		//去掉字符串中的前后空格
		String trim = str.trim();
		System.out.println("--"+trim+"--");
	}
	
	@Test
	public void testString02(){
		String str = "hello CSDN good morning";
		//pubic String replace(char oldChar,char newChar)
		//将调用者字符串中所有指定字符(oldChar)替换为新字符(newChar)
		String replace = str.replace('o', '@');
		System.out.println(replace);
		System.out.println(str);
		//public String replaceAll(String regex,String new)
		//将调用者字符串中与传入的正则表达式( regex)匹配的字符串替换为新字符串(new)
		String replaceAll = str.replaceAll("o{2}", "@_@");
		System.out.println(replaceAll);
	}
	
	@Test
	public void testString01(){
		String str = "helloCSDN";
		//public String substring(int startpoint)
		//返回调用者字符串中从指定位置开始到最后的子字符串
		String substring = str.substring(5);
		System.out.println(substring);
		//注意:调用者字符串本身不会改变,只是返回一个改变后的字符串
		System.out.println(str);
		//public String substring(int start,int end)
		//返回从开始位置到结束位置的半闭半开区间内的字符串[start,end)
		String substring2 = str.substring(4, 7);
		System.out.println(substring2);
	}

<span style="white-space:pre">	</span>@Test
	public void testString05(){
		//public String[] split(String regex)
		//根据指定分隔符将调用者字符串分割成若干个子字符串组成的数组
		String str = "go-od m-orn-ing CSD-N";
		String[] split = str.split(" ");
		for(String s : split){
			System.out.println(s);
		}
	}


	@Test
	public void testString01(){
		//String to Integer:Integer.parseInt("125");
		String str = "125we";
		int parseInt = 0;
		try {
			parseInt = Integer.parseInt(str);
		} catch (NumberFormatException e) {
			System.out.println("转换失败");
		}
		System.out.println(parseInt + 125);
	}

<span style="white-space:pre">	</span>@Test
	public void testString02(){
		//Integer to String
		//1.String str = i + "";
		int i = 12;
		String str = i + "";
		System.out.println(str);
		//2.String.valueOf(7);
		str = String.valueOf(7);
		System.out.println(str);
		//3.integer.toString();
		Integer integer = new Integer(28);
		str = integer.toString();
		System.out.println(str);
		//4.Integer.toString(135);
		str = Integer.toString(135);
		System.out.println(str);
	}

<span style="white-space:pre">	</span>@Test
	public void testSbf09(){
		StringBuffer sbf = new StringBuffer("good moorning");
		//public String substring(int start,int end)
		//半闭半开区间[4,10)
		String substring = sbf.substring(4, 10);
		System.out.println(substring);
	}
	
	@Test
	public void testSbf08(){
		StringBuffer sbf = new StringBuffer("good moorning");
		//public int indexOf(String str)
		int indexOf = sbf.indexOf("oo");
		indexOf = sbf.indexOf("oo", 3);
		System.out.println(indexOf);
	}
	
	@Test
	public void testSbf07(){
		StringBuffer sbf = new StringBuffer("good morning");
		//StringBuffer replace( int startIndex ,int endIndex, String str)
		//半闭半开区间[3,9)
		sbf.replace(3, 9, "@@@");
		System.out.println(sbf);
	}
	
	@Test
	public void testSbf06(){
		StringBuffer sbf = new StringBuffer("good morning");
		//public char charAt(int n)
		char charAt = sbf.charAt(5);
		//public void setCharAt(int n ,char ch)
		sbf.setCharAt(5, '@');
		System.out.println(sbf);
	}
	
	@Test
	public void testSbf05(){
		//StringBuffer delete(int startIndex, int endIndex)
		StringBuffer sbf = new StringBuffer("good");
		//半闭半开区间[1,3)
		sbf.delete(1, 3)
		   .insert(1,"oo")
		   .append(" morning")
		   .deleteCharAt(5);//删除指定位置的字符
		System.out.println(sbf);
	}
	
	@Test
	public void testSbf04(){
		//public StringBuffer reverse()
		StringBuffer sbf = new StringBuffer("good");
		sbf.reverse();
		System.out.println(sbf);
	}
	
	@Test
	public void testSbf03(){
		//StringBuffer insert(int index, String str)
		StringBuffer sbf = new StringBuffer("good");
		sbf.insert(2, "I")
		   .insert(3, 'C');
		System.out.println(sbf);
	}
	
	@Test
	public void testSbf02(){
		//StringBuffer append(String s)
		StringBuffer sbf = new StringBuffer("good");
		sbf.append(" morning")
		   .append(" Hello ")
		   .append(true)
		   .append(' ')
		   .append('m')
		   .append(' ')
		   .append(23.3)
		   .append(' ')
		   .append(new Apple("myApple"));
		System.out.println(sbf);
	}
	
	@Test
	public void testSbf01(){
		//创建初始容量为16的字符串缓冲区,但是并不等于sbf01的长度为16
		StringBuffer sbf01 = new StringBuffer();
		System.out.println(sbf01.length());
		StringBuffer sbf02 = new StringBuffer(20);
		System.out.println(sbf02.length());
		StringBuffer sbf03 = new StringBuffer("my String buffer");
		//length()函数返回的是实际字符串的长度
		System.out.println(sbf03.length());
	}

<span style="white-space:pre">	</span>@Test
	public void testStringBuilder02() {
		String text = "";
		long startTime = 0L;
		long endTime = 0L;
		StringBuffer buffer = new StringBuffer("");
		StringBuilder builder = new StringBuilder("");
		startTime = System.currentTimeMillis();
		for (int i = 0; i < 20000; i++) {
			buffer.append(String.valueOf(i));
		}
		endTime = System.currentTimeMillis();
		System.out.println("StringBuffer的执行时间:" + (endTime - startTime));
		startTime = System.currentTimeMillis();
		for (int i = 0; i < 20000; i++) {
			builder.append(String.valueOf(i));
		}
		endTime = System.currentTimeMillis();
		System.out.println("StringBuilder的执行时间:" + (endTime - startTime));
		startTime = System.currentTimeMillis();
		for (int i = 0; i < 20000; i++) {
			text = text + i;
		}
		endTime = System.currentTimeMillis();
		System.out.println("String的执行时间:" + (endTime - startTime));
	}

	@Test
	public void testStringBuilder() {
		// 21eurt gninrom d@oog
		StringBuilder sBd = new StringBuilder("good");
		sBd.append(" morning ").append(true).append(12).insert(3, '@')
				.reverse().delete(3, 7).replace(7, 13, "NNNNN")
				.setCharAt(12, '*');
		char charAt = sBd.charAt(12);
		System.out.println(charAt);
		System.out.println(sBd);
		int indexOf = sBd.indexOf("NNNNN");
		System.out.println(indexOf);
		String substring = sBd.substring(5, 12);
		System.out.println(substring);
	}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值