Strings are made up of characters.A character in a string can be refered by its position in the string.The index of the first character in a string is 0.String value cannot be changed.So string is immutable.
String functions:
1. charAt(index)
It gives the character at the specified index.
String s="Computer Application";
char x=s.charAt(3);
The answer is p,since index starts from 0.
2. indexOf(character)
It gives the index of the first occurance of the given character.Its just opposite of charAt.
String s="reading books";
int x=s.indexOf('0');
The answer will be 9,since space is also part of a string
3. lastIndexOf(character)
It gives the index of the last occurance of the given character.
String s="reading books";
int x=s.lastIndexOf('0');
The answer will be 10.
4. length()
It gives the total characters in the string.It value statrs from 1 not 0.Therefore the length of "BCBS" is 4.It includes even the space. Remember the length of the array is length, whereas string is length().
5. toLowerCase()
Converts the string to lowercase letters.
String s="BORDER";
String s1=s.toLowerCase();
The answer will be computer.
6.toUpperCase()
Converts the string to uppercase letters.
String s="tension";
String s1=s.toUpperCase();
The answer will be TENSION.
7. equals()
Compares two strings and return true if both are same else it will return false.
String s="Boys";
String s1="boys";
s.equals(s1) will give you false.
8. equalsIgnoreCase()
Returns true even if the strings are of different cases.
String s="Boys";
String s1="boys";
s.equals(s1) will give you true since it is ignore case.
9. compareTo()
Compares two strings lexicographically.
It gives 0 if both the strings are equal. value less than 0 if string1 lexicographically less than string2, value greater than 0 if string1 lexicographically greater than string2.
String a="Computer";
String b="Application";
int n=a.compareTo(b);
if a and b are same then n=0,
if a is greater than b then n>0,
if a is less than b then n>0, i am refering a always coz in the syntax i have given a first.
10. startsWith()
The result is true if the given string begins with a specified string.
11.endsWith()
The result is true if the given string ends with a specified string.
12. substring(int n )
Removes the part of the string till the end of the string
13. substring(int start, int end)
Removes the part of the string from starting from the index start upto character at the index end without including it.
String s="something";
String s1=s.substring(2,5);
The answer will be met.
14. replace(char ch1,char ch2)
It gives a new string obtained by replacing every occurance of character ch1 by character ch2.
StringBuffer:
You cannot change the size or the content once the strings are created.However, sometimes we need to have a dynamic string that can be modified.So we are using StringBuffer instead of string.
To assign a value:
StringBuffer s=new StringBuffer("Computer");
1. append(char ch)
It adds a character to the end of the StringBuffer.
2. insert(int index, char ch)
Inserts a character into the StringBuffer at the specified index.
3. setCharAt(int index,char c)
It changes the character with ch in the specified location
4. reverse()
5. capacity()
It gives the amount of memory space allocated.
6. delete(index1,index2)
It deletes the characters from index 1 to index2
Character Functions :
1. Character.isLetter()
Checks whether the character is a letter or not.
2.Character.isDigit()
Checks whether the character is a digit or not.
3. Character.isLetterOrDigit()
Checks whether the character is a letter or digit.
4. Character.isWhiteSpace()
Checks whether the character is a space or not.
5. Character.isUpperCase()
It checks whether the character is a uppercase letter.There is a difference between toUpperCase and isUpperCase.refer string functions.
6. Character.isLowerCase()
It checks whether the character is a uppercase letter.There is a difference between toLowerCase and isLowerCase.refer string functions.
7.Character.toUpperCase()
Changes the character into uppercase letter.There is a string function also.This is only for a particular character.
8.Character.toLowerCase()
Changes the character into lowercase letter.There is a string function also.This is only for a particular character.
Converting string data type to other data types:
String to integer
i. Integer.parseInt (string) or
Integer.valueOf(string)
same way you convert string into long,float double.Replace Integer with the corresponding data type.
Converting primitive data types to string :
Integer to string
Integer.toString(integer);
Now you can't do any mathematical operations with this number.
same way you convert long,float double to string.Replace Integer with the corresponding data type.
No comments:
Post a Comment