Found 33676 Articles for Programming

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

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

444 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

154 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

Revathi Satya Kondra
Updated on 31-Dec-2024 11:18:24

556 Views

In this article, we use the trim() method to remove whitespace at the beginning and end of a string. This method does not modify the original string; instead, it returns a new string with the leading and trailing whitespace removed. Let’s say the following is our string with whitespace − string = " Tutorialspoint "; Now, let us trim all the whitespaces from the beginning and the end. String trimmed = string.trim(); Removing whitespace using trim () Method The trim() method in Java is a built-in function of the String class that removes ... Read More

Java Program to replace one specific character with another

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

591 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 11-Nov-2024 19:16:55

35K+ Views

In this article, we will access a specific character from a string by using the charAt() method in Java. The program will demonstrate how to locate and display a character at a specified position within a string. Problem Statement We have a string, and we need to retrieve the character at a given position. For example, if we use the string "laptop", we want to display the character located at the 4th position (0-based index). Input "laptop" Output String: laptop Character at 4th position: t Steps to access character of a string The following an steps to access the ... Read More

Java program to reverse an array upto a given position

Samual Sam
Updated on 15-Oct-2024 11:54:38

881 Views

In this article, we will reverse the elements of an array up to a specified position using Java. You will learn how the program takes an array, reverses the first part of it up to a given index, and prints the modified array. An array can be reversed upto the given position pos and the remaining array is unchanged.Problem Statement Write a Java program to reverse an array upto a given position.Input Array = 1 2 3 4 5 6 7 8 9 10 Position = 6 Output Modified array is: 6 5 4 3 2 1 7 8 9 ... Read More

Java program to check for URL in a string

karthikeya Boyini
Updated on 07-Aug-2024 21:46:44

2K+ Views

In general, to check if a given string is a valid URL(Uniform Resource Locator), we will create a method that tries to form a URL object and catches any exceptions to determine if the string is a valid URL. By using Java's URL class and exception handling, we will demonstrate a straightforward approach to verify the correctness of URL strings. We can import from java.net package object is created from the input string, then converted to a URI using toURI() method. Problem Statement A program can be created to check if a string is a correct URL or not. An ... Read More

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

Alshifa Hasnain
Updated on 17-Dec-2024 03:31:34

511 Views

A heterogram is a string where each letter appears only once, with no repeated characters. This property makes it unique and an interesting problem to solve in Java. In this article, we will learn to determine whether a given string is a heterogram using a frequency-based approach and introduce an alternative using a Set. Set A Set is a type of Collection designed to hold unique elements, ensuring no duplicates are present. It reflects the concept of a mathematical set and includes methods from the Collection interface while enforcing this rule of uniqueness. Using an Array for Character Frequency  This ... 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

Shriansh Kumar
Updated on 25-Sep-2024 18:27:47

2K+ Views

Given a sentence or string, write a Java program to reverse each word in that sentence. Each word in that sentence should be reversed and at the same time, they should be in the same order as before. Let's understand the problem with an example − Example Scenario: Input: orgnl_sen = an apple is red Output: modfd_sen = na elppa si der Using Iteration First, split the given sentence into an array of words using the space character as the delimiter. Use a for-each loop to iterate over each word. Then, iterate through the characters of each ... Read More

Advertisements