[Go to site: main page, start]

0% found this document useful (0 votes)
2 views1 page

Java String Cheatsheet

This document is a cheat sheet for Java String methods, detailing various methods, their uses, examples, and outputs. It covers methods such as length(), charAt(), equals(), and many others, providing a quick reference for string manipulation in Java. Each method is accompanied by a brief description and an illustrative example.

Uploaded by

sid
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views1 page

Java String Cheatsheet

This document is a cheat sheet for Java String methods, detailing various methods, their uses, examples, and outputs. It covers methods such as length(), charAt(), equals(), and many others, providing a quick reference for string manipulation in Java. Each method is accompanied by a brief description and an illustrative example.

Uploaded by

sid
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Java String Methods Cheat Sheet

Method Use Example Output

length() Returns length of string "Hello".length() 5


charAt(i) Returns character at index "Hello".charAt(1) 'e'
toCharArray() Converts string to char array [Link]("Hi".toCharArray()) [H, i]
equals() Checks if two strings are equal "Java".equals("Java") true
equalsIgnoreCase() Compares ignoring case "Java".equalsIgnoreCase("java") true
compareTo() Lexicographic comparison "Java".compareTo("Apple") >0
contains() Checks if substring exists "program".contains("gram") true
indexOf() First occurrence index "Hello".indexOf("l") 2
lastIndexOf() Last occurrence index "Hello".lastIndexOf("l") 3
startsWith() Checks prefix "Hello".startsWith("He") true
endsWith() Checks suffix "Hello".endsWith("lo") true
substring() Extract substring "Welcome".substring(3,7) come
concat() Concatenates strings "Hi".concat(" There") Hi There
replace() Replaces characters "Java".replace('a', 'o') Jovo
replaceAll() Replaces using regex "Java".replaceAll("a","@") J@v@
trim() Removes leading/trailing spaces " Hi ".trim() Hi
toUpperCase() Converts to uppercase "hello".toUpperCase() HELLO
toLowerCase() Converts to lowercase "HELLO".toLowerCase() hello
split() Splits string by delimiter [Link]("a,b,c".split(",")) [a, b, c]
join() Joins strings with delimiter [Link]("-", "a","b","c") a-b-c
valueOf() Converts primitive to String [Link](10) 10
format() Formats string [Link]("Age: %d",20) Age: 20
isEmpty() Checks if empty "".isEmpty() true
isBlank() Checks blank/whitespace " ".isBlank() true
matches() Regex match "abc123".matches("[a-z]+\d+") true
intern() Returns from String pool new String("Hi").intern() "Hi"
repeat() Repeats string "Hi ".repeat(3) Hi Hi Hi

You might also like