자바 입문 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);
		}
	}
}
                이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Is Eclipse IDE dying?In 2014 the Eclipse IDE is the leading development environment for Java with a market share of approximately 65%. but ac...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.