Class String
java.lang.Object
java.lang.String
public final class String extends Object
•The String class represents character strings
How to Create String objects
String str = "abc";
char data[] = {'a', 'b', 'c'};
String str = new String(data);
String cde = "cde";
C lass String has method for
The class String includes methods for
• examining individual characters of the sequence
• comparing strings
• searching strings
• extracting substrings
• creating a copy of a string
char charAt(int index) Returns the char value at the specified index.
int
compareTo(String anotherString) Compares two strings lexicographically.
int
compareToIgnoreCase(String str) Compares two strings lexicographically, ignoring case differences.
String
concat(String str) Concatenates the specified string to the end of this string.
boolean
contentEquals(StringBuffer sb) Compares this string to the specified StringBuffer.
static String
copyValueOf(char[] data) Returns a String that represents the character sequence in the array specified.
static String
copyValueOf(char[] data, int offset, int count) Returns a String that represents the character sequence in the array specified.
boolean
endsWith(String suffix) Tests if this string ends with the specified suffix.
boolean equalsIgnoreCase(String anotherString) Compares this String to another String, ignoring case considerations.
int hashCode() Returns a hash code for this string.
int indexOf(int ch) Returns the index within this string of the first occurrence of the specified character.
int indexOf(int ch, int fromIndex) Returns the index within this string of the first occurrence of the specified character, starting the search at the specified index.
int indexOf(String str) Returns the index within this string of the first occurrence of the specified substring.
int indexOf(String str, int fromIndex) Returns the index within this string of the first occurrence of the specified substring, starting at the specified index.
boolean isEmpty() Returns true if, and only if, length() is 0.
int lastIndexOf(int ch) Returns the index within this string of the last occurrence of the specified character.
int lastIndexOf(int ch, int fromIndex) Returns the index within this string of the last occurrence of the specified character, searching backward
int
lastIndexOf(String str) Returns the index within this string of the rightmost occurrence of the specified substring.
int
lastIndexOf(String str, int fromIndex) Returns the index within this string of the last occurrence of the specified substring,
searching backward starting at the specified index. int length() Returns the length of this string.
char[] toCharArray() Converts this string to a new character array. String toLowerCase()
String
toString() This object (which is already a string!) is itself returned.
static String valueOf(boolean b) static String valueOf(char c)
static String valueOf(char[] data)
static String valueOf(char[] data, int offset, int count) static String valueOf(double d)
static String valueOf(float f) static String valueOf(int i) static String valueOf(long l)
static String
Substring
•
String Str=“abcdefgh”;
•
Str.subString(3);
•
Str.subString(2,4);
•
Str.subString(3,3)
cefgh
length or length()
String a[]={“a”,”b”};
a.length
2
indexOf()
String str=“Nihar ranjan roy”;
int i=str.indexOf(‘a’);
charAt()
String str=“Nihar”;
Char ch=str.charAt(2);
replace()replaces every occurrence of on character with another.
String lang=“english”;
equals() vs ==
“nihar”.equals(“nihar”); Or
String name=“nihar”; Name.equals(“nihar”);
equalsIgnoreCase()
equals compares for contents
== compares for two object references, to see
Example
String s1=“java” String s2=“JAVA”;
toString()
Class Student {String name; int roll
}
Student Obj=new Student(); System.out.println(Obj) ;
what is the output?;
Class Student {….
…..
public String toString() {
System.out.println(“Student@”+name+”@”+”roll”); }
StringBuffer
String class objects are immutable and with fixed length in contrast to StringBuffer class objects whose length can be varied and we can insert characters either in the middle or at the end
Constructor
StringBuffer() 16 char
StringBuffer(int i)
Principal methods
•
append
•
insert
•
length
•
capacity
•
reverse
StringBuffer name=new
StringBuffer(“Nihar”); name.append(“ Roy”);
Append example
class StrBuffer {
public static void main(String args[]) {
StringBuffer str=new StringBuffer(10); str.append("India is");
System.out.println(str);
System.out.println(str.length()); System.out.println(str.capacity()); str.append("a beautiful country"); System.out.println(str.length()); System.out.println(str.capacity()); }
Arrays
Collection of elements of the same type
that are referred by a common name.
int arr=new int[5];
All initialise with default value
MultiDimensional
int arr[][]=new int[4][4];
Initialisation
Arr[0][0]=45;
Arr[0][1]=34;
….
Or
Vector
package import java.util.*;
It permits to store different elements that are of different class.
• Vector in also an expandable array of objects
• Array can be either increased of decreased after
some its elements have been deleted
• We cannot use simple data types in vector
• Vector automatically increased its size when
Constructor
Vector()
Vector(int size)
Vector(int size, int increment)
The value is incremented whenever it
Methods in Vector class
addElement()at the last position
elementAt(int i)returns element at that position
Size()no of objects present
Capacityno of spaces allocated
removeElement(Object o)removes the first
occurrence of the object o in the vectore if and only if it is found