You are on page 1of 7

Podar International School, ICSE, Aurangabad

String Handling 2021-22


Conceptual notes
Std: X Subject : Computer Applications

Instructions :

Mention date, chapter number, chapter name before starting the work.

Submission method will be - all students will click pics of solved worksheet and share pics/ pdf with
me on Google classroom

1. String handling functions are set of pre-defined functions called string


handling functions to work with string values. They belong to java.lang package
and String class.
2. Java String length() method is used to find out the length of a String. This
method counts the number of characters in a String including the white spaces
and returns the count.
The syntax of the length() method is:

string.length()

 The length() method returns the length of the given string in int.

Eg: String str1 = "Java";


System.out.println(str1.length()); // 4

3. The Java String charAt() method returns the character at the specified index.

The syntax of the string charAt() method is:

string.charAt(int index)

 returns the character at the specified index

Eg:
String str1 = "Learn Java";
System.out.println(str1.charAt(0)); // 'L'

4. The String indexOf() method returns the index of the first occurrence of the
specified character/substring within the string.

The syntax of the String indexOf() method either

string.indexOf(int ch, int fromIndex)

 *returns the index of the first occurrence of the specified


character/string
 Eg:

String str1 = "Learn Java";


int result;
result = str1.indexOf('J');
System.out.println(result); // 6

5. The String lastIndexOf() method returns the index of the last occurrence of
the specified character/substring within the string.

The syntax of the String lastIndexOf() method either

string.lastIndexOf(int ch, int index)

 returns the index of the last occurrence of the specified character/string

Eg: 1
String str1 = "Learn Java";
int result;
result = str1.lastIndexOf('J');
System.out.println(result); // 6
Eg 2:
String str1 = "Learn Java programming";
int result;
result = str1.lastIndexOf('r', 4);
System.out.println(result); // 3

6. The Java String substring() method extracts a substring from the string and
returns it.

The syntax of the substring() method is:

string.substring(int startIndex, int endIndex)


The substring() method returns a substring from the given string.

String str1 = "program";

// from the first character to end


System.out.println(str1.substring(0)); // program

// from the 4th character to end


System.out.println(str1.substring(3)); // gram
String str1 = "program";

// from 1st to the 7th character


System.out.println(str1.substring(0, 7)); // program

// from 1st to the 5th character


System.out.println(str1.substring(0, 5)); // progr

7. The Java String replace() method replaces each matching occurrences of the
old character/text in the string with the new character/text.

The syntax of the replace() method is either

string.replace(char oldChar, char newChar)

or

string.replace(CharSequence oldText, CharSequence newText)


String str1 = "abc cba";

// all occurrences of 'a' is replaced with 'z'


System.out.println(str1.replace('a', 'z')); // zbc cbz

// all occurences of 'L' is replaced with 'J'


System.out.println("Lava".replace('L', 'J')); // Java

// character not in the string


System.out.println("Hello".replace('4', 'J')); // Hello

8. Java string concat() method concatenates multiple strings. This method


appends the specified string at the end of the given string and returns the
combined string. We can use concat() method to join more than one strings.
Eg 1: String s1="Java";
s1= s1.concat("is").concat(".").concat("easy");
Eg 2: String str1 = "Welcome";
str1 = str1.concat(" to ");
str1 = str1.concat(" String handling ");
System.out.println(str1);

9 The java string equals() method compares the two given strings based on the


content of the string. If any character is not matched, it returns false. If all
characters are matched, it returns true. 
Return datatype is boolean, parameter type is String.
1. String s1="Java";  
2. String s2="java";  
3. boolean b=s1.equals(s2);
4. b= false

10. The String equalsIgnoreCase() method compares the two given strings on


the basis of content of the string irrespective of case of the string. It is like
equals() method but doesn't check case. If any character is not matched, it
returns false otherwise it returns true.
Return datatype is boolean, parameter type is String.
1. String s1="javatpoint";  
2. String s2="javatpoint";  
1. System.out.println(s1.equalsIgnoreCase(s2));//true because content and 
case both are same  

11. The Java String toLowerCase() method converts all characters in the string to
lower case characters.

The syntax of the string toLowerCase() method is:

string.toLowerCase()

 returns a string with all upper case letters converted to lower case letters

String str1 = "Learn Java";


System.out.println(str1.toLowerCase()); // "learn java"

12. The Java String toUpperCase() method converts all characters in the string to
upper case characters.

The syntax of the string toUpperCase() method is:

string.toUpperCase()

toUpperCase() Return Value


 returns a string with all lower case letters converted to upper case

String str1 = "Learn Java";


System.out.println(str1.toUpperCase()); // "LEARN JAVA"

13. The Java String compareTo() method compares two strings lexicographically
(in the dictionary order). The comparison is based on the Unicode value of each
character in the strings.

The syntax of the compareTo() method is:

string.compareTo(String str)

compareTo() Return Value

 returns 0 if the strings are equal


 returns a negative integer if the string comes before the str argument in
the dictionary order
 returns a positive integer if the string comes before the str argument in
the dictionary order

String str1 = "Learn Java";


String str2 = "Learn Java";
int result;
// comparing str1 with str2
result = str1.compareTo(str2);
System.out.println(result); // 0

Eg 2:

String str1 = "Learn Python";


String str2 = "Learn Java";

// if str1 and str2 are equal, the result is 0


if (str1.compareTo(str2) == 0) {
System.out.println("str1 and str2 are equal");
}
else {
System.out.println("str1 and str2 are not equal");

14. The Java String compareToIgnoreCase() method compares two strings


lexicographically (in the dictionary order), ignoring case differences.

The syntax of the string  compareToIgnoreCase()  method is:

string.compareToIgnoreCase(String str)

compareToIgnoreCase() Return Value

 returns 0 if the strings are equal, ignoring case


considerations
 returns a negative integer if the string comes before
the  str  argument in the dictionary order
 returns a positive integer if the string comes before
the  str  argument in the dictionary order

String str1 = "Learn Java";


String str2 = "learn java";
// comparing str1 with str2
result = str1.compareToIgnoreCase(str2);
System.out.println(result); // 0

15. The Java String trim() method returns a string with any leading
(starting) and trailing (ending) whitespace removed.

The syntax of the string  trim()  method is:

string.trim()

the trim() method doesn't take any parameters


returns a string with leading and trailing whitespace removed
If there are no whitepace in the start or the end, it returns the original string.
Eg:
String str1 = " Learn Java Programming ";
System.out.println(str1.trim());
16. The Java String startsWith() method checks whether the string begins with
the specified string or not. It returns true if the string begins with the given string
returns false if the string doesn't begin with the given string
syntax: string.startsWith(String str)
Eg: String str = "Java Programming";
System.out.println(str.startsWith("Java")); // true
17. The Java String endsWith() method checks whether the string ends with the
specified string or not.The syntax of the string endsWith() method is:
string.endsWith(String str)
Return datatype: returns true if the string ends with the given string
returns false if the string doesn't end with the given string
Eg: String str = "Java Programming";
System.out.println(str.endsWith("mming")); // true

You might also like