Found 34494 Articles for Programming

Java Regex to extract maximum numeric value from a string

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

478 Views

The maximum numeric value is extracted from an alphanumeric string. An example of this is given as follows −String = abcd657efgh234 Maximum numeric value = 657A program that demonstrates this is given as follows −Example Live Demoimport java.util.regex.Matcher; import java.util.regex.Pattern; public class Example {  public static void main (String[] args) {     String str = "123abc874def235ijk999";     System.out.println("The string is: " + str);     String regex = "\d+";     Pattern ptrn = Pattern.compile(regex);     Matcher match = ptrn.matcher(str);     int maxNum = 0;     while(match.find()) {        int num = Integer.parseInt(match.group());        if(num > maxNum) {           maxNum = num;        }     }     System.out.println("The maximum numeric value in above string is: " + maxNum);  } }OutputThe string is: 123abc874def235ijk999 ... Read More

Java Program to get a character located at the String's specified index

Samual Sam
Updated on 26-Jun-2020 14:49:13

374 Views

Use the charAt() method in Java to get a character located at a specified index.Let’s say the following is our string.String str = "Laptop...";Finding character at 3rd position.str.charAt(2);The following is the final example.Example Live Demopublic class Demo {     public static void main(String[] args) {        String str = "Laptop...";        System.out.println("String: "+str);        System.out.println("Character at position 3 = " +str.charAt(2));     } }OutputString: Laptop... Character at position 3 = p

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

176 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

629 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

Advertisements