일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | 7 |
8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | 18 | 19 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 27 | 28 |
29 | 30 | 31 |
- 공유기 서버
- Spring Framework
- Spring Batch
- reactor core
- reactive
- ipTIME
- reactor
- 웹 커리큘럼
- 웹 스터디
- spring reactive
- 서버운영
- 웹앱
- Today
- Total
Hello World
[펌]Java StringBuilder Example 본문
object seems like a object but with the characteristics of an array. Every object of this type is like a sequence of characters that can be modified, since class provides us many methods for changing the content and/or the length of the sequence, for initializing the capacity etc. class is mostly used when we want to concatenate many strings continuously and/or treat them like variable-length arrays.
In this example as you can expect, we are going to show how to use basic operations of
class.
1. Example of StringBuilder
Create a java class named
and paste the following code.StringBuilderClass.java:
Now lets explain the code above. Firstly we create an instance of
where the default capacity is 16, which means 16 empty characters. In order to add a string, method is called with the specified string as a parameter. We can append more data type in the string builder, if we add the specific built-in data types as a parameter in method. Also Java provides operation, so we can insert the string representation of a data type in a specific offset of the sequence. As you can notice in the code above, we want to insert the string into the 6th position of the current string builder.In addition,
supports method which returns the current number of characters in the string builder sequence, as well as method which indicates the allocation of character space of the string builder. In this example we create an instance of and we set its capacity to 15. So, after the append of a 9-digit integer, the expecting length is 9 and the capacity is 15. Notice that the capacity cannot be less than 0 but can be greater than or equal to the length of the string builder. Moreover we can remove a specific portion of the string builder, by calling operation. In this situation we should indicate the part of the string builder we want to delete, by setting the and the of the removing substring.In conclusion, we show the advantage that
class offers for a better performance. In this situation we read all the lines of a file and append them into a string builder, separated by ‘@’ character, until the end of the file. Finally, we call method in order to take the string representation of the string builder.After the explanation, you can see the expected results of the execution in the output below.
Output:
Download the source file
This was a tutorial about StringBuilder in Java. Download the source code of this example: StringBuilderExample.z
출처: http://examples.javacodegeeks.com/core-java/lang/stringbuilder/java-stringbuilder-example
'Java > Core' 카테고리의 다른 글
Java Programing 치트 시트 (0) | 2016.04.18 |
---|---|
[JAVA] 정규표현식, Matcher 메서드 사용방법과 그룹 개념이해 (0) | 2016.03.04 |
[펌]Spring의 Singleton과 Java static기반 Singleton패턴의 차이 (0) | 2016.01.14 |
ElasticSearch Tutorial for Beginners (0) | 2016.01.12 |
10 Examples of using ArrayList in Java (0) | 2016.01.12 |