Stringconstructor, Charextract, STRCMP
Stringconstructor, Charextract, STRCMP
Baskar
1. String
Constructors
2. Character
Extraction
3. String
Comparison
Dr.N.Baskar
String Constructors
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
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)
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
Dr.N.Baskar
String Comparison
Dr.N.Baskar
String Comparison
Dr.N.Baskar
String Comparison
Dr.N.Baskar
String Comparison
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
https://forms.gle/TENyiEMp4v5DFG1CA
Dr.N.Baskar
Dr.N.Baskar