0% found this document useful (0 votes)
2 views15 pages

Java - Strings

The document provides an overview of strings in Java, explaining that they are objects of the String class and are immutable. It details how to create strings using literals and the new keyword, as well as various string operations such as length, concatenation, and character extraction methods. Additionally, it highlights the use of String, StringBuffer, and StringBuilder classes for string manipulation in Java.

Uploaded by

Vinod Agarkar
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)
2 views15 pages

Java - Strings

The document provides an overview of strings in Java, explaining that they are objects of the String class and are immutable. It details how to create strings using literals and the new keyword, as well as various string operations such as length, concatenation, and character extraction methods. Additionally, it highlights the use of String, StringBuffer, and StringBuilder classes for string manipulation in Java.

Uploaded by

Vinod Agarkar
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

String

Introduction

– Basically string is a sequence of


characters.

– Many programming languages implement


strings as character arrays.

– But in Java, string is an object that


represents a sequence of characters.
Strings
– When a string is created in Java, it actually
creates an object of type String.
– A Java string is an instantiated object of the
String class.
– In Java, strings are class objects and
implemented using two classes namely,
String and StringBuffer.
– These classes are used for string
manipulation.
– String is the only class where operator
overloading is supported in java.
Strings
– In Java, String is immutable object which
means that it cannot be changed once it is
created.
– That means when you create a String object,
you are creating a string that cannot be
changed i.e. you cannot change the
characters that comprise that string.
– But you can perform all types of string
operations.
– The String, StringBuffer and StringBuilder
classes are defined in [Link]. Thus, they
are available to all programs automatically.
String Creation
– There are two ways to create String object
in Java:
1) By string literal
Java String literal is created by using
double quotes. For Example:
String s = "welcome";

2) By new keyword
String s = new String("Welcome");
String Constructor
String class supports several constructors.
1) To create an empty String, the default
constructor is called as follows:
String s = new String();
2) To create a String initialized by an array
of characters :
char chars[] = {„a‟, „b‟, „c‟};
String s=new String(chars);
This constructor initializes s with the string
“abc”.
String Operations
String class supports several operations:
1) String Length
String s = new String(“Welcome”);
[Link]([Link]());

2) String Concatenation
-- By two ways
(i) using “+” operator
(ii) using concat() method
String Operations
(i) using “+” operator
-- operator overloading is not allowed
-- “+” is exception
String age = "9";
String s = "He is " + age +
" years old.";

(ii) using concat() method


String s1="hello";
s1 = [Link](" how are you");
String Operations
String class supports several operations:
3) The valueof() method
– Converts different types of values
in string.
[Link]()
– Example:
int a = 20;
String s1 = [Link](a);
[Link](s1+17);
– Output is 2017
String Operations – Character Extraction
Character can be extracted from string in
number of ways:
1) The charAt() method
– Extract single character from string.
.charAt(where)
– Where is index of character
– Example
char ch;
Ch = “abc”.charAt(1);
– Value of ch is b
String Operations – Character Extraction
2) The getChars() method
– Extract more than one character from string.
– Converts extracted characters into character
array
getChars(SourceStart, SourceEnd,
target[], targetStart)
– Where,
sourceStart – index of the first character
in the string to copy.
sourceEnd – index after the last character
in the string to copy.
String Operations – Character Extraction
target – Destination array of characters in
which the characters from String
gets copied.
targetStart – The index in array starting
from where the chars will be
pushed into the Array.
– Thus, the substring contains the
characters from sourceStart through
sourceEnd–1.
– And target array must be large enough to
hold character array.
String Operations – Character Extraction
Example:
String s = “Computer Science";
char buf[ ] = new char[20];
[Link](3, 6, buf, 0);
[Link](buf);
buf = ?

String s = “Computer Science";


char buf[ ] = new char[20];
[Link](9, 12, buf, 11);
[Link](buf);
buf = ?
String Operations – Character Extraction
Example:
String s = "This is a demo of the getChars";
int start = 10;
int end = 14;
char buf[ ] = new char[end - start];
[Link](start, end, buf, 0);
[Link](buf);

The output of above program is:


String Operations – Character Extraction
Example:
String s = "This is a demo of the getChars";
int start = 10;
int end = 14;
char buf[ ] = new char[end - start];
[Link](start, end, buf, 0);
[Link](buf);

The output of above program is: demo

You might also like