- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Found 9321 Articles for Object Oriented Programming
![Ramu Prasad](https://www.tutorialspoint.com/assets/profiles/13539/profile/60_87996-1512650215.jpg)
218 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
![Arushi](https://www.tutorialspoint.com/assets/profiles/13510/profile/60_55588-1512537391.jpg)
221 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
![V Jyothi](https://www.tutorialspoint.com/assets/profiles/13530/profile/60_77592-1512648869.jpg)
451 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
![Arjun Thakur](https://www.tutorialspoint.com/assets/profiles/13574/profile/60_74441-1560315595.jpg)
45 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
![Manikanth Mani](https://www.tutorialspoint.com/assets/profiles/13524/profile/60_53242-1512705034.jpg)
80 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
![Sai Nath](https://www.tutorialspoint.com/assets/profiles/13525/profile/60_63072-1512539005.jpg)
35 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
![Akshaya Akki](https://www.tutorialspoint.com/assets/profiles/13526/profile/60_48621-1512703980.jpg)
138 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
![Anjana](https://www.tutorialspoint.com/assets/profiles/13774/profile/60_78029-1512648681.jpg)
152 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.
![Akshaya Akki](https://www.tutorialspoint.com/assets/profiles/13526/profile/60_48621-1512703980.jpg)
2K+ 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
![V Jyothi](https://www.tutorialspoint.com/assets/profiles/13530/profile/60_77592-1512648869.jpg)
413 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