0% found this document useful (0 votes)
23 views

Learn Java - String Methods Cheatsheet - Codecademy

This document discusses several string methods in Java including length(), concat(), equals(), indexOf(), charAt(), toUpperCase(), and toLowerCase(). These methods allow manipulation of string values and characters within strings.

Uploaded by

SHUBHANSHU SINGH
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

Learn Java - String Methods Cheatsheet - Codecademy

This document discusses several string methods in Java including length(), concat(), equals(), indexOf(), charAt(), toUpperCase(), and toLowerCase(). These methods allow manipulation of string values and characters within strings.

Uploaded by

SHUBHANSHU SINGH
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Cheatsheets / Learn Java

String Methods

length() String Method in Java


In Java, the length() string method returns the total number of characters – the String str = "Codecademy";
length – of a String .

System.out.println(str.length());
// prints 10

concat() String Method in Java


In Java, the concat() string method is used to append one String to the end of String s1 = "Hello";
another String . This method returns a String representing the text of the combined
String s2 = " World!";
strings.

String s3 = s1.concat(s2);
// concatenates strings s1 and s2

System.out.println(s3);
// prints "Hello World!"

String Method equals() in Java


In Java, the equals() string method tests for equality between two String s. String s1 = "Hello";
equals() compares the contents of each String . If all of the characters between the
String s2 = "World";
two match, the method returns true . If any of the characters do not match, it returns
false .
Additionally, if you want to compare two strings without considering upper/lower System.out.println(s1.equals("Hello"));
cases, you can use .equalsIgnoreCase() . // prints true
System.out.println(s2.equals("Hello"));
// prints false

System.out.println(s2.equalsIgnoreCase("world"));
// prints true

indexOf() String Method in Java


In Java, the indexOf() string method returns the first occurence of a character or a String str = "Hello World!";
substring in a String . The character/substring that you want to find the index of goes
inside of the () .
If indexOf() cannot find the character or substring, it will return -1. System.out.println(str.indexOf("l"));
// prints 2

System.out.println(str.indexOf("Wor"));
// prints 6

System.out.println(str.indexOf("z"));
// prints -1

charAt() String Method in Java


In Java, the charAt() string method returns the character of a String at a specified String str = "This is a string";
index. The index value is passed inside of the () , and should lie between 0 and
length()-1 .
System.out.println(str.charAt(0));
// prints 'T'

System.out.println(str.charAt(15));
// prints 'g'
toUpperCase() and toLowerCase() String Methods
In Java, we can easily convert a String to upper and lower case with the help of a few String str = "Hello World!";
string methods:
toUpperCase() returns the string value converted to uppercase.
toLowerCase() returns the string value converted to lowercase. String uppercase = str.toUpperCase();
// uppercase = "HELLO WORLD!"

String lowercase = str.toLowerCase();


// lowercase = "hello world!"

Save Print Share

You might also like