0% found this document useful (0 votes)
47 views

Module 2 - String

This document provides an overview of strings in Java. It discusses how to declare strings, common string methods like charAt(), length(), substring(), and how to manipulate strings using StringBuffer/StringBuilder. It also covers wrapper classes in Java for converting between primitives and their corresponding object types, including autoboxing and unboxing.

Uploaded by

Dhriti Nayyar
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
47 views

Module 2 - String

This document provides an overview of strings in Java. It discusses how to declare strings, common string methods like charAt(), length(), substring(), and how to manipulate strings using StringBuffer/StringBuilder. It also covers wrapper classes in Java for converting between primitives and their corresponding object types, including autoboxing and unboxing.

Uploaded by

Dhriti Nayyar
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 34

Module 2 - String

Gowri Prasood u
1
SENSE
VIT Chennai
2
String

 A string is a sequence of characters.

 In simpler words, it is an array of characters.

 How to declare a string : - String mystring = "Hello";

 The string is declared as an array of characters: -


String mystring = {'H', 'e', 'l', 'l', 'o'};
3

 Create String objects using the new keyword.

 we can also declare it as


4 String Methods

 Java charAt()
 It returns the character at the specified index in a string.

 The character at index 1 in s1 is e. So, s1.charAt(2) returned e.


5 Java length()

 It returns the length (number of characters) in a string.


6 Java substring()

 It returns the substring starting from the specified start index and ending at


the specified end index. If no end index is given, then the substring starting
from the start index is returned.
7 Java concat()

 It concatenates or joins two strings.


8 Java replace() and replaceAll()
 It returns a string by replacing the specified character or substring with
another specified character or substring.
9 Java toLowerCase()

 It returns a string with all the characters in lowercase.


10 Java toUpperCase()

 It returns a string with all the characters in uppercase.


11 Java indexOf()
 It returns the index of the specified character or substring in a string.
 If the specified character or substring occurs more than once in the string,
then the index of its first occurrence is returned.
12

• s1.indexOf('t', 8) returned the character ‘t’ index after the 8th index.

• s1.indexOf("on", 12) returned the index of the string “on” after the 12th index.
13 Java compareTo()
 It compares two strings lexicographically (in the dictionary order).
 It returns 0 if both the strings are the same, a positive value if the first string
comes after the second string and a negative value if the first string comes
before the second string in the dictionary order.
 While comparing two strings, the Unicode values of their characters are
compared. The character having the lower Unicode value is smaller.
14 Java String equals()
 The Java String class 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.
15 Java String equalsIgnoreCase()
 The Java String class equalsIgnoreCase() method compares the two given strings on the
basis of the content of the string irrespective of the case (lower and upper) of the string.
16 Java String endsWith()

 Method checks if this string ends with a given suffix. It returns true if this
string ends with the given suffix; else returns false.
17 Java String startsWith()
 It returns a string with all the characters in uppercase. method checks if this
string starts with the given prefix. It returns true if this string starts with the
given prefix; else returns false.
18
19 Java StringBuffer Class

 A String that can be modified or changed is known as a mutable String.

 StringBuffer class is used for creating mutable strings.

StringBuffer sb = new StringBuffer();


20 StringBuffer Methods

 StringBuffer Class append() Method


 The append() method concatenates the given argument with this string.
21 StringBuffer insert() Method

 The insert() method inserts the given string with this string at the given position. 
22 StringBuffer replace() Method

 The replace() method replaces the given String from the specified beginIndex and
endIndex.
23 StringBuffer delete() Method

 The delete() method of the StringBuffer class deletes the string from the specified
beginIndex to endIndex-1.
24 StringBuffer reverse() Method

 The reverse() method of the StringBuilder class reverses the current string. 
StringBuffer capacity() Method
25
 The capacity() method of the StringBuffer class returns the current capacity of the buffer.
 The default capacity of the buffer is 16.
 If the number of characters increases from its current capacity, it increases the capacity by
(oldcapacity*2)+2.
26
27 Java Wrapper Classes

 The wrapper class in Java provides the mechanism to convert primitive into object and
object into primitive.
28
 For example, Integer is a predefined wrapper class, and its object
is used to store values of type int.
29  Creating Wrapper Class
30
 The following methods are used to get the value associated with the corresponding
wrapper object
31 Java Autoboxing
 Java can also automatically convert a primitive type into the corresponding wrapper class
object. This process is known as autoboxing.
32 Java unboxing
 Unboxing is just the opposite of autoboxing. It is the process in which Java
automatically converts a wrapper class object into the corresponding
primitive type.
33

 Java Program to convert all primitives into its corresponding
wrapper objects and vice-versa.  
34
Thank you

You might also like