Chapter 03 - Arrays & Strings Part-02 Strings
Chapter 03 - Arrays & Strings Part-02 Strings
Chapter 03 - Arrays & Strings Part-02 Strings
1
PART-02
STRINGS
2
Outline
What are Strings?
String & StringBuffer Classes
String v/s StringBuffer Classes
Different ways to create strings
Using string literals
Using new keyword
String Class methods
Example Java programs
3
What are Strings?
Java string is a sequence of characters. They are
objects of type String.
Once a String object is created it cannot be
changed. Stings are Immutable.
To get changeable strings use the class called
StringBuffer.
String is the only class where operator overloading
is supported in java. We can concat two strings
using + operator. For example "a"+"b"="ab"
String and StringBuffer classes are declared final,
so there cannot be subclasses of these classes. 4
StringBuffer Class
Implemented as a mutable sequence of
characters
Can be modified
Does not provide a set of methods for:
Comparing strings
Locating characters and substrings within a string
Created and stored in a buffer having a set
character capacity
5
String V/S StringBuffer classes
String class
Preferred class for displaying or comparing strings
that tend to remain constant
StringBuffer class
Should be used for applications that require:
Individual character additions
Modifications
Multiple text edits
6
Different Ways to Create String
1)Using string literal
This is the most common way of creating string. In this case a
string literal is enclosed with double quotes.
String str = "abc";
16) toUpperCase( )
Converts all of the characters in this String to upper
case.
Example: “hello there”.toUpperCase();
19
String Class Methods
Conversion methods
Convert strings to and from primitive numerical data
types
General-purpose methods
Typically invoked by preceding the method name with
the String class name and a period
21
Programs
public class countVowLetSpc
{
Java program to count
public static void main(String[] args) number of vowels, letters and
{ spaces in the string text
// Text string to be analyzed
String text = "To be or not to be, that is the question;"
+"Whether ‘tis nobler in the mind to suffer"
+" the slings and arrows of outrageous fortune,"
+" or to take arms against a sea of troubles,"
+" and by opposing end them?";
int spaces = 0, vowels=0, letters=0; // Count of spaces,vowels and letters
// Analyze all the characters in the string
int textLength = text.length(); // Get string length
for(int i = 0; i < textLength; i++)
{
char ch = Character.toLowerCase(text.charAt(i));
if(ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') //check for vowels
vowels++;
if(Character.isLetter(ch)) //check for letters
letters++;
if(Character.isWhitespace(ch)) //check for spaces
spaces++;
}
System.out.println("The text contains:");
System.out.println("vowels:"+vowels+"\nconstants:"+(letters-vowels)+"\nandOUTPUT spaces:"+spaces);
The text contained vowels: 60
} consonants: 93
} spaces: 37
Programs
public class CountTheAnd
{
public static void main(String[] args)
{
// Text string to be analyzed
String text = "To be or not to be, that is the question;"
+ " Whether ‘tis nobler in the mind to suffer"
Java program to find number
+ " the slings and arrows of outrageous fortune," of “ands” and number of
+ " or to take arms against a sea of troubles,"
+ " and by opposing end them?";
“thes” in the string text
int andCount = 0; // Number of and’s
int theCount = 0; // Number of the’s
int index = -1; // Current index position
String andStr = "and"; // Search substring
String theStr = "the"; // Search substring
// Search forwards for “and”
index = text.indexOf(andStr); // Find first ‘and’
while(index >= 0)
{
++andCount; This program will
index += andStr.length(); // Step to position after last ‘and’
index = text.indexOf(andStr, index);
produce the following
} output:
// Search backwards for “the”
index = text.lastIndexOf(theStr); // Find last ‘the’
The text contains 2 ands
while(index >= 0) The text contains 5 thes
{
++theCount;
index -= theStr.length(); // Step to position before last ‘the’
index = text.lastIndexOf(theStr, index);
}
System.out.println("The text contains " + andCount + " ands\n" + "The text contains " + theCount + " thes");
}
}