A String object is a string of characters used to store data in an ordered sequence, an unlimited number of characters can be stored, a string can be represented on a single line of characters enclosed in quotes “”, like “hello” a string has 5 characters. Here are some ways to add characters to String Java.
Adding a character to the beginning of a string using the + operator.
Here is the whole software for adding characters to the String’s beginning and end.
Output:
Java2blog
Java2blog
Add a character to the beginning of the string.
1
2
3
4
5
|
char startChar=‘J’;
String blogName = “ava2blog”;
String cblogName = startChar + blogName;
|
Add a character to the String’s end.
Adding a character to the beginning of a string using the + operator.
1
2
3
4
5
|
char endChar =‘g’;
String blogNameWOG = “Java2blo”;
String blogNameWG = blogNameWOG + endChar;
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
package org.arpit.java2blog;
public class AddCharacterStartEndMain {
public static void main(String[] args) {
char startChar=‘J’;
String blogName = “ava2blog”;
String cblogName = startChar + blogName;
System.out.println(cblogName);
char endChar =‘g’;
String blogNameWOG = “Java2blo”;
String blogNameWG = blogNameWOG + endChar;
System.out.println(blogNameWG);
}
}
|
You can see that we’ve added
Output:
Output:
'J'
to start of String "ava2blog"
and added 'g'
to the end of "Java2blo"
.
Add a character to a string in the specified position
To add a character to a string at a certain point, there are numerous approaches.Using StringBuffer
You can use method to add character to String at given position. To add a character to a String at a specified point, use theinsert()
function of the StringBuffer's
class.
Let’s examine with an illustration.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
package org.arpit.java2blog;
public class AddCharacterToStringAnyPosition {
public static void main(String[] args) {
String blogName = “JavaBlog”;
char two =‘2’;
String cblogName = addCharToString(blogName,two,4);
System.out.println(cblogName);
}
public static String addCharToString(String str, char c, int pos) {
StringBuilder stringBuilder = new StringBuilder(str);
stringBuilder.insert(pos, c);
return stringBuilder.toString();
}
}
|
Java2blog
As you can see, we have add char '2'
at position 4
to String "Javablog"
.
Using substring
Another option is to add a character to a string at a certain point using the substring function of a String. Using an illustration, let’s examine.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
package org.arpit.java2blog;
public class AddCharacterToStringAnyPosition {
public static void main(String[] args) {
String blogName = “JavaBlog”;
char two =‘2’;
String cblogName = addCharToStringUsingSubString(blogName,two,4);
System.out.println(cblogName);
}
public static String addCharToStringUsingSubString(String str, char c, int pos) {
return str.substring(0, pos)+ c +str.substring(pos);
}
}
|
Java2blog
Explanation
Here is the example:
Output:
Additionally, go no farther than Onextdigital if you’re seeking for a development for your eCommerce shop that is affordable. Along with a team of skilled experts and developers who work directly with the designers, we also provide economical Shopify Development, Bigcommerce Development, and Magento Development services.
- Obtain a substring before to the position.
- Add personality.
- Obtain a substring following the position.
+
operator.
As heap memory will fill up more quickly as a result of temporary objects, if we need to use this function often, this may result in frequent trash collection.
Using Apache common lang3
Additionally, you may add characters to the string at any position by using Apache Common’sStringUtils
.
Here is the dependency that you must add to your pom.xml
file for Apache Common Lang 3.
1
2
3
4
5
6
7
8
9
|
</p>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.9</version>
</dependency>
<p>
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
package org.arpit.java2blog;
package org.arpit.java2blog;
import org.apache.commons.lang3.StringUtils;
public class AddCharacterToStringAnyPosition {
public static void main(String[] args) {
String blogName = “JavaBlog”;
char two =‘2’;
String cblogName = addCharToStringUsingApache(blogName,two,4);
System.out.println(cblogName);
}
public static String addCharToStringUsingApache(String str, char c, int pos) {
return StringUtils.overlay(str,“”+c, pos, pos);
}
}
|
Java2blog
As you can see, we have used StringUtils
‘s overlay()
method to add character to the String at given position.
StringUtils.overlay(str, overlay, start, end)
takes four arguments.
str
: Orignal String
overlay
: String which you want to add
start
: start position
end
: end position
Because we are just adding one letter, the start and finish positions are both pos
.