Java Strings methods and operations.ppt

JyothiAmpally 43 views 18 slides Aug 05, 2024
Slide 1
Slide 1 of 18
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18

About This Presentation

Java Strings and its operations


Slide Content

●This module introduces the participants to the string
object.
●Importance of String Objects and How to create them.
●Varioius Constructors that are available to create
Strings.
●Working with methods that are available with strings.
●Understanding Necessity of StringBuffer class.
●Working with methods of StringBuffer class.
●Introduction to StringBuilder class.
●Working with methods of StringBuilder class.
Module Overview
2

Uderstand the concept of Strings and create string
objects.
Understand advantages and disadvantages of Strings.
Declare and create Strings using different types of
constructors.
Working with Strings and its different types of
methods.
Determine the usage of String Buffer and
StringBuilder.
Objectives
3

String: Strings are a sequence of characters, which are
implemented as objects.
Charecteristics of Strings:
•The String class is immutable,meaning once created
a String object cannot be changed.
•If any modifications is done to the existing string
object a new object is created internally.
•String objects are created by using the new keyword
and a constructor. The String class has eleven
constructors that allow you to provide the initial
value of the string using different sources, such as an
array of characters.
String
4

String Declaration
String Declaration: The most direct way to create a
string is as follows;
String greeting = "Hello world!";//String literal
Other Common Declarations:
String helloString = new String(helloArray);
char[] helloArray = { 'h', 'e', 'l', 'l', 'o', '.'};

Example of String Class.
public class StringDemo{
public static void main(String args[]){
char[] helloArray = { 'h', 'e', 'l', 'l', 'o', '.'};
String helloString = new String(helloArray);
String helloString2 = new String(“hello!!!”);
System.out.println( helloString );
}
}
Output:hello.

Methods Of String
Accessor:Methods used to obtain information
about an object are known as accessor methods.
One accessor method that you can use with
strings is the length() method, which returns the
number of characters contained in the string
object.
Syntax:
<StringOBject>.length();
Example:
String name = “java”;
int length = name.length();

Methods of String(Cont…)
Concatenating Strings:One String can be
concatenated to another string.This will produce
a new string object joining two strings.
Strings are more commonly concatenated with the
+ operator and concat method.
Syntax:
string1.concat(string2);
string1+string2

Methods of String(Cont…)
String substring(int beginIndex)
Returns a new string that is a substring of this string.
char[] toCharArray()
Converts this string to a new character array.
String toLowerCase()
Converts all of the characters in this String to lower
case using the rules of the default locale.
String replace(char oldChar, char newChar)
Returns a new string resulting from replacing all
occurrences of oldChar in this string with newChar.
String[] split(String regex)
Splits this string around matches of the given regular
expression.

String toString()
This object (which is already a string!) is itself returned.
String toUpperCase()
Converts all of the characters in this String to upper case
using the rules of the default locale.
String trim()
Returns a copy of the string, with leading and trailing
whitespace omitted.
boolean startsWith(String prefix)
Tests if this string starts with the specified prefix.
boolean contentEquals(StringBuffer sb)
Returns true if and only if this String represents the same
sequence of characters as the specified StringBuffer.
Methods of String(Cont…)

Working with methods using Strings
Like other values to (parameters)methods, Strings
also can be passed to methods.
Ex:
public String printString(String s) {
System.out.println(s);
return “modified”+s;
}
String newString =printString(new
String(“Java”));//invoking an array

Necessity of StringBuffer
String objects are immutable,meaning
modifications to the string object cannot be done
once created. If done a new String Object will be
created in memory.
The StringBuffer and StringBuilder classes are
used when there is a necessity to make alot of
modifications to Strings of characters.
StringBuffer and Stringbuilder can be modified
over and over again with out leaving behind alot
of new unused objects.

String Buffers Methods
public StringBuffer append(String s)
Updates the value of the object that invoked the
method. The method takes boolean, char, int,
long, Strings etc.
public StringBuffer reverse()
The method reverses the value of the
StringBuffer object that invoked the method.
public delete(int start, int end)
Deletes the string starting from start index until
end index.

String Buffers Methods
void setLength(int newLength)
Sets the length of this String buffer.
int length()
Returns the length (character count) of this string
buffer.
void ensureCapacity(int minimumCapacity)
Ensures that the capacity of the buffer is at least
equal to the specified minimum.
int capacity()
Returns the current capacity of the String buffer.

String Builder Methods
int length()
Returns the length (character count) of this string
buffer.
void ensureCapacity(int minimumCapacity)
Ensures that the capacity of the buffer is at least
equal to the specified minimum.
int capacity()
Returns the current capacity of the String buffer.
reverse() :Causes this character sequence to be
replaced by the reverse of the sequence.

StringBuilder vs StringBuffer
Below is a table that contains differences between
StringBuilder and StringBuffer.
StringBuffer StringBuilder
It is mutable It is mutable
It is
synchronized,provides
ThreadSafety.
It is non
synchronized.Provides no
ThreadSafety.
Capacity increases
automatically .
Capacity increases
automatically .

Excercise
Create an application that will convert a given
character array to a string.
Creat an application that will create a reverse of
a string.
Create an application that uses StringBuffer to
concatinate the user input.
Create an application that uses a StringBuilder
and String Buffer and insert one in another.

Thank You