Found 9316 Articles for Object Oriented Programming

Java Program to compare string using compareTo() method

karthikeya Boyini
Updated on 26-Jun-2020 14:51:24

97 Views

The compareTo(obj) method compares this String to another Object.The value 0 is returned if the argument is a string lexicographically equal to this string; a value less than 0 if the argument is a string lexicographically greater than this string; and a value greater than 0 if the argument is a string lexicographically less than this string.We have the following two strings −String str1 = "tom"; String str2 = "tim";Let us check them for all the return values.if(str1.compareTo(str2) > 0) {  System.out.println("First string is greater!"); } if(str1.compareTo(str2) == 0) {  System.out.println("First string is equal to Second string!"); } if(str1.compareTo(str2)  0) {           System.out.println("First string ... Read More

Java Program to check for equality between two strings ignoring the case

Samual Sam
Updated on 26-Jun-2020 14:54:14

172 Views

Use equalsIgnoreCase() in Java to check for equality between two strings ignoring the case.Let’s say the following are our two strings.String one = "rocky"; String two = "Rocky";Both are equal, but the case is different. Since the method ignore case, both of these strings would be considered equal.Here, we are checking the same.if(one.equalsIgnoreCase(two)) {     System.out.println("String one is equal to two (ignoring the case) i.e. one==two"); }else{     System.out.println("String one is not equal to String two (ignoring the case) i.e. one!=two"); }The following is the complete example.Example Live Demopublic class Demo {     public static void main(String[] args) {        String one = "rocky";       ... Read More

Java Program to compare strings for equality

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

178 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

422 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

630 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

7K+ 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 22-Jul-2024 17:19:53

2K+ Views

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 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 using Java. Input THIS IS DEMO LINE $$$ NEW LINE Output THIS IS DEMO LINE *** NEW LINE Steps to Replace All Occurrences of a Given Character in a String Below are the steps to replace all ... Read More

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

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

204 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

Advertisements