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

String

Uploaded by

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

String

Uploaded by

Hasanul Mahi
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 18

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(System.in);
4. String input1 = sc.next();
5. String input2 = sc.nextLine();
6. System.out.println(input1);
7. System.out.println(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. System.out.println(str1.length());
6. System.out.println(str2.length());
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: string1.charAt(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 <str.length(); i++){
5. System.out.println(str.charAt(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. System.out.println(str3);
converted to string. 7. System.out.println(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
System.out.println(output); Output:
Fall2023

String
int y1 =semester = “Fall
20; = y1
output + 23 ”+;semester;
System.out.println(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. str1.equals(str2) False


Case sensitive.

.equalsIgnoreCase() Checks if values in two strings are same. str1.equalsIgnoreCase(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 str1.compareTo(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= str1.toLowerCase()
3. String str1_upper = str1.toUpperCase()
4. System.out.println(str1_lower)
5. System.out.println(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. System.out.println(str1.charAt(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:


https://www.cs.cmu.edu/~pattis/15-1XX/common/handouts/ascii.html
Converting a Character to ASCII Value
● .codePointAt(index) 1. String str1 = "CSE110";
2. int unicode_str1 = str1.codePointAt(0);
○ Takes an index of a string as a 3. System.out.println(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. System.out.println(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!

You might also like