String and StringBuffer Classes

# String in java is object. Strings are immutable we cannot change the string after creation.
# both String and StringBuffer classes are final.

#String provide different types of constructors:

1) String str =" its string ";
2) String str = new String(); // its a empty string( str is a object, String str = null , str is not an object)
3) String str = new String( "its string ");
4 String str2 = new String (str);
5) char ch[] = { 'a', 'b', 'c'};
    String str2 = new String (ch);
6) String str2 = new String (char ch[], int offset, init count);


# String Operations :
String concatenation : "+" to concat strings .
we can concat string with another data type like a int so first it will convert to string then it will add with string.

Character extraction :

 char charAt(int index);
 char[] toCharArray();
boolean b1 = str.equals(str2);
boolean b1= str.equalsIgnoreCase(str2);
int  b =          str.compareToIgnoreCase(str2);  str2 > str value is grater then zero.
boolean b1 = str.startsWith(str2);
boolean b1 = str.startsWith(str2, startIndex);
boolean b1 =str.endsWith(str2);

Sting search methods:

int a = str.indexOf('c');
int a = str.indexOf('c', 2 );
int a = str.indexOf("its");
int a = str.indexOf("its" , 2);
int a = str.lastIndexOf('c');
int a = str.ladtIndexOf("its");


String str3 = str.substring(int beginIndex);
String str3 = str.substring(int beginIndex, int endIndex)
String str3 = str.concat(String str2) ; // add str2 at end of str
string str3 = str.trim()    //remove white spaces


Data conversion :
static String str3 = str.valueOf(int a); // any object we pass to valueOf it will call toString () method.

String str3 = toUpperCase();
String str3 = toLowerCase();

int a  = str.length();  // return number of characters


StringBuffer Class: its allocate memory.

StringBuffer() //construct a string buffer with no char and initial capacity 16.
StringBuffer(int capacity)
StingBuffer(String str) // capacity  = 16 + size of str.


methods of StringBufffer:
length();
capacity()
ensureCapacity(int minimum capacity);

char charAt(int index)
void setCharAt(int index, char ch);
append(char ch)
append (String str) // add at end

insert(int offset , char c)  / like  append but it add according to offset value.


str,reverse();
delete(int start , int end)
deleteCharAt(int index);
replace(int start , int end, String str)
substring(int start)
substring(int start , int last);












Comments

Popular posts from this blog

Creational Design Pattern: Factory Design Pattern

Introduction to Java

Design Patterns: The Secret Weapon of Expert Developers