Found 7442 Articles for Java

Java Program to compare strings for equality

karthikeya Boyini
Updated on 26-Jun-2020 14:31:46

243 Views

To compare string for equality in Java, use the equals() method. Let us see some examples wherein we have checked for same as well as different string values.Example Live Demopublic class Demo {     public static void main(String[] args) {        String one = "x";        String two = "x";        if(one.equals(two)) {           System.out.println("String one is equal to two i.e. one==two");        }else{           System.out.println("String one is not equal to String two i.e. one!=two");        }     } }OutputString one is equal to two i.e. one==twoLet us see another example.Example Live Demopublic class Demo {     public static void main(String[] args) { ... Read More

Java Program to concatenate Integers and a String

Samual Sam
Updated on 26-Jun-2020 14:34:31

516 Views

To concatenate integers with a string value, you need to use the + operator.Let’s say the following is the string.String str = "Demo Text";Now, we will concatenate integer values with the above string.String res = str + 1 + 2 + 3 + 4 + 5;Example Live Demopublic class Demo {     public static void main(String[] args) {        String str = "Demo Text";        System.out.println("String = "+str);        String res = 99 + 199 + 299 + 399 + str;        System.out.println(res);     } }OutputString = Demo Text 996Demo Text

Concatenate null to a string in Java

karthikeya Boyini
Updated on 26-Jun-2020 14:36:18

3K+ Views

To concatenate null to a string, use the + operator.Let’s say the following is our string.String str = "Demo Text";We will now see how to effortlessly concatenate null to string.String strNULL = str + "null";The following is the final example.Example Live Demopublic class Demo {     public static void main(String[] args) {        String str = "Demo Text";        System.out.println("String = "+str);        String strNULL = str + "null";        System.out.println("String concatenated with NULL: "+strNULL);     } }OutputString = Demo Text String concatenated with NULL: Demo Textnull

Get all digits from a string in Java

Samual Sam
Updated on 26-Jun-2020 14:38:18

1K+ Views

Let’s say the following is our string.String str = "DEMO98 TE4567XT";To display only the digits from the above string, we have used the replaceAll() method and replace all the characters with empty.str.replaceAll("\D", ""))The following is the final example that displays only digits from the string.Example Live Demopublic class Demo {    public static void main(String[] args) {       String str = "DEMO98 TE4567XT";       System.out.println("String = "+str);       System.out.println("Displaying digits: "+str.replaceAll("\D", ""));  } }OutputString = DEMO98 TE4567XT Displaying digits: 984567

Replace first occurrence of a character in Java

karthikeya Boyini
Updated on 26-Jun-2020 14:40:01

8K+ Views

To replace the first occurrence of a character in Java, use the replaceFirst() method.Here is our string.String str = "The Haunting of Hill House!";Let us replace the first occurrence of character “H”str.replaceFirst("(?:H)+", "B");The following is the complete example.Example Live Demopublic class Demo {     public static void main(String[] args) {        String str = "The Haunting of Hill House!";        System.out.println("String: "+str);        String res = str.replaceFirst("(?:H)+",  "B");        System.out.println("String after replacing a character's first occurrence: "+res);     } }OutputString: The Haunting of Hill House! String after replacing a character's first occurance: The Baunting of Hill House!Read More

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

323 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

292 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

Advertisements