Found 7442 Articles for Java

Java program to program to cyclically rotate an array by one

Samual Sam
Updated on 25-Jun-2020 13:18:13

1K+ Views

The array is cyclically rotated clockwise by one. This means that each of the array elements are displayed to the right by one and the last element ends up as the first element. An example of this is given as follows.Original array = 1 2 3 4 5 6 7 8 9 10 Rotated array = 10 1 2 3 4 5 6 7 8 9A 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, ... Read More

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

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

330 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

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

153 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

553 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

590 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

875 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

508 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

Advertisements