Java String Methods

Method

Description

Example

charAt(int index)

Returns the character at the specified index.

"Hello".charAt(1) // 'e'


Try it yourself

length()

Returns the length of the string.

"Java".length() // 4


Try it yourself

substring(int start, int end)

Extracts a substring.

"Hello".substring(0, 4) // Hell


Try it yourself

indexOf(String str)

Returns the first index of a substring.

"Hello".indexOf("l") // 2


Try it yourself

lastIndexOf(String str)

Returns the last index of a substring.

"Hello".lastIndexOf("l") // 3


Try it yourself

replace(char old, char new)

Replaces characters in the string.

"Java".replace('a', 'o') // Jovo


Try it yourself

toUpperCase()

Converts the string to uppercase.

"java".toUpperCase() // JAVA


Try it yourself

toLowerCase()

Converts the string to lowercase.

"JAVA".toLowerCase() // java


Try it yourself

trim()

Removes leading and trailing spaces.

" Java ".trim() // Java


Try it yourself

split(String regex)

Splits the string into an array based on a regex.

"a,b,c".split(",") // ["a", "b", "c"]


Try it yourself

startsWith(String prefix)

Checks if the string starts with the prefix.

"Hello".startsWith("He") // true


Try it yourself

endsWith(String suffix)

Checks if the string ends with the suffix.

"Hello".endsWith("lo") // true


Try it yourself


Whereisstuff is simple learing platform for beginer to advance level to improve there skills in technologies.we will provide all material free of cost.you can write a code in runkit workspace and we provide some extrac features also, you agree to have read and accepted our terms of use, cookie and privacy policy.
© Copyright 2024 www.whereisstuff.com. All rights reserved. Developed by whereisstuff Tech.