Found 33676 Articles for Programming

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

183 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

331 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

317 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

How to test String is null or empty?

V Jyothi
Updated on 26-Feb-2020 08:05:55

565 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

Java string toUpperCase() method example

Arjun Thakur
Updated on 07-Nov-2024 17:48:23

131 Views

In this article, we will learn how to convert all characters in a string to uppercase using the toUpperCase() method in Java. This method has two versions: one that uses the system's default language settings and another that lets you choose a specific language. Changing string characters to uppercase helps keep the text consistent, especially for usernames or titles. Problem StatementGiven a string, write a Java program to convert all its characters to uppercase using the toUpperCase() method.Input Initial String = "Welcome to Tutorialspoint.com"Output Converted String = "WELCOME TO TUTORIALSPOINT.COM" Steps to convert a string to uppercase The ... Read More

Java String contains() method example.

Manikanth Mani
Updated on 30-Jul-2019 22:30:21

146 Views

The contains() method of the String class returns true if and only if this string contains the specified sequence of char values.Example Live Demoimport java.lang.*; public class StringDemo {    public static void main(String[] args) {       String str1 = "tutorials point", str2 = "http://";       CharSequence cs1 = "int";             //string contains the specified sequence of char values       boolean retval = str1.contains(cs1);       System.out.println("Method returns : " + retval);             //string does not contain the specified sequence of char value       retval = str2.contains("_");       System.out.println("Methods returns: " + retval);    } }OutputMethod returns : true Methods returns: false

Java String toCharArray() method example.

Sai Nath
Updated on 26-Feb-2020 08:16:01

85 Views

The toCharArray() method of a String class converts this string to a character array.ExampleLive Demoimport java.lang.*; public class StringDemo {    public static void main(String[] args) {       // converts String value to character array type value       String str = " Java was developed by James Gosling";       char retval[] = str.toCharArray();       // displays the converted value       System.out.println("Converted value to character array = ");       System.out.println(retval);    } }OutputConverted value to character array = Java was developed by James Gosling

Advertisements