본문 바로가기

프로그래밍/java

Jenkov.com - Java String

String Concatenation Performance

자바 문자열은 컴파일 시에 + 을 StringBuilder 형태로 append 하므로, StringBuilder를 통해 명시적으로 구현하는 것이 성능에 도움.


String[] strings = new String[]{"one", "two", "three", "four", "five" }; String result = null; for(String string : strings) { result = result + string; // Call new StringBuilder for every iteration. }


StringBuilder temp = new StringBuilder(); for(String string : strings) { temp.append(string); }


Replacing Characters in Strings With replace()

자바의 문자열은 Immutable 이므로, replace 함수는 새로운 문자열을 생성해서 반환한다.


Converting Numbers to Strings With valueOf()

자바의 Primitive Type 숫자의 문자열 변환 시에 활용.

String intStr = String.valueOf(10);


Getting Characters and Bytes

String theString = "This is a good day to code";


byte[] bytes1 = theString.getBytes(); // default CharSet encoding on the machine

byte[] bytes2 = theString.getBytes(Charset.forName("UTF-8"));


'프로그래밍 > java' 카테고리의 다른 글

Jenkov.com - Java Nested Classes  (0) 2015.04.20
Java Math Operators and Math Class  (0) 2015.03.16
Jenkov.com - Java Data Types  (0) 2015.03.13