- 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 34484 Articles for Programming
2K+ Views
You can capitalize words in a string using the toUpperCase() method of the String class. This method converts the contents of the current string to Upper case letters.Example Live Demopublic class StringToUpperCaseEmp { public static void main(String[] args) { String str = "string abc touppercase "; String strUpper = str.toUpperCase(); System.out.println("Original String: " + str); System.out.println("String changed to upper case: " + strUpper); } }OutputOriginal String: string abc touppercase String changed to upper case: STRING ABC TOUPPERCASE
7K+ Views
The replaceAll() method of the String class replaces each substring of this string that matches the given regular expression with the given replacement. You can remove white spaces from a string by replacing " " with "".ExampleLive Demoimport java.io.*; public class Test { public static void main(String args[]) { String Str = new String("Hi how are you Welcome to Tutorialspoint.com"); System.out.print("Return Value :" ); System.out.println(Str.replaceAll(" ", "")); } }OutputReturn Value :HihowareyouWelcometoTutorialspoint.com
2K+ Views
The charAt() method of the String class returns the char value at the specified index. An index ranges from 0 to length() - 1. The first char value of the sequence is at index 0, the next at index 1, and so on, as for array indexing.The indexOf(int ch, int fromIndex) method of the String class returns the index within this string of the first occurrence of the specified character, starting the search at the specified index.If a character with value ch occurs in the character sequence represented by this String object at an index no smaller than fromIndex, then the index of ... Read More
233 Views
The parseXxx() method is used to get the primitive data type of a certain String. In this case to convert a String to integer you could use the parseInt() method.Example Live Demopublic class Test { public static void main(String args[]) { int x =Integer.parseInt("9"); double c = Double.parseDouble("5"); int b = Integer.parseInt("444",16); System.out.println(x); System.out.println(c); System.out.println(b); } }Output9 5.0 1092
2K+ 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.Example Live 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
339 Views
The replace() method of the returns a new string resulting from replacing all 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
215 Views
Following example shows how to remove a character from a particular position from a string with the help of removeCharAt(string, position) method.ExampleLive Demopublic class Sample { public static void main(String args[]) { String str = "this is Java"; System.out.println(removeCharAt(str, 3)); } public static String removeCharAt(String s, int pos) { return s.substring(0, pos) + s.substring(pos + 1); } }Outputthis is Java
376 Views
The hasMoreTokens() method is used to test if there are more tokens available from this tokenizer's string.ExampleLive Demoimport java.util.*; public class StringTokenizerDemo { public static void main(String[] args) { // creating string tokenizer StringTokenizer st = new StringTokenizer("Come to learn"); // checking elements while (st.hasMoreElements()) { System.out.println("Next element : " + st.nextElement()); } } }OutputNext element : Come Next element : to Next element : learn
145 Views
A switch statement allows a variable to be tested for equality against a list of values. Each value is called a case, and the variable being switched on is checked for each case.Example Live Demopublic class Test { public static void main(String args[]) { // char grade = args[0].charAt(0); char grade = 'C'; switch(grade) { case 'A' : System.out.println("Excellent!"); break; case 'B' : case 'C' : ... Read More
82 Views
We can compare Strings in Java using the compareTo() method and the == operator.comapareTo() method: The compareTo() method compares two strings lexicographically. The comparison is based on the Unicode value of each character in the strings. The character sequence represented by this String object is compared lexicographically to the character sequence represented by the argument string.The == operator: You can compare two strings using == operator. But, it compares references to the given variables, not values.Example Live Demoimport java.lang.*; public class StringDemo { public static void main(String[] args) { String str1 = "tutorials", str2 = "point"; ... Read More