Found 7442 Articles for Java

How to test String is null or empty?

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

562 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

128 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

144 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

84 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

Methods of StringTokenizer class in Java.

Akshaya Akki
Updated on 27-Feb-2020 05:26:24

240 Views

The StringTokenizer class of the java. util package allows an application to break a string into tokens.This class is a legacy class that is retained for compatibility reasons although its use is discouraged in new code.Its methods do not distinguish among identifiers, numbers, and quoted strings.These class methods do not even recognize and skip comments.ExampleLive Demoimport java.util.*; public class Sample {    public static void main(String[] args) {       // creating string tokenizer       StringTokenizer st = new StringTokenizer("Come to learn");       // checking next token       System.out.println("Next token is : " + st.nextToken());    } }OutputNext token is : Come

Constructors of StringTokenizer class in Java.

Anjana
Updated on 30-Jul-2019 22:30:21

249 Views

Following are the important constructors of the StringTokenizer class.Sr.No.Constructor & Description1StringTokenizer(String str)This constructor a string tokenizer for the specified string.2StringTokenizer(String str, String delim)This constructor constructs string tokenizer for the specified string.3StringTokenizer(String str, String delim, boolean returnDelims)This constructor constructs a string tokenizer for the specified string.

How to swap two String variables without third variable.

Akshaya Akki
Updated on 30-Jul-2019 22:30:21

3K+ Views

To swap the contents of two strings (say s1 and s2) without the third, first of all concatenate them and store in s1. Now using the substring() method of the String class store the value of s1 in s2 and vice versa.Example Live Demopublic class Sample {    public static void main(String args[]){       String s1 = "tutorials";       String s2 = "point";       System.out.println("Value of s1 before swapping :"+s1);       System.out.println("Value of s2 before swapping :"+s2);       int i = s1.length();       s1 = s1+s2;       s2 = ... Read More

StringTokenizer class in Java

V Jyothi
Updated on 05-Mar-2020 12:18:39

602 Views

The StringTokenizer class of the java.util package allows an application to break a string into tokens.This class is a legacy class that is retained for compatibility reasons although its use is discouraged in new code.Its methods do not distinguish among identifiers, numbers, and quoted strings.This class methods do not even recognize and skip comments.ExampleLive Demoimport java.util.*;   public class Sample {    public static void main(String[] args) {         // creating string tokenizer       StringTokenizer st = new StringTokenizer("Come to learn");         // checking next token       System.out.println("Next token is : " + st.nextToken());   }     }OutputNext token is : Come

StringTokenizer class in Java

V Jyothi
Updated on 05-Mar-2020 12:18:39

602 Views

The StringTokenizer class of the java.util package allows an application to break a string into tokens.This class is a legacy class that is retained for compatibility reasons although its use is discouraged in new code.Its methods do not distinguish among identifiers, numbers, and quoted strings.This class methods do not even recognize and skip comments.ExampleLive Demoimport java.util.*;   public class Sample {    public static void main(String[] args) {         // creating string tokenizer       StringTokenizer st = new StringTokenizer("Come to learn");         // checking next token       System.out.println("Next token is : " + st.nextToken());   }     }OutputNext token is : Come

Difference between StringBuffer and StringBuilder.

Akshaya Akki
Updated on 30-Jul-2019 22:30:21

1K+ Views

The StringBuffer and StringBuilder classes are used when there is a necessity to make a lot of modifications to Strings of characters.Unlike Strings, objects of type StringBuffer and String builder can be modified over and over again without leaving behind a lot of new unused objects.The StringBuilder class was introduced as of Java 5 and the main difference between the StringBuffer and StringBuilder is that StringBuilder’s methods are not thread safe (not synchronized).It is recommended to use StringBuilder whenever possible because it is faster than StringBuffer. However, if the thread safety is necessary, the best option is StringBuffer objects. Read More

Advertisements