자바 입문 String 알 아 보기

4527 단어 자바String
package hello;

/*
 *  Eclipse      jar  ,     
 * File->Export->  Runable jar file          
 *        ,               
 * cmd     ,    java -jar jarfilepath
 *               
 * */

public class World {
	
	public static void main(String[] args) {
		System.out.println("Hello java");

		// String     
		String str = "Hello world";
		int worIndex = str.indexOf("wor");
		System.out.println(String.format("worIndex is %1d", worIndex));
		String world = str.substring(worIndex);
		System.out.println(world);
		//   startIndex,     endIndex
		String wor = str.substring(worIndex, worIndex + 3);
		System.out.println(wor);

		String plus = "Hello " + "world";
		System.out.println(plus);

		//     
		String upperCase = plus.toUpperCase();
		System.out.println(upperCase);

		//   concat
		String a = "A";
		a = a.concat("bc");
		System.out.println(a);

		// char
		for (int i = 0; i < a.length(); i++) {
			System.out.print(a.charAt(i));
		}

		for (char c : a.toCharArray()) {
			System.out.print(c);
		}
		System.out.println();
		// split
		String toSplit = "Hello James Bond";
		String[] words = toSplit.split(" ");
		for (String word : words) {
			System.out.println(word);
		}

		// compare
		String str1 = "abc";
		String str2 = "def";
		int compareResult = str1.compareTo(str2);
		System.out.println(compareResult);

		// startsWith,endsWith
		boolean isStartsWitha = str1.startsWith("a");
		System.out.println(isStartsWitha);
		System.out.println(isStartsWitha);

		// StringBuffer       , StringBuilder    
		// StringBuffer      StringBuilder          StringBuilder
		StringBuffer sb = new StringBuffer();
		sb.append("Hello");
		sb.append(" ");
		sb.append("World");
		String strValue = sb.toString();
		System.out.println(strValue);

		StringBuilder sbr = new StringBuilder();
		sbr.append("Hello ");
		sbr.append("Java");
		System.out.println(sbr.toString());

		//        
		studyFormatInteger();

		//      
		studyFormatFloat();

		//           %,         
		String formated = String.format("    %1$.2f%%", 3.145);
		System.out.println(formated);

	}

	static void studyFormatFloat() {
		//       
		// %[index$][  ][    ][.  ]    
		//            ,         
		String[] formats = {
				// -          ,    “ 0  ”    ,      
				"%1$-6.2f",
				// +           ,   +
				"%1$+6.3f",
				//           ,    - 
				"%1$ 6.3f",
				//          
				"%1$ 6.3e",
				// g  G                       
				//                 6   3   .3   
				"%1$ 6.3g", "%1$g",
				// 'a', 'A' --                        
				"%1$a" };

		for (String format : formats) {
			float num = 3.14159f;
			String formated = String.format(format, num);
			System.out.println(formated);
		}
	}

	static void studyFormatInteger() {
		//        
		// java         c printf,   c# string.Format
		//         
		// http://blog.csdn.net/andycpp/article/details/1749700
		//         
		//         :%[index$][  ][    ]    
		// index   1  
		String strFormatInteger = String.format("%1$+5d;%2$ 5d", 99, 888);
		System.out.println(strFormatInteger);

		String[] formats = { "%1$-9d", "%1$#5X", "%1$+9d", "%1$09d", "%1$,9d",
				"%1$(9d" };

		// java   foreach   ,    for (type varname : iterable)  foreach
		for (String format : formats) {
			int num = -9999999;
			String formated = String.format(format, num);
			// System.out.println(formated);
			String output = String.format(
					"String.format(\"%1$s\",%2$d) == \"%3$s\"", format, num,
					formated);
			System.out.println(output);
		}
	}
}

좋은 웹페이지 즐겨찾기