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

Java String Methods With Examples

The document provides a comprehensive overview of various Java String methods, including their syntax and examples. It covers constructors for creating strings, methods for string manipulation, comparison, and conversion. Each method is accompanied by a brief description and a code example to illustrate its usage.

Uploaded by

ayushi3671
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)
3 views1 page

Java String Methods With Examples

The document provides a comprehensive overview of various Java String methods, including their syntax and examples. It covers constructors for creating strings, methods for string manipulation, comparison, and conversion. Each method is accompanied by a brief description and a code example to illustrate its usage.

Uploaded by

ayushi3671
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
You are on page 1/ 1

Java String Methods with Syntax and Examples

Syntax Purpose Example

ctor String s = new String(); Create empty string Example: String s = new String();

ctor String s = new String(char[] chars); Create from char array char[] arr = {'a','b'}; String s = new String(arr);

ctor String s = new String(char[] chars, int startIndex, int numChars); Create from part of char array char[] arr = {'a','b','c','d'}; String s = new String(arr, 1, 2);

ctor String s = new String(String another); Create copy of another string String s1 = "Hello"; String s2 = new String(s1);

int len = s.length(); Get length of string String s = "Hello"; int len = s.length();

char ch = s.charAt(int index); Get character at position String s = "Java"; char ch = s.charAt(1); // a

s() s.getChars(int srcStart, int srcEnd, char[] target, int targetStart); Copy part of string into char array String s = "Hello"; char[] buf = new char[3]; s.getChars(1, 4

() byte[] arr = s.getBytes(); Get bytes from string String s = "ABC"; byte[] b = s.getBytes();

rray() char[] arr = s.toCharArray(); Convert to char array String s = "Hi"; char[] ch = s.toCharArray();

s1.equals(Object str); Compare strings (case-sensitive) "Hello".equals("Hello"); // true

noreCase() s1.equalsIgnoreCase(Object str); Compare strings ignoring case "HELLO".equalsIgnoreCase("hello"); // true

atches() s1.regionMatches(int start1, String s2, int start2, int numChars); Compare regions of strings "HelloWorld".regionMatches(0, "Hello", 0, 5); // true

atches(ignore case) s1.regionMatches(true, int start1, String s2, int start2, int numChars); Compare regions ignoring case "HELLO".regionMatches(true, 0, "hello", 0, 5); // true

h() s1.startsWith(String str); Check if starts with given substring "Java".startsWith("Ja"); // true

h(index) s1.startsWith(String str, int index); Check start from specific index "HelloWorld".startsWith("World", 5); // true

h() s1.endsWith(String str); Check if ends with given substring "Java".endsWith("va"); // true

To() s1.compareTo(String str); Compare lexicographically "abc".compareTo("abd"); // negative

) s.indexOf(int ch); Find first occurrence of character "Hello".indexOf('l'); // 2

) s.indexOf(String str); Find first occurrence of substring "Hello".indexOf("lo"); // 3

start) s.indexOf(int ch, int startIndex); Find char from position "Hello".indexOf('l', 3); // 3

start) s.indexOf(String str, int startIndex); Find substring from position "HelloHello".indexOf("lo", 5); // 8

xOf() s.lastIndexOf(int ch); Find last occurrence of character "Hello".lastIndexOf('l'); // 3

xOf() s.lastIndexOf(String str); Find last occurrence of substring "HelloHello".lastIndexOf("lo"); // 8

xOf(start) s.lastIndexOf(int ch, int startIndex); Find last char before position "HelloHello".lastIndexOf('l', 5); // 3

xOf(start) s.lastIndexOf(String str, int startIndex); Find last substring before position "HelloHello".lastIndexOf("lo", 5); // 3

g() s.substring(int startIndex); Get substring from position to end "Hello".substring(2); // "llo"

g() s.substring(int startIndex, int endIndex); Get substring between positions "Hello".substring(1, 4); // "ell"

s.concat(String str); Append string "Hello".concat("World"); // "HelloWorld"

) s.replace(char old, char new); Replace characters "Hello".replace('l', 'w'); // "Hewwo"

s.trim(); Remove leading/trailing spaces " Hello ".trim(); // "Hello"

Case() s.toLowerCase(); Convert to lowercase "HELLO".toLowerCase(); // "hello"

Case() s.toUpperCase(); Convert to uppercase "hello".toUpperCase(); // "HELLO"

) String.valueOf(int num); Convert primitive to string String.valueOf(123); // "123"

) String.valueOf(Object obj); Convert object to string String.valueOf(new Integer(10)); // "10"

) obj.toString(); Object to string representation Integer i = 5; i.toString(); // "5"

You might also like