Java - Introduction to Programming
Lecture 12
Strings
Declaration
String name = "Tony";
Taking Input
Scanner sc = new Scanner([Link]);
String name = [Link]();
Concatenation (Joining 2 strings)
String firstName = "Tony";
String secondName = "Stark";
String fullName = firstName + " " + secondName;
[Link](fullName);
Print length of a String
String firstName = "Tony";
String secondName = "Stark";
String fullName = firstName + " " + secondName;
[Link]([Link]());
Access characters of a string
String firstName = "Tony";
String secondName = "Stark";
String fullName = firstName + " " + secondName;
for(int i=0; i<[Link](); i++) {
[Link]([Link](i));
}
Apna College
Compare 2 strings
import [Link].*;
public class Strings {
public static void main(String args[]) {
String name1 = "Tony";
String name2 = "Tony";
if([Link](name2)) {
[Link]("They are the same string");
} else {
[Link]("They are different strings");
}
//DO NOT USE == to check for string equality
//Gives correct answer here
if(name1 == name2) {
[Link]("They are the same string");
} else {
[Link]("They are different strings");
}
//Gives incorrect answer here
if(new String("Tony") == new String("Tony")) {
[Link]("They are the same string");
} else {
[Link]("They are different strings");
}
}
}
Substring
The substring of a string is a subpart of it.
public class Strings {
public static void main(String args[]) {
String name = "TonyStark";
[Link]([Link](0, 4));
Apna College
}
}
ParseInt Method of Integer class
public class Strings {
public static void main(String args[]) {
String str = "123";
int number = [Link](str);
[Link](number);
}
}
ToString Method of String class
public class Strings {
public static void main(String args[]) {
int number = 123;
String str = [Link](number);
[Link]([Link]());
}
}
ALWAYS REMEMBER : Java Strings are Immutable.
Apna College
Homework Problems
1. Take an array of Strings input from the user & find the cumulative (combined)
length of all those strings.
import [Link].*;
public class Strings {
public static void main(String args[]) {
Scanner sc = new Scanner ([Link]);
int size = [Link]();
String array[] = new String[size];
int totLength = 0;
for(int i=0; i<size; i++) {
array[i] = [Link]();
totLength += array[i].length();
[Link](totLength);
2. Input a string from the user. Create a new string called ‘result’ in which you will
replace the letter ‘e’ in the original string with letter ‘i’.
Example :
original = “eabcdef’ ; result = “iabcdif”
Original = “xyz” ; result = “xyz”
Apna College
import [Link].*;
public class Strings {
public static void main(String args[]) {
Scanner sc = new Scanner ([Link]);
String str = [Link]();
String result = "";
for(int i=0; i<[Link](); i++) {
if([Link](i) == 'e') {
result += 'i';
} else {
result += [Link](i);
[Link](result);
3. Input an email from the user. You have to create a username from the email by
deleting the part that comes after ‘@’. Display that username to the user.
Example :
email = “apnaCollegeJava@[Link]” ; username = “apnaCollegeJava”
email = “helloWorld123@[Link]”; username = “helloWorld123”
Apna College
import [Link].*;
public class Strings {
public static void main(String args[]) {
Scanner sc = new Scanner ([Link]);
String email = [Link]();
String userName = "";
for(int i=0; i<[Link](); i++) {
if([Link](i) == '@') {
break;
} else {
userName += [Link](i);
[Link](userName);
Apna College