String Methods
UNIT - 1
public char charAt(int index) – �It returns char value for the particular index
public class StringMethods {
public static void main(String[] args) {
// TODO Auto-generated method stub
String name="Java Prograaming";
//returns the char value at the 4th index
char ch=name.charAt(5);
System.out.println(ch);
}
}
public int length() – �Length of characters. In other words, the total number of characters present in the string.
public class StringMethods {
public static void main(String[] args) {
String str = "Javatpoint";
if(str.length()>0) {
System.out.println("String is not empty and length is: "+str.length());
}
str = "";
if(str.length()==0) {
System.out.println("String is empty now: "+str.length());
}
}
}
public static String format(String format, Object... args)
Throws
public static String format(String format, Object... args) �public static String format(Locale locale, String format, Object... args)
public class StringMethods {
public static void main(String[] args) {
String name="sonoo";
String sf1=String.format("name is %s",name);
String sf2=String.format("value is %f",32.33434);
//returns 12 char fractional part filling with 0
String sf3=String.format("value is %32.12f",32.33434);
System.out.println(sf1);
System.out.println(sf2);
System.out.println(sf3);
}�
}
public String substring(int startIndex) �public String substring(int startIndex, int endIndex)
Exception Throws
public boolean contains(CharSequence sequence)
Returns
true if the sequence of char value exists, otherwise false.
Exception
NullPointerException : if the sequence is null.
public class StringMethods {
public static void main(String[] args) {
String name="what do you know about me";
System.out.println(name.contains("do you know"));
System.out.println(name.contains("about"));
System.out.println(name.contains("hello"));
}
}
public static String join(CharSequence delimiter, CharSequence... elements) �public static String join(CharSequence delimiter, Iterable<? extends CharSequence> elements)
Parameters
delimiter : char value to be added with each element
elements : char value to be attached with delimiter
Returns
joined string with delimiter
Exception Throws
NullPointerException if element or delimiter is null.
Example of join methods
public static void main(String[] args) {
String words[] = new String[] {"Hello", "All"};
List<String> colors = Arrays.asList("red", "green");
//using characterSequence elements
System.out.println(String.join("/", "31","07","2024"));
//using iterable object
System.out.println(String.join(" ", colors));
System.out.println(String.join(" ", words));
}
Public boolean equals(Object anotherObject)
int compareTo(String anotherString)�int compareToIgnoreCase(String str)�
Parameters
Returns
String s1="javatpoint is a very good website";
System.out.println(s1.replace('a','e'));//replaces all occurrences of 'a' to 'e'
System.out.println(s1.replace("good", "best"));
public boolean isEmpty()
String str1 = null;
String str2 = "";
System.out.println(new String().isEmpty()); //true
System.out.println(str2.isEmpty()); //true
//raise NullPointerException
System.out.println(str1.isEmpty());
public String concat(String anotherString)
String str1 = "Hello";
String str2 = "All";
String str3 = "Reader";
// Concatenating one string
String str4 = str1.concat(str2);
System.out.println(str4);
// Concatenating multiple strings
String str5 = str1.concat(str2).concat(str3);
System.out.println(str5);
public String replace(char oldChar, char newChar) �public String replace(CharSequence target, CharSequence replacement)
Parameters
Returns - replaced string
Exception Throws - NullPointerException: if the replacement or target is equal to null.
String s1="javatpoint is a very good website";
System.out.println(s1.replace('a','e'));//replaces all occurrences of 'a' to 'e'
System.out.println(s1.replace("good", "best"));
public boolean equalsIgnoreCase(String str)
Parameters
Returns - It returns true if characters of both strings are equal, ignoring case otherwise false.
String s1="javatpoint";
String s2="javatpoint";
String s3="JAVATPOINT";
String s4="python";
System.out.println(s1.equalsIgnoreCase(s2));//true content is same
System.out.println(s1.equalsIgnoreCase(s3));//true because case is ignored
System.out.println(s1.equalsIgnoreCase(s4));//false because content is not same
public String split(String regex) �and, �public String split(String regex, int limit)
Parameters
Returns - array of strings
Throws - PatternSyntaxException if pattern for regular expression is invalid
s1="welcome to split world";
System.out.println("returning words:");
//no. of substring is greater than possible words
String[] words2 = s1.split("\\s",10);
System.out.println(words2.length); //4
for(String w: words2){
System.out.println(w);
}
public String intern()
Why?
public String intern()
What will be the output?
String s1 = new String("Hello");
String s2 = new String("Hello");
String s3 = s2;
System.out.println(s1==s2);
System.out.println(s1==s3);
System.out.println(s2==s3);
System.out.println(System.identityHashCode(s2)+" "+System.identityHashCode(s1));
public String intern()
What will be the output?
String s1 = new String("Hello");
String s2 = new String("Hello");
String s3 = s2;
System.out.println(s1==s2);\\false
System.out.println(s1==s3);\\false
System.out.println(s2==s3);\\true
System.out.println(System.identityHashCode(s2)+" "+System.identityHashCode(s1)); \\will print different memory
public String intern()
What will be the output?
String s1 = new String("Hello").intern();
String s2 = new String("Hello");
String s3 = s2;
System.out.println(s1==s2);
System.out.println(s1==s3);
System.out.println(s2==s3);
System.out.println(System.identityHashCode(s2)+" "+System.identityHashCode(s3));
false
false
true
1867750575 2046562095
public String intern()
What will be the output?
String s1 = new String("Hello").intern();
String s2 = new String("Hello").intern();
String s3 = s2;
System.out.println(s1==s2);
System.out.println(s1==s3);
System.out.println(s2==s3);
System.out.println(System.identityHashCode(s1)+" "+System.identityHashCode(s2)
+" "+System.identityHashCode(s3));
true
true
true
1867750575 1867750575 1867750575
public int indexOf()
Parameters
Returns - Index of the searched string or character.
public int indexOf()
Example of indexOf()
String s1="this is index of example";
//passing substring
int index1=s1.indexOf("is");//returns the index of is substring
int index2=s1.indexOf("index");//returns the index of index substring
System.out.println(index1+" "+index2);//2 8
//passing substring with from index
int index3=s1.indexOf("is",4);//returns the index of is substring after 4th index
System.out.println(index3);//5 i.e. the index of another is
//passing char value
int index4=s1.indexOf('s');//returns the index of s char value
System.out.println(index4);//3
public String toLowerCase() - coverts to lowercase �public String toLowerCase(Locale locale) - converts based on the specified locale.
Parameters
The second method is essential for handling text in different languages and cultures, as lowercase conversion rules can vary significantly.
String text = "TURKISH i CHARACTER";
�
// Convert to lowercase using the default locale (might not be correct)
String defaultLowercase = text.toLowerCase();
System.out.println(defaultLowercase); // Output: turkish i character
�// Convert to lowercase using Turkish locale
String turkishLowercase = text.toLowerCase(Locale.forLanguageTag("tr"));
System.out.println(turkishLowercase); // Output: türkış ı character
public String toUpperCase() �public String toUpperCase(Locale locale)
Parameters
The second method variant of toUpperCase(), converts all the characters into uppercase using the rules of given Locale.
String s = "hello string";
String turkish = s.toUpperCase(Locale.forLanguageTag("tr"));
String english = s.toUpperCase(Locale.forLanguageTag("en"));
System.out.println(turkish);//will print I with dot on upper side
System.out.println(english);
public String trim() - string with omitted leading and trailing spaces
String str = " abc ";
String str1 = str.trim();
System.out.println(str.hashCode());
System.out.println(str1.hashCode() + "\n");
String s = "xyz";
String s1 = s.trim();
System.out.println(s.hashCode());
System.out.println(s1.hashCode());
Returns the same reference of the string if string didn’t contain any leading and trailing spaces. Otherwise returns a new string
32539678
96354
�
119193
119193