Strings
What is a string?
● In many other programming languages, string is a sequence of characters.
● In Java, strings are objects.
● Strings are surrounded by double quotes.
● Strings are immutable. Once a string is created, it can’t be changed.
● If we want to to modify a string, it will create a new string which contains the
modifications.
String Input and Output
● .next() and .nextLine() of the scanner class is used to take string as input.
● .next() only takes input until it finds a “ ” or space in the given input.
● .nextLine() takes input until it finds a “\n” in the given input.
1. class stringInput{
2. public static void main(String[] args) {
3. Scanner sc = new Scanner([Link]);
4. String input1 = [Link]();
5. String input2 = [Link]();
6. [Link](input1);
7. [Link](input2);
8. }
9. }
Welcome to DrJava. Working directory is C:\Users\Desktop
CSE IS FUN.
>
CSE IS FUN.
>
CSE
CSE IS FUN.
String Initialization
● Initialization using String literals:
Literal String
String my_string = “Hello World!”;
Variable
name
String Initialization
● Creating an object of String class:
To create an object
Class name of String class
String my_str = new String( “Hello”);
Object String
name
String Initialization
String Length
● The number of characters in a string.
● Can find using the length() method.
1. class stringLength {
2. public static void main(String[] args) {
3. String str1 = "Hello World";
4. String str2 = ""; // Empty string
5. [Link]([Link]());
6. [Link]([Link]());
7. }
8. }
Welcome to DrJava. Working directory is C:\Users\Desktop
> run stringLength
11
0
String Indexing
● String indexing starts from 0 and ends at (Length of string - 1).
● If we create string1 = “CSE IS FUN.”, which has a length of 11, the indexing will look
like this:
0 1 2 3 4 5 6 7 8 9 10
C S E I S F U N .
● Blanks and special characters are also counted as characters.
● We can get characters at each each index by using charAt(index) method.
Example: [Link](2) will return “E”.
Iterating a String
● Accessing the characters of a string one by one.
● Iterating a string using for loop and charAt() method.
1. class StringIteration {
2. public static void main(String[] args) {
3. String str = new String("Hello");
4. for (int i = 0; i <[Link](); i++){
5. [Link]([Link](i));
6. }
7. }
8. }
Welcome to DrJava. Working directory is C:\Users\Desktop
> run StringIteration
H
e
l
l
o
String Concatenation
● “+” is used to concatenate two or more strings 1. String str1 = "We are learning";
together. 2. String str2 = "Java.";
● New string created after concatenation. 3. int num = 123;
● If a different data type follows a string data type 4. String str3 = str1 + str2;
5. String str4 = str1 + num;
in concatenation, that data is automatically
6. [Link](str3);
converted to string. 7. [Link](str4);
Welcome to DrJava. Working directory
is C:\Users\Desktop
We are learning Java.
We are learning 123
String Concatenation
int semester
y1 =output
String = “Fall ” +; y1 + 23;
20; = semester
[Link](output); Output:
Fall2023
String
int y1 =semester = “Fall
20; = y1
output + 23 ”+;semester;
[Link](output); Output:
43Fall
Comparing Strings
● String str1 = new String(“Hello”); String str2 = new String(“hello”);
Methods Functions Method Call Value
returned
.equals() Checks if values in two strings are same. [Link](str2) False
Case sensitive.
.equalsIgnoreCase() Checks if values in two strings are same. [Link](str2) True
Not case sensitive.
== or equals Checks if reference (address) of two strings str1 == str2 False
operator are same.
.compareTo() Compares two string values [Link](str2) -32
lexicographically. Returns 0 when identical, (The output is
-32 since “H”
(+)ve when first string is greater and (-)ve is 32 less
values when second string is greater than “h”
respectively. lexicographic
ally)
Case Conversion
● .toLowerCase()
○ Converts all characters to 1. String str1 = "LearNinG IS Fun";
lowercase letters. 2. String str1_lower= [Link]()
3. String str1_upper = [Link]()
4. [Link](str1_lower)
5. [Link](str1_upper)
● .toUpperCase() Welcome to DrJava. Working directory is C:\Users\
Desktop
○ Converts all characters to >learning is fun
uppercase letters. LEARNING IS FUN
.charAt(index) Method
● .charAt(index) 1. String str1 = "Hello World";
○ Returns the character at index 2. [Link]([Link](3))
value passed as argument. Welcome to DrJava. Working directory is C:\Users\
Desktop
>o
ASCII values
● Unique Unicode value assigned to all keyboard characters.
● Range of ASCII values of uppercase characters (‘A’ to ‘Z’): 65 - 90
● Range of ASCII values of uppercase characters (‘A’ to ‘Z’): 97 - 122
● ASCII value of space: 32
See more from:
[Link]
Converting a Character to ASCII Value
● .codePointAt(index) 1. String str1 = "CSE110";
2. int unicode_str1 = [Link](0);
○ Takes an index of a string as a 3. [Link](unicode_str1)
parameter and returns the ASCII value Welcome to DrJava. Working directory is C:\
of the character of that index. Users\Desktop
>67
● int(chr)
○ Can simply cast a character as an 1. char c1 = 'a';
2. int unicode_c1 = int(c1);
integer.
3. [Link](unicode_c1)
Welcome to DrJava. Working directory is C:\
Can you now try to figure out how you can convert an Users\Desktop
uppercase character to its equivalent lowercase >97
character using the knowledge you gained on ASCII
values so far?
Practice
● Write a Java program to compare two strings lexicographically. You cannot use any built in function except
length().
● Write a Java program to test if a given string contains the specified sequence of char values.
● Write a Java program to check whether a given string ends with another string.
● Write a Java program to replace a specified character with another character.
● Write a Java program to get a substring of a given string at two specified positions.
● Write a Java program to convert all characters in a string to lowercase.
● Write a Java program to print the result of removing duplicates from a given string.
THANK YOU!