Found 34487 Articles for Programming

Replacing Substrings in a Java String

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

115 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

317 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

510 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

560 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

337 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

Java program to reverse each word in a sentence

Samual Sam
Updated on 25-Jun-2020 13:07:03

864 Views

Each word in a sentence can be reversed and the sentence displayed with the words in the same order as before. An example of this is given as follows −Original sentence = an apple is red Modified sentence = na elppa si derA program that demonstrates this is given as follows.Example Live Demopublic class Example { public static void main(String[] args) { String str = "the sky is blue"; System.out.println("The original string is: " + str); String strWords[] = str.split("\s"); String rev = ""; for(String sw : strWords) { StringBuilder sb = new StringBuilder(sw); sb.reverse(); rev += sb.toString() + " "; ... Read More

Add seconds to current date using Calendar.add() method in Java

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

2K+ Views

Import the following package for Calendar class in Java.import java.util.Calendar;Firstly, create a Calendar object and display the current date and time.Calendar calendar = Calendar.getInstance(); System.out.println("Current Date and Time = " + calendar.getTime());Now, let us increment the seconds using the calendar.add() method and Calendar.SECOND constant.calendar.add(Calendar.SECOND, 15);Example Live Demoimport java.util.Calendar; public class Demo {    public static void main(String[] args) {       Calendar calendar = Calendar.getInstance();       System.out.println("Current Date = " + calendar.getTime());       // Add 15 seconds to current date       calendar.add(Calendar.SECOND, 15);       System.out.println("Updated Date = " + calendar.getTime());    } ... Read More

Advertisements