Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
4 views

Strings in Java

This Ppt Contains Strings concept in JAVA

Uploaded by

VISNU DHARSINI S
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Strings in Java

This Ppt Contains Strings concept in JAVA

Uploaded by

VISNU DHARSINI S
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 13

STRINGS IN JAVA

Prepared By
Dr.S.Visnu Dharsini
Assistant Professor
CSE
• String Handling – String Constructors, String Length, Special
String Operations -Character Extraction, String Comparison,
Searching Strings, Modifying Strings, using valueOf(),Comparison
of String.

• Collections framework - Collections overview, Collections


Interfaces- Collection Interface, List Interface. Collections Class –
Array List class. Accessing a Collection via an Iterator.

• Event handling - Event Handling Mechanisms, Delegation Event


Model, Event Classes, Sources of Events, Event Listener
Interfaces, Using the delegation Model.
• In java, string is basically an object that represents
sequence of char values. Java String provides a lot of
concepts that can be performed on a string such as
compare, concat, equals, split, length, replace,
compareTo, intern, substring etc.

• In java, string objects are immutable. Immutable


simply means unmodifiable or unchangeable.
String s="javatpoint";
• There are two ways to create String object:
1. By string literal
2. By new keyword

1 ) 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");
• In java, string is basically an object that represents
sequence of char values. Java String provides a
lot of concepts that can be performed on a string
such as compare, concat, equals, split, length,
replace, compareTo, intern, substring etc.
• In java, string objects are immutable.
Immutable simply means unmodifiable or
unchangeable.
String s="javatpoint";
• There are two ways to create String object:
1. By string literal
2. By new keyword
1 ) 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 CONSTRUCTORS

• A string is a sequence of characters. Sometimes we want to


create a string object from different sources. For example,
byte array, character array, StringBuffer, and StringBuilder.
The String class constructors are provided to create a string
object from these arguments
String Constructor Categories
We can broadly classify string constructors into following
categories.
 Creating Empty String
 Creating String from another string
 String from Byte Array
 String from Character Array
 String from Code Points
 String from StringBuffer and StringBuilder
LIST OF JAVA STRING CONSTRUCTORS
1. String(): creates an empty string. It’s mostly useless because String is immutable.
2. String(String original): creates a string object from another string. Since String is
immutable, it’s of no use.
3. String(byte[] bytes): constructs a new string from the byte array using system
default encoding.
4. String(byte bytes[], String charsetName): uses the specified character encoding. If
the encoding is not supported, UnsupportedEncodingException is thrown.
5. String(byte bytes[], Charset charset): a better way to specify an encoding for
constructing the string object.
6. String(byte bytes[], int offset, int length): The offset specifies the index of the first
byte to decode. The length specifies the number of bytes to decode. This
constructor throws IndexOutOfBoundsException if offset is negative, length is
STRING CONSTRUCTORS-CONTD

8. String(byte bytes[], int offset, int length, String charsetName): Similar to above
except that the character set encoding name is passed as a string. This will throw
UnsupportedEncodingException if the encoding is not supported.

9. String(char value[]): creates the string object from the character array.

10. String(char value[], int offset, int count): The offset specifies the index of the first
character. The length specifies the number of characters to use. This constructor
throws IndexOutOfBoundsException if offset is negative, length is negative, or
offset is greater than value.length - length.

11. String(int[] codePoints, int offset, int count): creates a string from the input Unicode
code points array. It throws IllegalArgumentException if any of the code points are
invalid. This constructor throws IndexOutOfBoundsException if the offset is
negative, the length is negative, or offset is greater than codePoints.length - length.

12. String(StringBuffer buffer): creates a new string from the contents of the string
STRING CONSTRUCTORS-
CONTD

String(byte[] byte_arr)
Construct a new String by decoding the byte array. It uses the
platform’s default character set for decoding.
Example

byte[] b_arr = {71, 101, 101, 107, 115};


String s_byte =new String(b_arr); //Geeks

String(byte[] byte_arr, int start_index, int


length)
Construct a new string from the bytes array depending on the start_index(Starting
location) and length(number of characters from starting location).
String(char[] char_arr)
Allocates a new String from the given Character array
Example:
char char_arr[] = {'G', 'e', 'e', 'k', 's'};
String s = new String(char_arr); //Geeks

String(char[] char_array, int start_index, int count)


Allocates a String from a given character array but choose count
characters from the start_index.
Example:
char char_arr[] = {'G', 'e', 'e', 'k', 's'};
String s = new String(char_arr , 1, 3); //eek
STRING LENGTH

• A String in Java is actually an object, which contain methods that


can perform certain operations on strings.
• For example, the length of a string can be found with the length()
method:
public class Main {
public static void main(String[] args) {
String txt = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
System.out.println("The length of the txt string is: " + txt.length());
}
}
Output:
The length of the txt string is: 26
STRING CHARACTER EXTRACTION
METHODS
1. Extracting a Single Character 2. Extracting a Substring
charAt(int index) substring(int beginIndex) and
substring(int beginIndex, int endIndex)

The charAt method returns the character value at


These methods return a new string that is a substring of
the specified index . the original string. The beginIndex is inclusive, and the
endIndex is exclusive.

Example: Example:

String str = "JavaGuides"; String str = "JavaGuides";


String subStr = str.substring(4, 10);
char ch = str.charAt(3);
// Result: "Guides"
// Result: 'a'
3. Extracting Characters into an Array
To CharArray() and
getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)

The to CharArray method returns a newly allocated character array whose


length is the length of the string and whose contents are initialized to contain
the character sequence represented by the string.

The getChars method copies characters from a string into the destination
character array.
THANK YOU

You might also like