Found 9150 Articles for Object Oriented Programming

Replace Character in a String in Java without using replace() method

Samual Sam
Updated on 26-Jun-2020 09:32:30

8K+ Views

To replace a character in a String, without using the replace() method, try the below logic.Let’s say the following is our string.String str = "The Haunting of Hill House!";To replace character at a position with another character, use the substring() method login. Here, we are replacing 7th position with character ‘p’int pos = 7; char rep = 'p'; String res = str.substring(0, pos) + rep + str.substring(pos + 1);The following is the complete example wherein a character at position 7 is replaced.Example Live Demopublic class Demo {     public static void main(String[] args) {        String str = "The Haunting of Hill House!";        System.out.println("String: "+str);   ... Read More

Java program to replace all occurrences of a given character in a string

karthikeya Boyini
Updated on 19-Nov-2024 18:23:54

3K+ Views

In this article, we will learn to replace all occurrences of a given character in a string in Java. Replace() method The replace() method in Java is used to replace all occurrences of a given character in a string with another character. In this example, we will replace all occurrences of the character $ with *. Use the replace() method to replace all occurrences of a given character in a string. Problem Statement Given a string, replace all occurrences of a specific character with another character in Java. Below is the demonstration of the same − Input THIS IS DEMO LINE ... Read More

Java Program to replace all occurrences of given String with new one

Samual Sam
Updated on 23-Jan-2025 22:53:32

322 Views

In this article, we will learn how to replace all occurrences of a given string with a new one using the replaceAll() method in Java. This method is particularly useful when we want to modify specific parts of a string.   replaceAll() method The replaceAll() method in Java is used to find substrings within a string that match a specified regular expression and replace them with a given string. This method performs a search for all occurrences that match the provided regex pattern and replaces them with the new string.Syntax public String replaceAll(String regex, String replace_str) Steps to replace all occurrences ... Read More

Java program to check the end of a string

karthikeya Boyini
Updated on 02-Aug-2024 18:17:40

3K+ Views

In general, the endswith() method in Java is used to check whether the given string ends with a specific suffix string or not. If the substring ends with the specific string then it will return a Boolean value true, otherwise it will return false if the value is not found. Syntax public boolean endsWith(String suffix) Problem Statement Write a program to check if a string ends with a specific substring in Java − Input str = demo Output The string ends with the word mo Step to check the end of a string Following are the steps to check with ... Read More

Java Program to check the beginning of a string

Shriansh Kumar
Updated on 17-Apr-2025 10:47:18

290 Views

You are given a String and your task is to find its beginning. Here, beginning means starting characters of the string up to a certain position. In Java, String is a class of java.lang package that represents a sequence of characters. These characters are enclosed within double quotes. If you want to access characters in a Java String, use their index. Each character is associated with a numerical index, starting from 0 up to n-1, where n is the length of String. In this article, we will learn how to use the index to find the ... Read More

Java program to remove a character at a specified position

karthikeya Boyini
Updated on 16-Sep-2024 23:26:05

2K+ Views

In this article, we will learn to remove a character from a string at a specified position using Java. We will use the substring() method to achieve this by splitting the string before and after the character we want to remove, then combining the two parts Problem Statement Write a program in Java to remove a character at a specified position. Input The Haunting of Hill House! Output String after removing a character: The Hauting of Hill House! Steps to remove a character at a specified position Following are the steps to remove a character at a specified position − ... Read More

Java Program to remove leading and trailing whitespace from a string

Samual Sam
Updated on 26-Jun-2020 09:22:53

453 Views

Use the Trim() method to remove leading and trailing whitespace from a string. Let’s say the following is our string with white spaces in the beginning and the end.String str = " a ";Now, use the trim() method.str.trim()The following is the final example with output.Example Live Demopublic class Demo {    public static void main(String[] args) {       String str = " a ";       System.out.println("Text = "+str);       System.out.println("Text after using trim() method = " + str.trim());     } }OutputText = a Text after using trim() method = a

Java Program to convert integer to hexadecimal

karthikeya Boyini
Updated on 26-Jun-2020 09:24:33

6K+ Views

Use the + Integer.toHexString() method in Java to convert integer to hexadecimal.Let’s say the following is our integer.int val = 768;Let us convert it to a hexadecimal value.Integer.toHexString(val)The following is the final example with the output.Example Live Demopublic class Demo {     public static void main(String[] args) {        int val = 768;        // integer        System.out.println("Integer: "+val);        // hex        System.out.println("Hex String = " + Integer.toHexString(val));     } }OutputInteger: 768 Hex String = 300

Java program to convert integer to octal

Samual Sam
Updated on 15-Oct-2024 10:39:56

3K+ Views

In Java, converting integers to different numeral systems can be easily achieved using built-in methods. The Integer.toOctalString(int) method allows you to convert an integer to its octal string representation. This is particularly useful in applications that require data representation in octal format. To convert integer to octal in Java, use the Integer.toOctalString() method. Problem Statement Given a set of integer values, we need to convert each integer to its octal string representation using the Integer.toOctalString(int) method. Input int val = 768; Output Octal String = 1400 Step to convert the integer to octal Below are the steps to convert integer ... Read More

Java Program to convert an integer into binary

karthikeya Boyini
Updated on 21-Jun-2024 13:59:18

13K+ Views

To convert an integer to binary, use the Integer.toBinaryString() method in Java.Let’s say the following is the integer we want to convert.int val = 999;Converting the above integer to binary.Integer.toBinaryString(val)Example Live Demopublic class Demo {     public static void main(String[] args) {        int val = 999;        // integer        System.out.println("Integer: "+val);        // binary        System.out.println("Binary = " + Integer.toBinaryString(val));     } }OutputInteger: 999 Binary = 1111100111

Advertisements