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

Strings in Java

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
50 views

Strings in Java

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

What is a String?

 Strings are used for storing text.


 A string is a sequence of characters that is
treated as a single value.
 String Literal: Enclosed in double quotes like
 “Hello Java”
 In Java, strings are basically objects of a
predefined Class called “String”
 The java.lang.String class is used to create a
string object.
How To Create a String?
• There are two ways to create String object:
• By new keyword
• By string literal
1) By new keyword
• Since string is an object of class String, We use new operator to
create a string object
• For this, we use a string variable, new operator, and a
constructor method
Example:
String str=new String(“Hello Java”);
OR
String str; // Declare a string variable
Str=new String(“Hello Java”);
//create new object in memory with contents “Hello Java”and assign its reference to str
string variable
Second Method to Create String

• Declare String variable and initialize


with String Literal

• String str1=“Hello Java”;

• String str2=“Programming”;
How To Access Individual Characters from String

• Use charAt(index) method

• For example To print 5th character


• String str=“Hello”; // create & initialize string object
• System.out.println(str.charAt(4)); // 5th item is on index 4

Output:
o
• Use length() Method
• Returns number of characters in string

Example:
String str=“Java”;
System.out.println(str.length());
Output:
4
How To Input String?
• Use Scanner class and next() or nextLine() Methods
import java.util.Scanner;
public class StringInput {
public static void main (String[] args) {
Scanner input = new Scanner(System.in);
String name;
System.out.print("What is your name?");
name = input.next( ); //get one word string, no space
Or
name=input.nextLine(); //get multiple word string-(space)
System.out.print(“Your Name is:”+name);
}
}
How To Input String? Method-II
• Use JOptionPane Class, showInputDialog() method
import javax.swing.JOptionPane;
class StringInput2
{
public static void main(String[] args)
{
String name=JOptionPane.showInputDialog("Enter Name");
System.out.println(name);
JOptionPane.showMessageDialog(null,name);
System.exit(0);
}
}
Display All Chars of String

• Use charAt(index) and length() methods


String str=“Java”;
for(int i=0; i<str.length(); i++) // Loop i=0 to 3
System.out.println(str.charAt(i));
• Output:
J
a
v
a
Program: Reverse a String
import javax.swing.JOptionPane;
public class StringReverse {
public static void main(String[] args) {
String name=JOptionPane.showInputDialog("Enter Name");
String s="";
for(int i = name.length()-1; i>=0; i--) //if name=ABCD, Put D,C,B,A in s variable
s=s+name.charAt(i);
OptionPane.showMessageDialog(null, s);
System.exit(0);

}
}
String Comparison

• Use equals() Method


• equals() method returns true if both strings are
same otherwise false.
Example: string 2
• String s1=“abc”;
• String s2=“abc”;
If(s1.equals(s2))

string 1
String Comparison

Example:
• String s1=“abc”;
• String s2=“abc”;
• If(s1.equals(s2))
JOptionPane.showMessageDialog(null,“Same”);
else
JOptionPane.showMessageDialog(null,“Different”);
String Concatenation
• Use concat() Method or + Operator
• Returns concatenated String
Example:
• String s1=“Hello”;
• String s2=“ Java”;
• String s3=s1.concat(s2);
• String s4=s1 + s2;

JOptionPane.showMessageDialog(null,s3);
JOptionPane.showMessageDialog(null,s4);

You might also like