Download as pdf or txt
Download as pdf or txt
You are on page 1of 1

JAVA STRING CHEAT SHEET Learn JAVA from experts at http://www.edureka.

co

Java Strings Methods of Strings Programs


In Java, a string is an object that represents a str1==str2 //compares Removing Trailing spaces from string
Java Exceptions address;
sequence of characters. The java.lang.String class
String newStr = str1.equals(str2);
is used to create string object. String contains an
//compares the values int len = str.length();
immutable sequence of Unicode characters.
StringBuilt-in
newStrExceptions User -Defined Exceptions
=str1.equalsIgnoreCase() for( ; len > 0; len--) {
//compares the values ignoring the case if( ! Character.isWhitespace(
Creating a String newStr = str1.length() str.charAt( len - 1)))
//calculates length break;
String str1 = “Welcome”; newStr = str1.charAt(i) }
// Using literal String //extract i'th character return str.substring( 0, len);
str2 = new String(”Edureka”); newStr = str1.toUpperCase()
//returns string in ALL CAPS
// Using new keyword newStr = str1.toLowerCase() Finding Duplicate characters in a String
//returns string in ALL LOWERvCASE public void countDupChars{
newStr = str1.replace(oldVal, newVal) Map<Character, Integer> map = new HashMap
Immutable Strings //search and replace <Character, Integer>();
newStr = str1.trim() //Convert the String to char array
class Stringimmutable //trims surrounding whitespace char[] chars = str.toCharArray();
newStr = str1.contains("value"); Set<Character> keys = map.keySet();
{
//check for the values //Obtaining set of keys
public static void main(String args[]) public static void main(){
newStr = str1.toCharArray();
{ // convert String to character type System.out.println("String: Edureka");
String s="JavaStrings"; array newStr = str1.IsEmpty(); obj.countDupChars("Edureka");
s.concat(" CheatSheet"); //Check for empty String System.out.println("\nString:
System.out.println(s); newStr = str1.endsWith(); StringCheatSheet");
} //Checks if string ends with the given obj.countDupChars("StringCheatSheet");
} suffix }
}
String Conversions String Joiner Class
String to Int Conversion StringJoiner mystring = new
StringJoiner("-");
String str="123"; // Passing Hyphen(-) as delimiter
int inum1 = 100; mystring.add("edureka");
int inum2 = // Joining multiple strings by using
Integer.parseInt(str); add() method

JAVA
// Converting a string to int mystring.add("YouTube");

Int to String Conversion String reverse using Recursion

int var = 111;


String str =
Certification String str = "Welcome to Edureka";
String reversed=reverseString(str);
ReturnreverseString(str.substring(1))

Training
String.valueOf(var);
+ str.charAt(0);
System.out.println(555+str);
//Calling Function Recursively
// Conversion of Int to String

String to Double Conversion Double to String Conversion String reverse


String str;
String str = "100.222"; double dnum = 88.9999; //double value System.out.println("Enter your
double dnum = Double.parseDouble(str); String str = String.valueOf(dnum); username: ");
//displaying the value of variable dnum //conversion using valueOf() method String reversed = reverseString(str);
// Reversing a String
return reverseString(str.substring(1))
String vs String Buffer String Buffer vs Builder + str.charAt(0);
//Calling Function Recursively
It is immutable It is mutable StringBuffer StringBuilder is non-
is synchronized i.e. thread synchronized i.e. not String Pool
String class overrides StringBuffer class safe. thread safe.
the equals() method of doesn't override the String str1 = "abc";
Object class.. equals() method of StringBuffer is less StringBuilder is more String str2 = "abc";
Object class. efficient than StringBuilder efficient than StringBuffer System.out.println(str1 == str2);
as it is Synchronized. as it is not synchronized. System.out.println(str1 == "abc");

You might also like