- 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 34489 Articles for Programming
![Anjana](https://www.tutorialspoint.com/assets/profiles/13774/profile/60_78029-1512648681.jpg)
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
![Manikanth Mani](https://www.tutorialspoint.com/assets/profiles/13524/profile/60_53242-1512705034.jpg)
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
![Abhinanda Shri](https://www.tutorialspoint.com/assets/profiles/13552/profile/60_73264-1512711555.jpg)
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
![Govinda Sai](https://www.tutorialspoint.com/assets/profiles/13542/profile/60_86497-1512650655.jpg)
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
![Ramu Prasad](https://www.tutorialspoint.com/assets/profiles/13539/profile/60_87996-1512650215.jpg)
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
![V Jyothi](https://www.tutorialspoint.com/assets/profiles/13530/profile/60_77592-1512648869.jpg)
89 Views
You can compare two strings using == operator. But, it compares references of the given variables, not values.ExampleLive Demopublic class Sample { public static void main(String args[]){ String str1 = "hello"; String str2 = "hello"; System.out.println(str1 == str2); } }Outputtrue
![Moumita](https://www.tutorialspoint.com/assets/profiles/13507/profile/60_122499-1557898491.jpg)
3K+ Views
The append(char c) method of the java.lang.StringBuffer appends the string representation of the char argument to this sequence. The argument is appended to the contents of this sequence. The length of this sequence increases by 1.Example Live Demoimport java.lang.*; public class StringBufferDemo { public static void main(String[] args) { StringBuffer buff = new StringBuffer("tuts "); System.out.println("buffer = " + buff); // appends the char argument as string to the string buffer. buff.append('A'); // print the string buffer after appending ... Read More
![Jai Janardhan](https://www.tutorialspoint.com/assets/profiles/13513/profile/60_63529-1512538711.jpg)
92 Views
The equals() method of the String class compares this string to the specified object. The result is true if and only if the argument is not null and is a String object that represents the same sequence of characters as this object.Example Live Demopublic class Test { public static void main(String args[]) { Integer x = 5; Integer y = 10; Integer z =5; Short a = 5; System.out.println(x.equals(y)); System.out.println(x.equals(z)); System.out.println(x.equals(a)); } }Outputfalse true false
![George John](https://www.tutorialspoint.com/assets/profiles/13511/profile/60_58410-1512537824.png)
131 Views
You can compare Strings using compareTo() method or, equals() method or, or == operator. Following example demonstrates how to initialize and compare strings in Java. Example Live Demo public class StringDemo { public static void main(String[] args) { String str1 = "tutorials"; String str2 = "point"; // comparing str1 and str2 int retval = str1.compareTo(str2); // prints the return value of the comparison if (retval < 0) { System.out.println("str1 is greater than str2"); } else if (retval == 0) { System.out.println("str1 is equal to str2"); } else { System.out.println("str1 is less than str2"); } } } Output str1 is less than str2
![Paul Richard](https://www.tutorialspoint.com/assets/profiles/13505/profile/60_52698-1512536733.jpg)
1K+ Views
The append() method in the StringBuffer class adds the specified String to the contents of the current String buffer. Using this method you can append a single character to a string or char array in java.Examplepublic class Test { public static void main(String args[]) { StringBuffer sb = new StringBuffer("Hi hello how are yo"); sb.append("u"); System.out.println(sb); } }OutputHi hello how are you