1. byte_arr, int start_index, int length) It constructs a

1.     
Objective

 

In this tutorial for Java we are going to
learn about the various types of strings in Java, how to declare them, the
syntax used for them, and examples for them.

We Will Write a Custom Essay Specifically
For You For Only $13.90/page!


order now

 

2.     
Introduction

 

Strings are a collection of characters, and
in Java they are immutable, i.e. they cannot be changed once created.

There are two ways to create a string –

·        
Using a string literal –

String s = “DataFlair”;

 

·        
Using a new keyword –

String s = new String (“DataFlair”);

 

3.     
Constructors

 

a.     
String(byte byte_arr)

 

It constructs a new string by decoding of
the byte array using the default character set of the platform for decoding.

Example –

byte b_arr = {81, 101, 101, 107, 115};

String s_byte =new String(b_arr);

b.     
String(byte byte_arr, Charset
char_set)

 

It constructs a new string by decoding of
the byte array, it uses the char_set for decoding.

Example –

byte b_arr = {81, 101, 101, 107, 115};

Charset cs = Charset.defaultCharset();

String s_byte_char = new String(b_arr, cs);

 

c.      
String(byte byte_arr, String
char_set_name)

 

It constructs a new string by decoding of
the byte array using the char_set_name of the platform for decoding.

Example –

byte b_arr = {68, 101, 101, 107, 115};

String s = new String(b_arr,
“US-ASCII”);

 

d.     
String(byte byte_arr, int
start_index, int length)

 

It constructs a new string by decoding of
the byte array depending on the start_index(Starting location) and
length(number of characters from starting location), it used char_set for
decpding.

Example –

byte b_arr = {68, 101, 101, 107, 115};

Charset cs = Charset.defaultCharset();

String s = new String(b_arr, 1, 3, cs);

 

e.     
String(byte byte_arr, int
start_index, int length, String char_set_name)

 

It constructs a new string by decoding of
the byte array depending on the start_index(Starting location) and
length(number of characters from starting location), it uses char_set_name for
decoding.

Example –

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

String s = new String(b_arr, 1, 4,
“US-ASCII”);

 

f.       
String(char char_arr)

 

This method allocates a new string from the
character array.

Example –

char char_arr = {‘d’, ‘o’, ‘g’, ‘k’,
‘s’};

String s = new String(char_arr);

 

g.     
String(char char_array, int
start_index, int count)

 

This method allocates a string to the given
character array, the only difference is that it chooses count characters from
the start index.

Example –

char char_arr = {‘w’, ‘e’, ‘e’, ‘k’,
‘s’};

String s = new String(char_arr , 1, 3);

 

h.     
String(int uni_code_points,
int offset, int count)

 

This method allocates a String from a uni_code_array
but chooses the character from start_index.

Example –

int uni_code = {61, 101, 101, 107, 115};

String s = new String(uni_code, 1, 3);

 

i.       
String(StringBuffer s_buffer)

 

This method allocates a new method to the
string in s_buffer.

Example –

String s_buffer = “dogs”;

String s = new String(s_buffer);

 

j.       
String(StringBuilder s_builder)

 

This method allocates a new method to the
string in s_builder.

Example –

String s_builder = “dogs”;

String s = new String(s_builder);

 

4.     
String methods

 

a.     
int length()

 

This method returns the number of character
in the string.

Example –

“Shadow”.length();

 

b.     
Char charAt(int i)

 

This method returns the character at Ith
index.

Example –

“Shadow”.charAt(3);

 

c.      
String substring (int i)

 

This method returns the ith index substring.

Example –

“Shadow”.substring(3);

 

d.     
String substring (int i, int j)

 

This method returns the substring from I to
j-1 index.

Example –

“Shadow”.substring(2, 5);

 

 

e.     
String concat( String str)

 

This method concatenates specified string
to the end of this string.

Example –

String s1 = “Shadow”;

 String s2 = “isfive”;

 String output = s1.concat(s2);

 

f.       
int indexOf (String s)

 

This method returns the index within the
string of the first occurrence of the specified string.

Example –

String s = “Shadow is Five”;

 int
output = s.indexOf(“Five”);

 

g.     
int indexOf (String s, int i)

 

This method returns index within the string
of the first occurrence of the specified string, starting at the specified
index.

Example –

String s = “Shadow is Five”;

 int
output = s.indexOf(‘a’,3);

 

h.     
Int lastindexOf( int ch)

 

This method returns the index within the
string of the last occurrence of the specified string.

Example –

String s = “Shadow is five”;

 int
output = s.lastindexOf(‘a’);

 

i.       
boolean equals( Object
otherObj)

 

This method compares this string to the
specified object.

Example –

Boolean out = “shadow”.equals(“shadow”); //
returns true

 Boolean out = “shadow”.equals(“Shadow”); //
returns false

 

j.       
boolean  equalsIgnoreCase (String anotherString)

 

This method compares string to another
string, ignoring case considerations.

Example –

Boolean out= “shadow”.equalsIgnoreCase(“shadow”);
// returns true

 Boolean out = “Shadow”.equalsIgnoreCase(“shadow”);
// returns true

 

k.      
int compareTo( String
anotherString)

 

This method compares two string
lexicographically.

Example –

int out = s1.compareTo(s2);  // where s1 ans s2 are

                             // strings to be
compared