String Manipulation: String Function
To input String
String str = [Link](); //word
To input statement
String str = [Link](); //statement
To find the length of a String object
int len= str. length() ;
Default values
String s=””;
String s1=”\0”;
String m=null;
char ch =’\0’;
Removing LEADING and TRAILING spaces.
Str=[Link]();
To find length() of String
String S= “SUNSHINE”;
int len =[Link](); // 8
TO join (concatenate two Strings)
String ns= [Link](str2) OR String ns= strl + str2;
To Extract /chop characters from an index
int ind=[Link](index);
String st= “GATE”;
char ch= [Link](2) ;
OUTP: T
To find the index of a particular char or String
[Link](char/String)
Finds the first occurrence of String or char
Eg. String s=”WOODEN”;
int in1=[Link](“DEN”);
OUTP : 3
int in2=[Link](‘O’);
OUTP : 1
int in3=[Link](‘D’);
OUTP : 3
Eg. String s=”APPLICATIONS”;
int in3=[Link](‘A’,1); // FIRST OCC FROM THE SPECIFIED SEARCH INDEX
OUTP: 6
compareTo()
Comparing Strings
both strl and str2 are exactly the same If result == 0
strl is alphabetically greater than str2 If result > 0
strl is alphabetically smaller than str2 If result <0
Eg.
String strl="FRUIT"; String str2="FRUIT";
int result = [Link](str2);
Outp: 0 (then result will be = 0)
String strl="DOG"; String str2="CAT";
int result = [Link](str2);
Outp: 1 (then result will be > 0]
String strl="Bombay"; String str2="Delhi";
int result = [Link](str2);
Outp : -1 (then res will be <0)
compareToIgnoreCase() // ignores case sensitivity
Replacing char/String with new char/String:
s = [Link](old char/Str, new char/Str);
Eg:
String s=” GREEN GREEN FOREST GREEN”;
s = [Link](“GREEN”, “YELLOW”);
OUTP: YELLOW YELLOW FOREST YELLOW
substring()
Returning substring of String (PART OF String)
1. String ss= [Link](beginIndexNo);
2. String ss= [Link](beginIndexNo, endIndexNo);
String st=” GOOD MORNING”;
String sbst=[Link](0); // GOOD MORNING LEN 12 (FROM 0 TO 11)
[Link]([Link](5); // MORNING (FROM 5 TO 11)
[Link]([Link](0,4); // GOOD (FROM 0 TO 3)
[Link]([Link](0,[Link]() ); // GOOD MORNING (FROM 0 TO 11)
[Link]([Link](0,[Link]()-1) ) // GOOD MORNIN (FROM 0 10)
Getting ascii code of a character:
int asc = character Variable;
int ASC= ‘A’; // 65
Getting char of ascii code
character Variable = (char) asc;
char V = (char) ASC;
Converting all chars OF String to lower
newstr = [Link]();
Converting all char OF String to upper
newstr =[Link]():
IMPORTANT
Converting NUMERIC String to an integer number
int num= [Link](strv);
String NumSt="23786";
int no= [Link](NumSt);
int b=no+2;
[Link](b);
OUTPUT : 23788
Converting NUMERIC String to an double number
double num= [Link](strv);
String NumSt="15.86";
double no= [Link](NumSt);
double b=no+2;
[Link](b);
Output : 17.86 //addition
Converting NUMERIC String to an Long number
double num= [Link](strv);
Converting NUMERIC String to an float number
float num= [Link](strv);
Converting NUMBER to String
String newstr =String. valueOf(number);
String d=[Link](12.3);
[Link](d);
12.3
[Link](d+5);
12.35 // CONCATENATION
( String Functions returning boolean result (does not work on characters))
{ startsWith () AND endsWith() ARE :
To check if a String starts with a given string:
boolean result = [Link](String);
eg: String word = “COMPUTER”;
boolean result = [Link](“COMP”);
OUTPT: true
To check if a String ends with a given string
boolean result = [Link] With (String);
eg: String word = “COMPUTER IS FUN”;
boolean result = [Link] With (“FUN”);
OUTPT: true
(does not work on characters)) IF YOU WISH TO CHECK A CHARACTER THEN DO THE
FOLLOWING)
String word = "BLUEBELLS";
boolean RESULT = [Link]("B"); // NOTICE THE “ “ USED TO CHECK B SO IT’S A String
[Link](RESULT);
To check if two Strings are same (case sensitive):
boolean result = [Link](strv2);
To check if two strings are same (ignore case):
boolean result = [Link](strv2);
A TO Z 65 TO 90
a to z 97 to 122
0 to 9 48 to 57
Space ‘ ‘ 32
Diff between upper and lower 32
Character functions :
To check if a character is an alphabet
boolean g=-[Link](char var/const);
To check if a character is a digit
boolean f= = [Link](char var/const);
To check if a character is an alphabet or digits
boolean j=-[Link](char var/const);
To check if a character is a Whitespace
boolean k=[Link](char var/const);
To check if a character is a upper case alphabet:
boolean k=[Link](ch vr/const);
To check if a character is a lower case alphabet:
boolean g=[Link](ch vr/const);
To convert character from lower case to upper case:
char ch=[Link](char var/const);
To convert character from upper case to lower case:
char ch=Character. toLowerCase(char var/const);
(THERE ARE 6 NOT 8 FUNCTIONS FOR UPPER /LOWER CASE 2 FOR String 4 for Character (is/to))
GOLDEN RULE : ONLY Strings concatenate
String x="DAISY";
[Link] ([Link](0)+[Link](3));
OUTPUT : 151 // REMEMBER GOLDEN RULE
[Link] (""+[Link](0)+[Link](3)); // NOTICE null String
OUTPUT: DS