Replace character in string Java
Characters in Java
In the package of Java language, there is a container class called Character. A single field of type char is contained in a Character object. For manipulating characters, Character class gives us much useful class (i.e., static) methods. The Character function Object can be used to generate a Character object.
Creating character object
Character ch = new Character('a');
The preceding code generates a Character object with the char type 'a'. The Character class has only one function Object that takes a char data type argument.
When we send a simple character into a method that expects an object, then the compiler turns it to a Char class object automatically. Autoboxing and Unboxing are the terms for this feature.
Methods in the character class
- Boolean isLetter (char ch):-
This method tells us whether or not the supplied char value is a letter. If it's letter([A-Z],[a-z]), the procedure will return true; otherwise, it'll return false. As char to int is automatically typecasted in java, we may pass an ASCII value also as an input in place of a character.
Syntax:
boolean isLetter(char ch);
- Boolean isDigit(char ch):-
This method estimates whether or not the supplied char value(ch) is a digit. We can also pass an ASCII value as an input here.
Syntax:
boolean isDigit(char ch)
- Boolean isWhitespace(char ch):-
It checks to see if the given char value is white space. Space, tab, and newline are examples of whitespace.
Syntax:
boolean isWhitespace(char ch)
- boolean isUpperCase(char ch):-
It checks whether or not the provided char value is uppercase.
Syntax:
boolean isUpperCase(char ch);
- char toUpperCase(char ch):
It returns the provided char value's uppercase. If an ASCII value is supplied, the uppercase ASCII value will be returned.
Syntax:
char toUpperCase(char ch);
- boolean isLowerCase(char ch):-
It checks whether or not the provided char value is lowercase.
Syntax:
boolean isLowerCase(char ch);
- toString(char ch):-
It provides a String class object that represents the char value, which is a one-character string. We can't pass an ASCII value here.
Syntax:
String toString(char ch);
What is a String?
Strings are a collection of characters that are commonly used in Java programming. Strings are regarded as objects in the Java programming language.
“String” is a Java platform class that allows you to construct and handle strings.
Creating Strings
The normal way to create a string in Java is to simply write:-
String s = “Hello World”;
The compiler constructs a String object with the value "Hello world!" whenever it detects a string phrase in your code.
String objects cannot be made by using the new keyword and a function Object, just like any other object. The String class provides 11 constructors that allow you to set the string's initial value from a variety of sources, including an array of characters.
Another way to create a string is :-
String s = new String (“Hello World”);
String Builder:
The char value, which is a one-character string, is represented by a String class object. We can't use an ASCII value in this case.
Syntax:
StringBuilder str = new StringBuilder();
str.append("GFG");
String Buffer:-
StringBuffer is a String companion class that provides a lot of the same functionality as strings. StringBuffer shows growable and writable character sequences, whereas string represents fixed-length, immutable character sequences.
Syntax:
StringBuffer s = new StringBuffer("Hello World");
String Tokenizer:-
The StringTokenizer class in Java is used to tokenize a string.
String Joiner:-
String Joiner is a java.util package class that is used to build a sequence of characters divided by a delimiter and optionally starting with a transferred prefix and finishing with a transferred suffix.
Syntax:
public StringJoiner(CharSequence delimiter);
As we study in the above part, we have sufficient knowledge about replacing a character in a string. So, there are a few ways to replace characters in a string:
String Builder:
In contrast to the String Class, the StringBuilder class includes a built-in function for this — setCharAt (). By calling this function and giving the character and the index as parameters, you can replace the character at a given index.
Now take a look at an example:
public class GFG {
public static void main(String args[])
{
String str = "Hello World";
int index = 3;
char ch = 'F';
System.out.println("Original String = " + str);
StringBuilder string = new StringBuilder(str);
string.setCharAt(index, ch);
System.out.println("Modified String = " + string);
}
}
OUTPUT:
Original String: Hello World
New String: Hello World
String Buffer:
The StringBuffer class, like StringBuilder, provides a predefined method for this - setCharAt (). By calling this function and giving the character and the index as parameters, you can replace the character at a given index. Thread-safe StringBuffer When compared to StringBuffer, StringBuilder is faster, but it is not thread-safe.
Example:
public class GFG {
public static void main(String args[])
{
String str = “Hello World";
int index = 6;
char ch = 'F';
System.out.println("Original String = " + str);
StringBuffer string = new StringBuffer(str);
string.setCharAt(index, ch)
System.out.println("Modified String = " + string);
}
}
OUTPUT:
Original String: Hello World
New String: Hello World