Home » Java programming language

How to add characters to a string in Java?

Adding characters to a string: Here, we are going to learn how to add characters to a string in java?
Submitted by Preeti Jain, on July 04, 2019

Case 1: In a simple way we will learn how to add characters by using predefined methods.

  • With the help of Java StringBuffer insert(int offset, String s) method.
  • This method is available in package java.lang.StringBuffer.insert(int offset, String s).
  • This method is used to insert the string representation with the given object at the specified position.
  • This method is overridable so it is available in different-2 forms like:
    1. StringBuffer insert(int offset , boolean b)
    2. StringBuffer insert(int offset , char c)
    3. StringBuffer insert(int offset , char[] c) etc.
  • The return type of this method is StringBuffer that means this method returns a reference to this object.
  • We can pass two or three objects according to our need as a parameter in the method and that object will insert in a sequence at the specified position.

Example:

import java.lang.StringBuffer;

public class AddCharactersToString {
    public static void main(String[] args) {

        StringBuffer sb = new StringBuffer("Java is a programming language : ");

        // use insert(int offset ,Boolean b) 
        // it will insert the Boolean parameter at index 33 
        // as string to the StringBuffer
        sb.insert(33, true);

        // Display result after inserting
        System.out.println("The result will be after inserting Boolean object to the StringBuffer is :" + sb.toString());

        sb = new StringBuffer("Is java support OOPS : ");

        // use insert(int offset , int i) 
        // it will insert the String parameter at index 21 
        // as string to the StringBuffer
        sb.insert(21, "Yes");

        // Display result after inserting
        System.out.println("The result will be after inserting Integer object to the StringBuffer is :" + sb.toString());
    }
}

Output

D:\Programs>javac AddCharactersToString .java

D:\Programs>java AddCharactersToString
The result will be after inserting Boolean object to the StringBuffer is :Java is a programming language : true
The result will be after inserting Integer object to the StringBuffer is :Is Java Support OOPS : Yes

Case 2: How to add characters to a string in java with the help of logic?

With the help of concatenating operator (+) we can add characters to a string. We will see in an example,

Example:

public class AddCharactersToString {

    public static void main(String[] args) {
        String old_string = "Hello , Welcome in Java Worl";
        char added_char = 'd';
        String added_string = "d in version 8";

        old_string = old_string + added_char;
        String old_string1 = old_string + added_string;

        System.out.println(old_string);
        System.out.println(old_string1);
    }
    
}

Output

Output:
D:\Programs>javac AddCharactersToString.java

D:\Programs>java AddCharactersToString
Hello , Welcome in Java World
Hello , Welcome in Java Worldd in version 8





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.