GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||||||||||
GeezerVison is showing zetcode.com — Java StringBuilder - mutable Java strings with StringBuilder Java StringBuilderlast modified January 27, 2024
Java StringBuilder tutorial shows how to use StringBuilder
Java StringBuilder constructors
Java StringBuilder is mutable
Java com/zetcode/MutableImmutableEx.java
package com.zetcode;
public class MutableImmutableEx {
public static void main(String[] args) {
var word = "rock";
var word2 = word.replace('r', 'd');
System.out.println(word2);
var builder = new StringBuilder("rock");
builder.replace(0, 1, "d");
System.out.println(builder);
}
}
The example demonstrates the main difference between var word2 = word.replace('r', 'd');
Java var builder = new StringBuilder("rock");
builder.replace(0, 1, "d");
On the other hand, dock dock Java StringBuilder append method
com/zetcode/StringBuilderEx.java
package com.zetcode;
import java.util.stream.LongStream;
public class StringBuilderAppendEx {
private final static long MAX_VAL = 500;
public static void main(String[] args) {
var builder = new StringBuilder();
var sum = LongStream.rangeClosed(0, MAX_VAL).sum();
LongStream.rangeClosed(1, MAX_VAL).forEach(e -> {
builder.append(e);
if (e % 10 == 0) {
builder.append("\n");
}
if (e < MAX_VAL) {
builder.append(" + ");
} else {
builder.append(" = ");
}
});
builder.append(sum);
System.out.println(builder);
}
}
The example builds one big string from hundreds of small strings. The string has the following form: 1 + 2 + 3 + ... + MAX_VAL = SUM. var builder = new StringBuilder();
An empty LongStream.rangeClosed(1, MAX_VAL).forEach(e -> {
A range of values 1..MAX_VAL is created. We iterate over
these values with the builder.append(e);
We append the current value to the string builder with the if (e % 10 == 0) {
builder.append("\n");
}
In order for the output to fit the screen, we add a newline character after each ten values. if (e < MAX_VAL) {
builder.append(" + ");
} else {
builder.append(" = ");
}
Between the values, we add either the "+" or the "=" characters. builder.append(sum); At the end of the string, we add the sum of values. System.out.println(builder); Finally, the string is printed to the console. Java StringBuilder insert method
The com/zetcode/StringBuilderInsertEx.java
package com.zetcode;
public class StringBuilderInsertEx {
public static void main(String[] args) {
var sentence = "There is a red fox in the forest.";
var builder = new StringBuilder(sentence);
builder.insert(19, "and a wolf ");
System.out.println(builder);
}
}
The example inserts a string into a sentence with the There is a red fox and a wolf in the forest. We have created this sentence. Getting indexes of substrings
The com/zetcode/StringBuilderIndexesEx.java
package com.zetcode;
public class StringBuilderIndexesEx {
public static void main(String[] args) {
var builder = new StringBuilder();
builder.append("There is a wolf in the forest. ");
builder.append("The wolf appeared very old. ");
builder.append("I never saw a wild wolf in my life.");
var term = "wolf";
int firstIdx = builder.indexOf(term);
int firstIdx2 = builder.indexOf(term, 15);
System.out.format("First occurrence of %s %d%n", term, firstIdx);
System.out.format("First occurrence of %s %d%n", term, firstIdx2);
int lastIdx = builder.lastIndexOf(term);
int lastIdx2 = builder.lastIndexOf(term, 15);
System.out.format("Last occurrence of %s %d%n", term, lastIdx);
System.out.format("Last occurrence of %s %d%n", term, lastIdx2);
System.out.println(builder);
}
}
The example uses the var builder = new StringBuilder();
builder.append("There is a wolf in the forest. ");
builder.append("The wolf appeared very old. ");
builder.append("I never saw a wild wolf in my life.");
We create a string builder with the int firstIdx = builder.indexOf(term); We get the first occurrence of the "wolf" term from the builder. int firstIdx2 = builder.indexOf(term, 15); We get the first occurrence of the "wolf" term from the builder, starting from the index 15. int lastIdx = builder.lastIndexOf(term); int lastIdx2 = builder.lastIndexOf(term, 15); Similarly, we get the last occurrences of the "wolf" substring. First occurrence of wolf 11 First occurrence of wolf 35 Last occurrence of wolf 78 Last occurrence of wolf 11 There is a wolf in the forest. The wolf appeared very old. I never saw a wild wolf in my life. StringBuilder replace method
The com/zetcode/StringBuilderReplaceEx.java
package com.zetcode;
public class StringBuilderReplaceEx {
public static void main(String[] args) {
var sentence = "I saw a red fox running into the forest.";
var builder = new StringBuilder(sentence);
var term = "fox";
var newterm = "dog";
int idx = builder.indexOf(term);
int len = term.length();
builder.replace(idx, idx + len, newterm);
System.out.println(builder);
}
}
The example replaces the "fox" substring with the "dog" string. int idx = builder.indexOf(term); We find the beginning index of the substring to be replaced. int len = term.length(); In our operation, we need to know the length of the substring. builder.replace(idx, idx + len, newterm);
We call the Java StringBuilder delete charactersThere are two methods for deleting characters in a string builder. com/zetcode/StringBuilderRemoveEx.java
package com.zetcode;
public class StringBuilderRemoveEx {
public static void main(String[] args) {
var sentence = "There is a red fox in the forest.";
var builder = new StringBuilder(sentence);
builder.delete(11, 14);
System.out.println(builder);
builder.deleteCharAt(11);
System.out.println(builder);
}
}
The example deletes a few characters from a string. builder.delete(11, 14);
With the builder.deleteCharAt(11);
With the There is a fox in the forest. There is a fox in the forest. Java StringBuilder substrings
With the com/zetcode/StringBuilderSubstringsEx.java
package com.zetcode;
public class StringBuilderSubstringsEx {
public static void main(String[] args) {
var sentence = "There is a red fox in the forest.";
var builder = new StringBuilder(sentence);
var word = builder.substring(15, 18);
System.out.println(word);
var sbstr = builder.substring(15);
System.out.println(sbstr);
}
}
In the example, we retrieve two substrings. var word = builder.substring(15, 18); This line retrieves a substring with a starting index 15 and ending index 18. var sbstr = builder.substring(15); Here we retrieve a substring from index 15 until the end of the sentence. fox fox in the forest. SourceJava StringBuilder - language reference In this article we have worked with Java StringBuilder. AuthorMy name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming. List all Java tutorials. | ||||||||||
| You are visitor 01402520 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |