Found 34494 Articles for Programming

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

karthikeya Boyini
Updated on 26-Jun-2020 09:35:47

2K+ Views

Use replace() method to replace all occurrences of a given character in a string.Here is our string.String str = "THIS IS DEMO LINE $$$ NEW LINE";Let us now replace all occurrences of character $ with *str.replace("$", "*")The following is the complete example that replace all occurrences of $ with *Example Live Demopublic class Demo {     public static void main(String[] args) {        String str = "THIS IS DEMO LINE $$$ NEW LINE";        System.out.println("String = "+str);        System.out.println("Replacing all occurrence of given character...");        System.out.println("Updated string = "+str.replace("$",  "*"));     } }OutputString = THIS IS DEMO LINE $$$ NEW LINE Replacing ... Read More

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

Samual Sam
Updated on 26-Jun-2020 09:38:42

203 Views

User replaceAll() method to replace all occurrences of a given string. Let’s say the following is our string.String str = "THIS IS DEMO LINE AND NEW LINE!";Here, we are replacing all occurrences of string “LINE” to “TEXT”str.replaceAll("LINE", "TEXT");The following is the final example.Example Live Demopublic class Demo {    public static void main(String[] args) {       String str = "THIS IS DEMO LINE AND NEW LINE!";       Sytem.out.println("String = "+str);       System.out.println("Replacing all occurrence of string LINE...");       System.out.println("Updated string = "+str.replaceAll("LINE",  "TEXT"));     } }OutputString = THIS IS DEMO LINE AND NEW LINE! Replacing all occurrence of string LINE... Updated string = ... Read More

Java Program to check the end of a string

karthikeya Boyini
Updated on 26-Jun-2020 09:45:42

2K+ Views

To check the end of a string, use the endsWith() method.Let’s say the following is our string.String str = "demo";Now, check for the substring “mo” using the endsWith() method in an if-else condition.if(str.endsWith("mo")) {    System.out.println("The string ends with the word mo"); } else {    System.out.println("The string does not end with the word mo"); }The following is the complete example with output.Example Live Demopublic class Demo {     public static void main(String[] args) {        String str = "demo";        if(str.endsWith("mo")) {           System.out.println("The string ends with the word mo");        } else {           System.out.println("The string does not ... Read More

Java Program to check the beginning of a string

Samual Sam
Updated on 10-Aug-2023 12:52:28

159 Views

A string is a class in Java that stores a series of characters enclosed within double quotes. Those characters are actually String-type objects. The string class is available in the 'java.lang' package. Suppose we have given a string and our task is to find the beginning of that string. Let's assume the beginning as the first two or three characters of that string. To check the beginning of a string, we can use several built-in methods available in Java including substring() and charAt(). Java Program to Check the Beginning of a String To check the beginning of a string we ... Read More

Java Program to remove a character at a specified position

karthikeya Boyini
Updated on 26-Jun-2020 09:21:31

663 Views

To remove a character at a specified position, use the following logic −Let’s say the following is our string.String str = "The Haunting of Hill House!";We want to remove a character at position 7. For that, use the below logic.// removing character at position 7 int pos = 7; String res = str.substring(0, pos) + str.substring(pos + 1);The following is the complete example with output.Example Live Demopublic class Demo {     public static void main(String[] args) {        String str = "The Haunting of Hill House!";        System.out.println("String: "+str);        // removing character at position 7        int pos = 7;        String res = str.substring(0,  pos) + str.substring(pos + 1); ... Read More

Java Program to remove leading and trailing whitespace from a string

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

394 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 26-Jun-2020 09:26:39

1K+ Views

To convert integer to octal in Java, use the Integer.toOctalString() method.Let’s say the following is our integer.int val = 768;Convert the above integer to octal.Integer.toOctalString(val)The following is the final example.Example Live Demopublic class Demo {     public static void main(String[] args) {        int val = 768;        // integer        System.out.println("Integer: "+val);        // octal        System.out.println("Octal String = " + Integer.toOctalString(val));     } }OutputInteger: 768 Octal String = 1400

Java Program to convert an integer into binary

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

9K+ 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

Java NumberFormat.getPercentageInstance() method

Samual Sam
Updated on 26-Jun-2020 09:09:02

385 Views

The java.text.NumberFormat class is used for formatting numbers and currencies as per a specific Locale. Number formats varies from country to countryTo format a number as percentage you need a percentage NumberFormat instance. To achieved this, use the getPercentageInstance() method.Example Live Demoimport java.text.NumberFormat; import java.util.Locale; public class MainClass {    public static void main(String[] args) {        // Currency of France is Euro        NumberFormat n = NumberFormat.getPercentInstance(Locale.FRANCE);        // points        double points = 1.78;        double totalPoints = points * 1000;        System.out.println(n.format(points));        System.out.println(n.format(totalPoints));     } }Output178 % 178 000 %

Advertisements