INSPIREWEBSOFT Home
Basics Terminologies in
Java String Functions in Java
Input by the User in Java
Operators in Java ☞These functions deal only with string manipulations.
Mathematical Functions ☞Some of them are :
in Java
1. length()
Decision Control 2. charAt()
Statement in Java 3. indexOf()
Iterative Statement in 4. lastIndexOf()
5. substring()
Java
6. toUpperCase()
Jump Statement in Java 7. toLowerCase()
8. replace()
switch-case Statement in
9. trim()
Java [Link]()
Method/Function in Java [Link]()
Constructor in Java
Array in Java length()
String in Java ☞It is used to return the length of a string i.e. number of characters in the string.
☞Function prototype : int length()
Inheritance/Encapsulation in
Java ☞It returns a value of int type.
☞Syntax : <string or string_variable>.length();
Example
public class StringExample {
public static void main(String[] args) {
String s1 = "inspirewebsoft";
[Link]("_inspirewebsoft_".length());
[Link]([Link]());
}
}
Output
16
14
charAt()
☞It is used to return a character from the given index of the string.
☞Function prototype : char charAt(int index)
☞It returns a value of char type.
☞Syntax : <string or string_variable>.charAt(index_number);
Example
public class StringExample {
public static void main(String[] args) {
String s1 = "inspirewebsoft";
[Link]("_inspirewebsoft_".charAt(15));
[Link]([Link](6));
}
}
Output
_
e
indexOf(char ch)
☞It is used to return the length of a string i.e. number of characters in the string.
☞Function prototype : int indexOf(char ch) or int indexOf(char ch, int
begin_index)
☞It returns a value of int type.
☞Syntax : <string or string_variable>.indexOf(character,begin_index);
Example
public class StringExample {
public static void main(String[] args) {
String s1 = "inspirewebsoft";
[Link]("_inspirewebsoft_".indexOf('_'));
[Link]([Link]('e'));
[Link]([Link]('e',7));
}
}
Output
0
6
8
lastIndexOf()
☞It is used to return the index of the last occurence of a character in the string.
☞Function prototype : int lastIndexOf(char ch)
☞It returns a value of int type.
☞Syntax : <string or string_variable>.lastIndexOf(character);
Example
public class StringExample {
public static void main(String[] args) {
String s1 = "inspirewebsoft";
[Link]("_inspirewebsoft_".lastIndexOf('_'));
[Link]([Link]('e'));
}
}
Output
15
8
substring()
☞Part of a string is known as substring.
☞It is used to extract a set of characters simultaneously from a given index up to the
end of string or to the index number specified.
☞Function prototype : String substring(int begin_index) or String substring(int
begin_index, int last_index)
☞It returns a value of string type.
☞Syntax : <string or string_variable>.substring(begin_index); or <string or
string_variable>.substring(begin_index,last_index);
Example
public class StringExample {
public static void main(String[] args) {
String s1 = "inspirewebsoft";
[Link]("_inspirewebsoft_".substring(7));
[Link]([Link](4));
[Link]([Link](4,10));
}
}
Output
ewebsoft_
irewebsoft
ireweb
Note : begin_index is inclusive whereas last_index is exclusive
toLowerCase()
☞It is used to convert a given string into lowercase characters.
☞Function prototype : String toLowerCase()
☞It returns a value of String type.
☞Syntax : <string or string_variable>.toLowerCase();
Example
public class StringExample {
public static void main(String[] args) {
String s1 = "inspirewebSOFT";
[Link]("_inspirewebsoft_".toLowerCase());
[Link]([Link]());
}
}
Output
_inspirewebsoft_
inspirewebsoft
Note : If character is already in lower case or is a special character, then it will remain
same.
toUpperCase()
☞It is used to convert a given string into uppercase characters.
☞Function prototype : String toUpperCase()
☞It returns a value of String type.
☞Syntax : <string or string_variable>.toUpperCase();
Example
public class StringExample {
public static void main(String[] args) {
String s1 = "inspirewebSOFT";
[Link]("_inspirewebsoft_".toUpperCase());
[Link]([Link]());
}
}
Output
_INSPIREWEBSOFT_
INSPIREWEBSOFT
Note : If character is already in upper case or is a special character, then it will remain
same.
replace()
☞It is used to replace a character by another character or to replace a String by
another String at all its occurences in the given string.
☞Function prototype : String replace(char old_char, char new_char) or String
replace(String old_string, String new_string)
☞It returns a value of String type.
☞Syntax : <string or string_variable>.replace(old_character, new_character); or
<string or string_variable>.replace(old_string, new_string);
Example
public class StringExample {
public static void main(String[] args) {
String s1 = "inspirewebSOFT";
[Link]("_inspirewebsoft_".replace('e','*'));
[Link]([Link]("SOFT","soft"));
}
}
Output
_inspir*w*bsoft_
inspirewebsoft
trim()
☞It is used to remove leading and trailing blanks from a string.
☞Function prototype : String trim()
☞It returns a value of String type.
☞Syntax : <string or string_variable>.trim();
Example
public class StringExample {
public static void main(String[] args) {
String s1 = " We develop a passion for learning ";
[Link](" inspirewebsoft ".trim());
[Link]([Link]());
}
}
Output
inspirewebsoft
We develop a passion for learning
Note : The other blanks, which are available in between the words will remain
unchanged.
endsWith()
☞It is used to check whether a given string has a specified suffix or not.
☞Function prototype : boolean endsWith(String s)
☞It returns a value of boolean type.
☞Syntax : <string or string_variable>.endsWith(string);
Example
public class StringExample {
public static void main(String[] args) {
String s1 = " We develop a passion for learning ";
[Link]("inspirewebsoft".endsWith("soft"));
[Link]([Link]("We"));
}
}
Output
true
false
startsWith()
☞It is used to check whether a given string has a specified prefix or not.
☞Function prototype : boolean startsWith(String s)
☞It returns a value of boolean type.
☞Syntax : <string or string_variable>.startsWith(string);
Example
public class StringExample {
public static void main(String[] args) {
String s1 = " We develop a passion for learning ";
[Link]("inspirewebsoft".startsWith("soft"));
[Link]([Link]("We"));
}
}
Output
false
true
All Content © Copyright. All Rights Reserved. Developed By inspirewebsoft