KEMBAR78
Learn Java Part 6 | PDF
String Functions
• String concat(String str)- Concat string str to given string, and returns the concated
string.
String s=“Hello”;
s.concat(“ Java”); //s will not be updated
System.out.print(s);
will print Hello
System.out.print(s.concat(“ Java”));
will print Hello Java
s=s.concat(“ Java”); //now s will be updated
System.out.print(s);
will print Hello Java
• int length()- returns the length of string
String s=“Hello”;
int len=s.length();
System.out.print(len);
will print 5
• String trim()- removes the spaces from start and end
String s=“ hello ”;
s.trim(); //s will not be affected
System.out.print(s);
will print: Hello (with spaces)
System.out.print(s.trim());
will print: Hello (Without spaces)
s=s.trim(); //now s will be affected
System.out.print(s);
will print: Hello (Without spaces)
• String toUpperCase()- returns the string with all characters in Upper Case
String s=“Hello”;
s.toUpperCase(); //s will not be affected
System.out.print(s);
will print Hello
System.out.print(s.toUpperCase());
will print HELLO
s=s.toUpperCase(); //now s will be affected
System.out.print(s);
will print HELLO
• String toLowerCase()- returns the string with all characters in Lower Case
String s=“Hello”;
s.toLowerCase(); //s will not be affected
System.out.print(s);
will print Hello
System.out.print(s.toLowerCase());
will print hello
s=s.toLowerCase(); //now s will be affected
System.out.print(s);
will print hello
• Boolean isEmpty()- returns true if string is empty, and returns false if string is not empty
String s=“Hello”;
System.out.print(s.isEmpty());
will print false
String s=“”;
System.out.print(s.isEmpty());
will print true
• Boolean startsWith(String str)- returns true if given string starts with string str, and
returns false if given string do not starts with string str
String s=“Hello”;
System.out.print(s.startsWith(“H”));
will print true
System.out.print(s.startsWith(“G”));
will print false
• Boolean endsWith(String str)- returns true if given string ends with string str, and
returns false if given string do not ends with string str
String s=“Hello”;
System.out.print(s.endsWith(“elo”));
will print true
System.out.print(s.startsWith(“HH”));
will print false
• Boolean equals(String str)- returns true if all characters of given string is equal to all
characters of str
String s1=“Hello”;
String s2=“Hello”;
System.out.print(s1. equals(s2));
will print true
• Boolean equalsIgnoreCase(String str)- returns true if all characters of given string is
equal to all characters of str by ignoring the case
String s1=“Hello”;
String s2=“HELLO”;
System.out.print(s1. equalsIgnoreCase(s2));
will print true
• String replace(char oldCharacter, char newCharacter)- replace the all occurrence of
oldCharacter with newCharacter
String s1=“Hello”;
System.out.print(s1. replace(‘l’,’$’));
will print He$$o
To update s1 we have to assign it:
s1=s1. replace(‘l’,’$’);
• Boolean equalsIgnoreCase(String str)- returns true if all characters of given string is
equal to all characters of str by ignoring the case
String s1=“Hello”;
String s2=“HELLO”;
System.out.print(s1. equalsIgnoreCase(s2));
will print true
• String replace(char oldCharacter, char newCharacter)- replace the all occurrence of
oldCharacter with newCharacter
String s1=“Hello”;
System.out.print(s1. replace(‘l’,’$’));
will print He$$o
To update s1 we have to assign it:
s1=s1. replace(‘l’,’$’);
• String replace(String oldString, String newString)- replace the all occurrence of oldString
with newString
String s1=“Hello”;
System.out.print(s1. replace(“ll” , ”%”));
will print He%o
To update s1 we have to assign it:
s1=s1. replace(“ll” , ”%”);
• String substring(int start)- returns substring from ‘start’ index. ‘start’ is included.
String s1=“ABCDEF”;
System.out.print(s1. substring(3));
will print DEF
• String substring(int start,int end)- returns substring from ‘start’ index to ‘end’ indes.
‘start’ is included while ‘end’ is exculded
String s1=“ABCDEF”;
System.out.print(s1. substring(2,5));
will print CDE
• char charAt(int index) – returns the character at given index
String s1=“ABCDEFG”;
System.out.print(s1.charAt(3));
will print D
• int indexOf(char ch)- returns the first index of given character
int indexOf(String str)- returns the first index from where given string starts
int indexOf(char ch,int start)- returns the first index of given character after the start
index. start is included
int indexOf(String str,int start)- returns the first index from where given string starts
after the start index. start is included
String s1=“ABCDEFABCDEF”;
System.out.print(s1. indexOf(‘B’));
will print 1
System.out.print(s1. indexOf(‘B’,5));
will print 7
• int lastIndexOf(char ch)- returns the last index of given character
int lastIndexOf (String str)- returns the last index from where given string starts
int lastIndexOf (char ch,int last)- returns the last index of given character before the
last index. last is included
int lastIndexOf(String str,int last)- returns the last index from where given string starts
before the last index. last is included
String s1=“ABCDEFABCDEF”;
System.out.print(s1. lastIndexOf(‘B’));
will print 7
• Boolean contains(String str)- returns true if string str is present in given string
String s1=“ABCDEFABCDEF”;
System.out.print(s1. contains(“ABC”));
will print true
• int compareTo(String str)- compares the string str with given string
String s1=“ABC”;
String s2=“abc”;
System.out.print(s1.compareTo(s2));
will print -32
• int compareToIgnoreCase(String str)- compares the string str with given string by
ignoring the case
String s1=“ABC”;
String s2=“abc”;
System.out.print(s1.compareToIgnoreCase(s2));
will print 0
• char[] toCharArray()- converts the string to array of characters
String s1=“ABC”;
char ch[]=s1.toCharArray();
System.out.print(ch[2]);
will print C
• byte[] toByteArray()- converts the string to array of bytes
String s1=“ABC”;
byte ch[]=s1.toByteArray();
System.out.print(ch[0]);
will print 65
• String[] split(String str)- splits the given string to string array. Splits after str
String s1=“Hello World”;
String ch[]=s1.split(“ “);
System.out.print(ch[0]);
will print Hello

Learn Java Part 6

  • 1.
  • 2.
    • String concat(Stringstr)- Concat string str to given string, and returns the concated string. String s=“Hello”; s.concat(“ Java”); //s will not be updated System.out.print(s); will print Hello System.out.print(s.concat(“ Java”)); will print Hello Java s=s.concat(“ Java”); //now s will be updated System.out.print(s); will print Hello Java
  • 3.
    • int length()-returns the length of string String s=“Hello”; int len=s.length(); System.out.print(len); will print 5 • String trim()- removes the spaces from start and end String s=“ hello ”; s.trim(); //s will not be affected System.out.print(s); will print: Hello (with spaces) System.out.print(s.trim()); will print: Hello (Without spaces) s=s.trim(); //now s will be affected System.out.print(s); will print: Hello (Without spaces)
  • 4.
    • String toUpperCase()-returns the string with all characters in Upper Case String s=“Hello”; s.toUpperCase(); //s will not be affected System.out.print(s); will print Hello System.out.print(s.toUpperCase()); will print HELLO s=s.toUpperCase(); //now s will be affected System.out.print(s); will print HELLO
  • 5.
    • String toLowerCase()-returns the string with all characters in Lower Case String s=“Hello”; s.toLowerCase(); //s will not be affected System.out.print(s); will print Hello System.out.print(s.toLowerCase()); will print hello s=s.toLowerCase(); //now s will be affected System.out.print(s); will print hello
  • 6.
    • Boolean isEmpty()-returns true if string is empty, and returns false if string is not empty String s=“Hello”; System.out.print(s.isEmpty()); will print false String s=“”; System.out.print(s.isEmpty()); will print true • Boolean startsWith(String str)- returns true if given string starts with string str, and returns false if given string do not starts with string str String s=“Hello”; System.out.print(s.startsWith(“H”)); will print true System.out.print(s.startsWith(“G”)); will print false
  • 7.
    • Boolean endsWith(Stringstr)- returns true if given string ends with string str, and returns false if given string do not ends with string str String s=“Hello”; System.out.print(s.endsWith(“elo”)); will print true System.out.print(s.startsWith(“HH”)); will print false • Boolean equals(String str)- returns true if all characters of given string is equal to all characters of str String s1=“Hello”; String s2=“Hello”; System.out.print(s1. equals(s2)); will print true
  • 8.
    • Boolean equalsIgnoreCase(Stringstr)- returns true if all characters of given string is equal to all characters of str by ignoring the case String s1=“Hello”; String s2=“HELLO”; System.out.print(s1. equalsIgnoreCase(s2)); will print true • String replace(char oldCharacter, char newCharacter)- replace the all occurrence of oldCharacter with newCharacter String s1=“Hello”; System.out.print(s1. replace(‘l’,’$’)); will print He$$o To update s1 we have to assign it: s1=s1. replace(‘l’,’$’);
  • 9.
    • Boolean equalsIgnoreCase(Stringstr)- returns true if all characters of given string is equal to all characters of str by ignoring the case String s1=“Hello”; String s2=“HELLO”; System.out.print(s1. equalsIgnoreCase(s2)); will print true • String replace(char oldCharacter, char newCharacter)- replace the all occurrence of oldCharacter with newCharacter String s1=“Hello”; System.out.print(s1. replace(‘l’,’$’)); will print He$$o To update s1 we have to assign it: s1=s1. replace(‘l’,’$’);
  • 10.
    • String replace(StringoldString, String newString)- replace the all occurrence of oldString with newString String s1=“Hello”; System.out.print(s1. replace(“ll” , ”%”)); will print He%o To update s1 we have to assign it: s1=s1. replace(“ll” , ”%”); • String substring(int start)- returns substring from ‘start’ index. ‘start’ is included. String s1=“ABCDEF”; System.out.print(s1. substring(3)); will print DEF • String substring(int start,int end)- returns substring from ‘start’ index to ‘end’ indes. ‘start’ is included while ‘end’ is exculded String s1=“ABCDEF”; System.out.print(s1. substring(2,5)); will print CDE
  • 11.
    • char charAt(intindex) – returns the character at given index String s1=“ABCDEFG”; System.out.print(s1.charAt(3)); will print D • int indexOf(char ch)- returns the first index of given character int indexOf(String str)- returns the first index from where given string starts int indexOf(char ch,int start)- returns the first index of given character after the start index. start is included int indexOf(String str,int start)- returns the first index from where given string starts after the start index. start is included String s1=“ABCDEFABCDEF”; System.out.print(s1. indexOf(‘B’)); will print 1 System.out.print(s1. indexOf(‘B’,5)); will print 7
  • 12.
    • int lastIndexOf(charch)- returns the last index of given character int lastIndexOf (String str)- returns the last index from where given string starts int lastIndexOf (char ch,int last)- returns the last index of given character before the last index. last is included int lastIndexOf(String str,int last)- returns the last index from where given string starts before the last index. last is included String s1=“ABCDEFABCDEF”; System.out.print(s1. lastIndexOf(‘B’)); will print 7 • Boolean contains(String str)- returns true if string str is present in given string String s1=“ABCDEFABCDEF”; System.out.print(s1. contains(“ABC”)); will print true
  • 13.
    • int compareTo(Stringstr)- compares the string str with given string String s1=“ABC”; String s2=“abc”; System.out.print(s1.compareTo(s2)); will print -32 • int compareToIgnoreCase(String str)- compares the string str with given string by ignoring the case String s1=“ABC”; String s2=“abc”; System.out.print(s1.compareToIgnoreCase(s2)); will print 0 • char[] toCharArray()- converts the string to array of characters String s1=“ABC”; char ch[]=s1.toCharArray(); System.out.print(ch[2]); will print C
  • 14.
    • byte[] toByteArray()-converts the string to array of bytes String s1=“ABC”; byte ch[]=s1.toByteArray(); System.out.print(ch[0]); will print 65 • String[] split(String str)- splits the given string to string array. Splits after str String s1=“Hello World”; String ch[]=s1.split(“ “); System.out.print(ch[0]); will print Hello