Found 7442 Articles for Java

Java program to convert a string to lowercase and uppercase.

Alshifa Hasnain
Updated on 30-Dec-2024 19:15:18

1K+ Views

In Java, converting a string to lowercase or uppercase is a common operation used in text processing, formatting, and case normalization. The String class provides built-in methods to achieve this with ease. This article explores how to use these methods with practical examples, demonstrating step-by-step how to convert strings between uppercase and lowercase. Key Methods toLowerCase(): Converts all characters in a string to lowercase. toUpperCase(): Converts all characters in a string to uppercase. These methods are part of the java.lang.String class and work seamlessly for any string input. Using toLowerCase() ... Read More

Converting to upper case in Java.

Ramu Prasad
Updated on 26-Feb-2020 09:41:12

271 Views

This method has two variants. The first variant converts all of the characters in this String to upper case using the rules of the given Locale. This is equivalent to calling toUpperCase(Locale.getDefault()).The second variant takes a locale as an argument to be used while converting into upper case.ExampleLive Demoimport java.io.*; public class Test {    public static void main(String args[]) {       String Str = new String("Welcome to Tutorialspoint.com");       System.out.print("Return Value :");       System.out.println(Str.toUpperCase());    } }OutputReturn Value :WELCOME TO TUTORIALSPOINT.COM

How to Split a String with Escaped Delimiters?

Sravani S
Updated on 26-Feb-2020 09:42:44

444 Views

The split(String regex) method of the String class splits this string around matches of the given regular expression.ExampleLive Demopublic class Sample{    public static void main(String args[]){       String s = "|A|BB||CCC|||";       String[] words = s.split("\|");       for (String string : words) {          System.out.println(string);       }    } }OutputA BB CCC

How to Split String in Java using Regular Expression?

V Jyothi
Updated on 21-Aug-2024 00:03:07

6K+ Views

In this article, we will learn to split a string in Java using regular expressions. We will be using the split(String regex) method of the String class to split this string around matches of the given regular expression. This method works in the same way as invoking the method i.e. split(String regex, int limit) with the given expression and a limit argument of zero. Therefore, trailing empty strings are not included in the resulting array. Steps to split string in Java using regular expression Following are the steps to split strings in Java using regular expression − ... Read More

Java StringTokenizer and String Split Example

Shriansh Kumar
Updated on 16-Aug-2024 08:04:45

2K+ Views

Both StringTokenizer class and the split() method in Java are used to divide a string into tokens or substrings. However, they are different from each other. The StringTokenizer class does not support regular expressions, whereas the split() method works with regular expressions. In this article, we will see some Java examples that show how to split strings using them. Example Scenario: Input: str = "simple easy learning" Output: split_str = "simple", "easy", "learning" Splitting String using StringTokenizer Class The StringTokenizer is a legacy class of java.util package. This class provides methods to break a string into multiple tokens. It ... Read More

What is the correct way to check if String is empty in Java?

Jai Janardhan
Updated on 26-Feb-2020 09:43:28

180 Views

We can verify whether the given string is empty using the isEmpty() method of the String class. This method returns true only if length() is 0.ExampleLive Demoimport java.lang.*; public class StringDemo {    public static void main(String[] args) {       String str = "tutorialspoint";       // prints length of string       System.out.println("length of string = " + str.length());       // checks if the string is empty or not       System.out.println("is this string empty? = " + str.isEmpty());    } }Outputlength of string = 14 is this string empty? = false

What is the difference between replace() and replaceAll() in Java?

Govinda Sai
Updated on 26-Feb-2020 10:04:59

2K+ Views

The replace method of the String class accepts two characters and it replaces all the occurrences of oldChar in this string with newChar.Exampleimport java.io.*; public class Test {    public static void main(String args[]) {       String Str = new String("Welcome to Tutorialspoint.com");       System.out.print("Return Value :" );       System.out.println(Str.replace('o', 'T'));       System.out.print("Return Value :" );       System.out.println(Str.replace('l', 'D'));    } }OutputReturn Value :WelcTme tT TutTrialspTint.cTm Return Value :WeDcome to TutoriaDspoint.comThe replaceAll() method replaces each substring of this string that matches the given regular expression with the given replacement.Exampleimport java.io.*; ... Read More

Checking for Null or Empty in Java.

Aishwarya Naglot
Updated on 04-Aug-2025 18:24:21

13K+ Views

A string is a collection of characters. In Java, it is represented by the String class, and it accepts NULL values. Checking for Null or Empty in Java In this article, we will learn if a string is null or empty in Java. The following are the ways to check if a string is null or empty in Java: Using isEmpty() Method Using length() Method Using isBlank() Method Using isEmpty() Method The isEmpty() method of the String class checks if a string is empty. It returns TRUE if ... Read More

How to replace characters on String in Java?

Ramu Prasad
Updated on 30-Jul-2019 22:30:21

329 Views

The replace method of the String class accepts two characters and it replaces all the occurrences of oldChar in this string with newChar.Example Live Demoimport java.io.*; public class Test {    public static void main(String args[]) {       String Str = new String("Welcome to Tutorialspoint.com");       System.out.print("Return Value :" );       System.out.println(Str.replace('o', 'T'));       System.out.print("Return Value :" );       System.out.println(Str.replace('l', 'D'));    } }OutputReturn Value :WelcTme tT TutTrialspTint.cTm Return Value :WeDcome to TutoriaDspoint.com

How to check if String is empty in Java?

Arushi
Updated on 26-Feb-2020 08:24:12

315 Views

The isEmpty() method of the String class returns true if the length of the current string is 0.ExampleLive Demoimport java.lang.*; public class StringDemo {    public static void main(String[] args) {       String str = "tutorialspoint";       //prints length of string       System.out.println("length of string = " + str.length());       //checks if the string is empty or not       System.out.println("is this string empty? = " + str.isEmpty());    } }Outputlength of string = 14 is this string empty? = false

Advertisements