0% found this document useful (0 votes)
3 views3 pages

String Methods in Java

This is string method in java for btech 5th sem java lab

Uploaded by

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

String Methods in Java

This is string method in java for btech 5th sem java lab

Uploaded by

bhavyanshs19
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

String Method Description

1. int length() Returns the number of characters in the String.


2. Char charAt(int i) Returns the character at ith index.
3. String substring (int i) Return the substring from the ith index character to end.
4. String substring (int i, int j)Returns the substring from i to j-1 index.
5. String concat( String str) Concatenates specified string to the end of this string.
Returns the index within the string of the first occurrence
of the specified string.
6. int indexOf (String s)
If String s is not present in input string then -1 is returned
as the default value.
Returns the index within the string of the first occurrence
7. int indexOf (String s, int i)
of the specified string, starting at the specified index.
Returns the index within the string of the last occurrence
of the specified string.
8. Int lastIndexOf( String s)
If String s is not present in input string then -1 is returned
as the default value.
9. boolean equals( Object otherObj) Compares this string to the specified object.
10. boolean equalsIgnoreCase Compares string to another string, ignoring case
(String anotherString) considerations.
11. int compareTo( String
Compares two string lexicographically.
anotherString)
Compares two string lexicographically, ignoring case
12. int considerations.
compareToIgnoreCase( String
anotherString) Note: In this case, it will not consider case of a letter (it
will ignore whether it is uppercase or lowercase).
13. String toLowerCase() Converts all the characters in the String to lower case.
14. String toUpperCase() Converts all the characters in the String to upper case.
Returns the copy of the String, by removing whitespaces
15. String trim()
at both ends. It does not affect whitespaces in the middle.
Returns new string by replacing all occurrences of
16. String replace (char oldChar, oldChar with newChar.
char newChar)
Note: s1 is still feeksforfeeks and s2 is geeksgorgeeks
17. boolean contains(CharSequence
Returns true if string contains contains the given string.
sequence)
18. Char[] toCharArray(): Converts this String to a new character array.
19. boolean startsWith(String
Return true if string starts with this prefix.
prefix)

// Java code to illustrate different constructors and methods in String class


import java.io.*;
import java.util.*;

class Test
{
public static void main (String[] args)
{
String s= "GeeksforGeeks";
// or String s= new String ("GeeksforGeeks");

// Returns the number of characters in the String.


System.out.println("String length = " + s.length());

// Returns the character at ith index.


System.out.println("Character at 3rd position = "
+ s.charAt(3));

// Return the substring from the ith index character


// to end of string
System.out.println("Substring " + s.substring(3));

// Returns the substring from i to j-1 index.


System.out.println("Substring = " + s.substring(2,5));

// Concatenates string2 to the end of string1.


String s1 = "Geeks";
String s2 = "forGeeks";
System.out.println("Concatenated string = " +
s1.concat(s2));

// Returns the index within the string


// of the first occurrence of the specified string.
String s4 = "Learn Share Learn";
System.out.println("Index of Share " +
s4.indexOf("Share"));

// Returns the index within the string of the


// first occurrence of the specified string,
// starting at the specified index.
System.out.println("Index of a = " +
s4.indexOf('a',3));

// Checking equality of Strings


Boolean out = "Geeks".equals("geeks");
System.out.println("Checking Equality " + out);
out = "Geeks".equals("Geeks");
System.out.println("Checking Equality " + out);

out = "Geeks".equalsIgnoreCase("gEeks ");


System.out.println("Checking Equality " + out);

//If ASCII difference is zero then the two strings are similar
int out1 = s1.compareTo(s2);
System.out.println("the difference between ASCII value is="+out1);
// Converting cases
String w1 = "GeeKyMe";
System.out.println("Changing to lower Case " +
w1.toLowerCase());

// Converting cases
String w2 = "GeekyME";
System.out.println("Changing to UPPER Case " +
w2.toUpperCase());

// Trimming the word


String w4 = " Learn Share Learn ";
System.out.println("Trim the word " + w4.trim());
// Replacing characters
String str1 = "feeksforfeeks";
System.out.println("Original String " + str1);
String str2 = "feeksforfeeks".replace('f' ,'g') ;
System.out.println("Replaced f with g -> " + str2);
}
}
Output
String length = 13
Character at 3rd position = k
Substring ksforGeeks
Substring = eks
Concatenated string = GeeksforGeeks
Index of Share 6
Index of a = 8
Checking Equality false
Checking Equality true
Checking Equality false
the difference between ASCII value is=-31
Changing to lower Case geekyme
Changing to UPPER Case GEEKYME
Trim the word Learn Share Learn
Original String feeksforfeeks
Replaced f with g -> geeksgorgeeks

You might also like