14
String Functions

Learn Java Part 6

Embed Size (px)

Citation preview

Page 1: Learn Java Part 6

String Functions

Page 2: Learn Java Part 6

• String concat(String str)- Concat string str to given string, and returns the concatedstring.

String s=“Hello”;s.concat(“ Java”); //s will not be updatedSystem.out.print(s);will print Hello

System.out.print(s.concat(“ Java”));will print Hello Java

s=s.concat(“ Java”); //now s will be updatedSystem.out.print(s);will print Hello Java

Page 3: Learn Java Part 6

• 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 endString s=“ hello ”;s.trim(); //s will not be affectedSystem.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 affectedSystem.out.print(s);will print: Hello (Without spaces)

Page 4: Learn Java Part 6

• String toUpperCase()- returns the string with all characters in Upper Case

String s=“Hello”;s.toUpperCase(); //s will not be affectedSystem.out.print(s);will print Hello

System.out.print(s.toUpperCase());will print HELLO

s=s.toUpperCase(); //now s will be affectedSystem.out.print(s);will print HELLO

Page 5: Learn Java Part 6

• String toLowerCase()- returns the string with all characters in Lower Case

String s=“Hello”;s.toLowerCase(); //s will not be affectedSystem.out.print(s);will print Hello

System.out.print(s.toLowerCase());will print hello

s=s.toLowerCase(); //now s will be affectedSystem.out.print(s);will print hello

Page 6: Learn Java Part 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, andreturns 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

Page 7: Learn Java Part 6

• Boolean endsWith(String str)- returns true if given string ends with string str, andreturns 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 allcharacters of str

String s1=“Hello”;String s2=“Hello”;System.out.print(s1. equals(s2));will print true

Page 8: Learn Java Part 6

• Boolean equalsIgnoreCase(String str)- returns true if all characters of given string isequal 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 ofoldCharacter 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’,’$’);

Page 9: Learn Java Part 6

• Boolean equalsIgnoreCase(String str)- returns true if all characters of given string isequal 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 ofoldCharacter 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’,’$’);

Page 10: Learn Java Part 6

• String replace(String oldString, String newString)- replace the all occurrence of oldStringwith 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 exculdedString s1=“ABCDEF”;System.out.print(s1. substring(2,5));will print CDE

Page 11: Learn Java Part 6

• 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 1System.out.print(s1. indexOf(‘B’,5));will print 7

Page 12: Learn Java Part 6

• 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 stringString s1=“ABCDEFABCDEF”;System.out.print(s1. contains(“ABC”));will print true

Page 13: Learn Java Part 6

• int compareTo(String str)- compares the string str with given stringString 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 byignoring 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 charactersString s1=“ABC”;char ch[]=s1.toCharArray();System.out.print(ch[2]);will print C

Page 14: Learn Java Part 6

• byte[] toByteArray()- converts the string to array of bytesString 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 strString s1=“Hello World”;String ch[]=s1.split(“ “);System.out.print(ch[0]);will print Hello