String类中常用方法总结【三】

package com.wyq.test;

public class TestSt {
	public static void main(String[] args) {
		String str = new String("helloworld");
		// 【1】获取指定位置上的字符char charAt(int index),实际上就是根据索引获取char类型的数组value的值
		// value【index】
		for (int i = 0; i < str.length(); i++) {
			System.out.println(str.charAt(i));// charSequence接口中的方法
		}
		// 【2】int compareTo(String str)比价两个字符串的大小 comparable接口中的方法
		System.out.println("apple.compareTo(cat)" + ("apple".compareTo("cat")));
		System.out.println("apple.compareTo(apple)" + ("apple".compareTo("apple")));
		System.out.println("cat.compareTo(apple)" + ("cat".compareTo("apple")));
		String str1 = "aanana";
		String str2 = "banana";
		String str3 = "can";
		System.out.println("str1.compareTo(str2)" + (str1.compareTo(str2)));
		System.out.println("str1.compareTo(str2)" + (str1.compareTo(str3)));
		/**
		 * compareTo的比较,默认是按照首字母进行比较的
		 */
		// 【3】int compareToIgnoreCase(String str )忽略大小写
		System.out.println("apple".compareToIgnoreCase("APPLE"));

		// 【4】string concat (String),拼接
		String str4 = "apple";
		String str5 = str4.concat(str2);
		System.out.println(str5);
		System.out.println(str5.concat("China"));

		// 【5】boolean contains(CharSequence ch ) 接口作方法的形式参数 , 可以传入接口的任何实现类
		System.out.println(str5.contains("ba"));
		System.out.println(str5.contains(str2));

		// 【6】boolean endsWith(String str ) 判断是否以指定的字符 str 结尾,用于判断是否是指定类型的文件
		System.out.println(str5.endsWith("na"));
		System.out.println(str5.endsWith(str2));
		System.out.println("iloveyou".endsWith("you"));

		// [7]boolean equals(Object obj ) 父类 Object 作为方法的形式参数 ,Object 类中的方法
		System.out.println("apple".equals("APPLE"));
		System.out.println("apple".equals(str4));
		System.out.println(str4.equalsIgnoreCase("APPLE"));

		// [8]static String format(String ,Object ... obj )
		int a = 10, b = 2;
		System.out.println(a + "/" + b + "=" + (a / b));
		System.out.println(String.format("%d/%d=%d", a, b, (a / b)));

		byte[] by = { 97, 98, 99, 100, 101 };
		String s = new String(by);
		System.out.println(s);
		System.out.println(new String(by)); // byte ————>String
		/**
		 * 【1】byte [] getByte()
		 */
		String s1 = "helloworld";
		byte by1[] = s1.getBytes();
		for (int i : by1) {
			System.out.println(i);
		}
		/**
		 * 【2】 int indexof(int/String) int indexof(int/string,int from index)
		 */
		System.out.println(s.indexOf(101));// 在索引為1的位置上,找到返回索引
		System.out.println(s.indexOf(101, 4));// 從索引為4的位置刪開始找,找到后返回索引值
		System.out.println(s.indexOf("a"));
		System.out.println(s.indexOf('l'));
		System.out.println(s1.indexOf('l'));// 自動類型轉換,字符型準環襯了int類型

		/**
		 * 【3】int lastIndexOf( int /String) int lastIndexOf( int /String, int
		 * fromIndex
		 */
		System.out.println("********************************");
		System.out.println(s1.indexOf("o"));
		System.out.println(s1.lastIndexOf('o', 7));
		System.out.println(s1.lastIndexOf("o", 7));

		/**
		 * [4]boolean isEmpty 字符串長度為0時,結果為true
		 */
		System.out.println("".isEmpty());
		String s2 = null;
		// System.out.println(s2.isEmpty());

		/**
		 * [5]replace(char oldchar,char newchar) replace(charSequence
		 * c,charSequence c2)
		 */
		System.out.println(s1);
		System.out.println(s1.replace("0", "中國人"));// 使用replace時需要創建對象,也就是等號右邊要參與運損
		String s3 = str.replace("o", "中華人民");
		System.out.println(s3);

		/**
		 * [6]String [] split(String s) 1998-8-9
		 */
		String sdate = "1999-09-08";
		String ss[] = sdate.split("-");
		for (String i : ss) {
			System.out.println(i);
		}

		/**
		 * [7]charSequence subSequence(int startIndex ,int endIndex)
		 */
		CharSequence sss = s1.subSequence(2, 5); // 韓頭部捍衛
		System.out.println("sss" + sss);// 實際上是調用了toString方法

		/**
		 * 【8】String subString(int startIndex,int endIndex)
		 */
		// charSequence ——>string ——>toSatring()
		// str = sdate.substring(2,5);
		System.out.println(str);

		/**
		 * [9]char [] toCharArray()
		 * 
		 */
		char[] c = str.toCharArray();
		System.out.println("素組的長度是:" + c.length);
		for (char s0 : c) {
			System.out.println(s0);
		}

		/**
		 * [10]String trim() 可以將String類型的前後空格去掉
		 */
		String strr = "      hel     ll   o   wo     rl   d        ";
		System.out.println(strr.length());
		strr = strr.trim();
		System.out.println(strr.length());

		/**
		 * [11]staticString valueOf(任意類型) valueOf():可以將任意類型轉換成String類型
		 */
		System.out.println("++++++++++++++++++++++++");
		String ss1 = String.valueOf(12);// int ——>String
		System.out.println(ss1);
		String ss2 = String.valueOf("12");// String——>String
		System.out.println(ss2);
		String ss3 = String.valueOf('1');// char——>String
		System.out.println(ss3);
		String ss4 = String.valueOf(new char[] { 'a', 'b', 'c', 'd' });// charArray——>String
		System.out.println(ss4);
		String ss5 = String.valueOf(new Integer(12));// Integer——>String
		System.out.println(ss5);
		String ss6 = String.valueOf(new Double[] { 12.0, 23.0, 34.0, 45.0, 56.0 });// intArray——>String
		System.out.println(ss6);
	}

}

猜你喜欢

转载自blog.csdn.net/wyqwilliam/article/details/92740108
今日推荐