0% found this document useful (0 votes)
3 views24 pages

Stringconstructor, Charextract, STRCMP

The document provides an overview of the String class in Java, detailing its constructors, character extraction methods, and comparison techniques. It explains how to create Strings using character arrays and byte arrays, as well as methods for extracting characters and comparing Strings. Key methods discussed include equals(), equalsIgnoreCase(), startsWith(), endsWith(), and compareTo().

Uploaded by

rajkumarm
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)
3 views24 pages

Stringconstructor, Charextract, STRCMP

The document provides an overview of the String class in Java, detailing its constructors, character extraction methods, and comparison techniques. It explains how to create Strings using character arrays and byte arrays, as well as methods for extracting characters and comparing Strings. Key methods discussed include equals(), equalsIgnoreCase(), startsWith(), endsWith(), and compareTo().

Uploaded by

rajkumarm
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
You are on page 1/ 24

Dr.N.

Baskar
1. String
Constructors
2. Character
Extraction
3. String
Comparison

Dr.N.Baskar
String Constructors

 String: An object storing a sequence of text characters.


 The String class supports several constructors.
 To create an empty String use default constructor.
 String s = new String();
 To create a String initialized by an array of characters, use the
constructor
 Syntax: String(char chars[])

Dr.N.Baskar
 Example: char ab[ ] = {‘a’,’b’,’c’};
String s = new String (ab);
 To specify a sub range of a character array as an
initializer using the constructor :
 Syntax: String(char ab[ ], int startindex, int
numchars)
 Example: char chars[ ] = {’a’,’b’,’c’,’d’,’e’,’f’};
String s = new String(chars, 2, 3);
Output: cde

Dr.N.Baskar
String Constructors
 String object that contains the same character
sequence as another String object using this
constructor:
Syntax: String(String strObj)Example:
class makestring
{
public static void main(String args[])
{ char c[]={’J’,’a’,’v’,’a’};
String s1=new String(c);
String s2=new String(s1);
System.out.println(s1);
System.out.println(s2);
}
}
Dr.N.Baskar
String Constructors

 The String class provides constructors that initialize a string


when given a byte array.
 Syntax
String(byte asciiChars[ ])
String(byte asciiChars[ ], int startIndex,int numChars)

Dr.N.Baskar
 Example
class substringcons
{
public static void main(String args[])
{
byte ascii[ ] ={65,66,67,68,69,70};
String s1 = new String(ascii);
System.out.println(s1);
String s2 = new String(ascii,2,3);
System.out.println(s2);
}
}
Character Extraction

 The String class provides a number of ways in which characters can be extracted from a String object.
 charAt() -To extract a single character from a String. Return the char at the specified position.
Syntax: char chatAt(int where)
Example:
char ch = “abc”.charAt(1);
Output: b

Dr.N.Baskar
getChars(): To extract more than one characters one at a time
Syntax: void getChars(int sourceStart, int sourceEnd, char target[ ] , int targetStart)

Index of the beginning of the sub


index i.e., one past the end of the desired substring
array that will receive the characters is specified by target
Index within target at which the substring will be copied is passed in targetStart

Dr.N.Baskar
Character Extraction
class getcharsdemo
{public static void main(String ab[])
{
String s="this is a demo of the getchars
method.";
int start=10;
int end=14;
char buf[ ] =new char[end-start];
s.getChars(start,end,buf,0);
System.out.println(buf);
}
}
Dr.N.Baskar
Character Extraction
 getBytes() : It uses the default character-to-byte conversions provided by the platform.
 Syntax : byte[ ] = getBytes();
Example
public class StringGetBytesExample
{ public static void main(String ab[])
{ String s1="ABCDE";
byte[] barr=s1.getBytes();
for(int i=0;i<barr.length;i++)
{
System.out.println(barr[i]);
}
}
}
Dr.N.Baskar
Character Extraction
 toCharArray( ) : To convert all the characters in a String object into character array, the
easiest way is to call toCharArray().
 Syntax: char[] toCharArray()

Example
public class StringToCharArrayExample{
public static void main(String args[]){
String s1="hello";
char[] ch=s1.toCharArray();
for(int i=0;i<ch.length;i++){
System.out.print(ch[i]);
}
}}
Dr.N.Baskar
To create an empty String use _______ constructor.

Answer: C) default

Dr.N.Baskar
String Comparison

 The string class includes several methods that


compare strings or substrings within strings.
 equals() : Compares the two given strings
based on the content of the string. If any
character is not matched, it returns false. If all
characters are matched, it returns true.
 Syntax
 public boolean equals(Object anotherObject)
 anotherObject : another object i.e.
compared with this string

Dr.N.Baskar
String Comparison

 equalsIgnoreCase() : Compares the two


given strings on the basis of content of
the string irrespective of case of the
string. If any character is not matched, it
returns false otherwise it returns true.
 syntax
 public boolean equalsIgnoreCase
(String str)

Dr.N.Baskar
String Comparison

 RegionMatches( ) : Compares a specific region inside


a string with another specific region in another string.
 Syntax
boolean regionMatches(int startIndex, String
str2,int str2StastIndex,int numChars)

Specifies the index at which the region begins

string being compared


Comparision will start Within str2 is specified here
Length of the substring being compared

Dr.N.Baskar
String Comparison

 The java string startsWith() method checks if this


string starts with given prefix. It returns true if this
string starts with given prefix else returns false.
 Syntax
public boolean startsWith(String prefix)
 prefix : Sequence of character

public boolean startsWith(String prefix, int offset)

 This is overloaded method of startWith()


method which is used to pass one extra
argument (offset) to the function.

Dr.N.Baskar
String Comparison

 The java string endsWith() method


checks if this string ends with given suffix.
It returns true if this string ends with given
suffix else returns false.
 syntax
public boolean endsWith
(String suffix)

Dr.N.Baskar
String Comparison
 equals( ) Versus = =
 compares a characters inside
compares two object references
to see
 a String object whether they refer
to the same instance.
 equals( ) :
 syntax
 public boolean equals( Object
anotherObject)

Dr.N.Baskar
String Comparison
compareTo( ) : It is not enough to simply know
whether 2 strings are identical. For sorting
applications, you need to know which is less
than, equal to, greater than the next.
 A string is less than another if it comes
before the other in dictionary order.
 A string is greater than another if it
comes after the other in dictionary
order.
 Syntax
 int compareTo(String str)
 if s1 > s2, it returns positive number
 if s1 < s2, it returns negative number
 if s1 == s2, it returns 0

Dr.N.Baskar
The java string endsWith() method checks if this string ends with
given___________.

Answer: A) suffix

Dr.N.Baskar
Summary
String: An object storing a sequence of text characters.
To create an empty String use default constructor
To specify a sub range of a character array as an initializer using the constructor.
The String class provides constructors that initialize a string when given a byte
array.
getBytes() uses the default character-to-byte conversions provided by the
platform.
String Comparison methods are equalsIgnoreCase() , startwith(), endswith(),
equals, compareTo().,

Dr.N.Baskar
Quiz

Scan QR Code (or) Link

https://forms.gle/TENyiEMp4v5DFG1CA

Dr.N.Baskar
Dr.N.Baskar

You might also like