Found 9326 Articles for Object Oriented Programming

Java Program to replace only first occurrences of given String with new one

karthikeya Boyini
Updated on 25-Jun-2020 13:19:35

204 Views

Use the replaceFirst() method to replace only first occurrences of given String with new one.Let’s say we have the following string.String str = "THIS IS DEMO TEXT!";We have to replace the first occurrence of “IS” with “EV”. For that, use replaceFirst() method.str.replaceFirst("IS", "EV");The following is the final example, wherein the first occurrence of “IS” is replaced.Example Live Demopublic class Demo {    public static void main(String[] args) {       String str = "THIS IS DEMO TEXT!";       System.out.println("String = "+str);       System.out.println("Replacing only the first occurrence of substring IS...");       System.out.println("Updated string = ... Read More

Java Program to replace all occurrences of a given character with new character

Samual Sam
Updated on 25-Jun-2020 13:21:05

289 Views

Let’s say the following is our string.THIS IS DEMO TEXT!Here, to replace every occurrence of ‘I’ with ‘E’, use the replace() method.str.replace('I', 'E'));The following is the complete example to replace all occurrences of a given character with new character.Example Live Demopublic class Demo {    public static void main(String[] args) {       String str = "THIS IS DEMO TEXT!";       System.out.println("String = "+str);       System.out.println("Updated string = "+str.replace('I', 'E'));    } }OutputString = THIS IS DEMO TEXT! Updated string = THES ES DEMO TEXT!

Replacing Substrings in a Java String

karthikeya Boyini
Updated on 25-Jun-2020 13:23:11

112 Views

Let’s say the following is our string.String str = "The Walking Dead!";We want to replace the substring “Dead” with “Alive”. For that, let us use the following logic. Here, we have used a while loop and within that found the index of the substring to be replaced. In this way, one by one we have replaced the entire substring.int beginning = 0, index = 0; StringBuffer strBuffer = new StringBuffer(); while ((index = str.indexOf(subStr1, beginning)) >= 0) {    strBuffer.append(str.substring(beginning, index));    strBuffer.append(subStr2);    beginning = index + subStr1.length(); }The following is the complete example to replace substrings.Example Live Demopublic class ... Read More

Java Program to remove whitespace from the beginning and end of a string

karthikeya Boyini
Updated on 25-Jun-2020 13:24:39

312 Views

Use the trim() method to remove whitespace from the beginning and end of a string.Let’s say the following is our string with whitespace.str1 = " The Big Bang Theory ";Now, let us trim all the whitespaces from the beginning and the end.String str2 = str1.trim();The following is the final example with output.Example Live Demopublic class Demo {    public static void main(String[] args) {       String str1 = " The Big Bang Theory ";       System.out.println("String: "+str1);       String str2 = str1.trim();       System.out.println("Updated string (trimming spaces): "+str2);    } }OutputString: The Big ... Read More

Java Program to replace one specific character with another

Samual Sam
Updated on 25-Jun-2020 13:26:35

507 Views

Use the replace() method to replace a specific character with another. Let’s say the following is our string and here we are replacing a whitespace with a $ character.String str1 = "Orange is the new Black!";Now, use the replace() method to replace a character with $str1.replace(' ', '$');Example Live Demopublic class Demo {    public static void main(String[] args) {       String str1 = "Orange is the new Black!";       System.out.println("String: "+str1);       String str2 = str1.replace(' ', '$');       System.out.println("Updated string: "+str2);    } }OutputString: Orange is the new Black! Updated string: ... Read More

Java Program to access character of a string

karthikeya Boyini
Updated on 14-Jun-2024 14:44:43

31K+ Views

To access character of a string in Java, use the charAt() method. The position is to be added as the parameter.Let’s say we have the following string −String str = "laptop";Let’s find the character at the 4th position using charAt() method.str.charAt(3));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);       // finding character at 4th position       System.out.println("Character at 4th position: "+str.charAt(3));    } }OutputString: laptop Character at 4th position: t

Java program to reverse an array upto a given position

Samual Sam
Updated on 25-Jun-2020 13:29:46

553 Views

An array can be reversed upto the given position pos and the remaining array is unchanged. An example of this is given as follows −Array = 1 2 3 4 5 6 7 8 9 10 Position = 6 Modified array = 6 5 4 3 2 1 7 8 9 10A program that demonstrates this is given as follows −Example Live Demopublic class Example {    public static void main(String args[]) {       int[] arr = {1, 2, 3, 4, 5, 6, 7 ,8 ,9, 10};       int n = arr.length;       int pos ... Read More

Java program to check for URL in a String

karthikeya Boyini
Updated on 25-Jun-2020 13:31:17

1K+ Views

A program can be created to check if a string is a correct URL or not. An example of an URL is given as follows −String = https://www.wikipedia.org/ The above string is a valid URLA program that demonstrates this is given as follows.Example Live Demoimport java.net.URL; public class Example {    public static boolean check_URL(String str) {    try {       new URL(str).toURI();       return true;    }catch (Exception e) {       return false;    } } public static void main(String[] args) {    String str = "http://www.wikipedia.org/";    System.out.println("String = " + str);   ... Read More

Java program to check whether a given string is Heterogram or not

Samual Sam
Updated on 25-Jun-2020 13:02:12

336 Views

A string is a Heterogram if no letter of the alphabet occurs more than once in it. An example of this is given as follows −String = the big dwarf only jumpsThis string is a Heterogram as each alphabet in the string occurred only once.A program that demonstrates this is given as follows.Example Live Demopublic class Example {    public static void main (String[] args) {       String str = "mango";       int n = str.length();       int alphaList[] = new int[26];       int flag = 1;       System.out.println("The string is: ... Read More

Check if a String is not empty ("") and not null in Java

karthikeya Boyini
Updated on 25-Jun-2020 13:03:23

2K+ Views

Let’s say we have the following string −String myStr1 = "Jack Sparrow";Let us check the string now whether it is not null or not empty.if(myStr != null || myStr.length() != 0) { System.out.println("String is not null or not empty");Example Live Demopublic class Demo {    public static void main(String[] args) {       String myStr = "Jack Sparrow";       boolean res;       if(myStr != null || myStr.length() != 0) {          System.out.println("String is not null or not empty");       } else {          System.out.println("String is null or empty");       }    } }OutputString is not null or not empty

Advertisements