String Function
String Function
String Concatenation:-
class string1
System.out.println(str);
}}
Compile:
F:\jdk\bin>javac string1.java
F:\jdk\bin>java string1
Output:-
class string2
{
String s2="Tendulkar";
String s3=s1.concat(s2);
System.out.println(s3)
Compile:
F:\jdk\bin>javac string2.java
F:\jdk\bin>java string2
Output:-
Sachin Tendulkar
length ():-
As the name indicate the length () method is used to find the length of a string.
This method returns as integer value representing the number of characters in
the string including spaces note that this method does not need only
arguments.
class string3
System.out.println(wish.length());
Compile:
F:\jdk\bin>javac string3.java
F:\jdk\bin>java string3
Output:-
14
indexOf (ch):-
This method is used to search a string for a particular character and returns an
integer representing the first occurrence of the character ch in the string. It
returns -1 if the character is not found in the string.
Program: 4 (Program for index of character in string):-
class string4
System.out.println(message.indexOf('j'));
System.out.println(message.indexOf('u'));
Compile:
F:\jdk\bin>javac string4.java
F:\jdk\bin>java string4
Output:-
11
-1
lastIndexOf (ch):-
This method is similar to indexOf method. But in this case if returns the index
of the last occurrence of the character ch in the string. lastIndexOf also return -
1 the character is not found in the string.
class string5
{
System.out.println(message.indexOf('a'));
System.out.println(message.lastIndexOf('a'));
Compile:
F:\jdk\bin>javac string5.java
F:\jdk\bin>java string5
Output:-
12
14
indexOf (str):-
This method searches for the string. str in the current object and returns the
index value of the first character. It returns -1 if it cannot find str in the string.
class string6
{
String message="Welcome to java";
System.out.println(message.indexOf("java"));
Compile:
F:\jdk\bin>javac string6.java
F:\jdk\bin>java string6
Output:-
11
charAt (i):-
class string7
Compile:
F:\jdk\bin>javac string7.java
F:\jdk\bin>java string7
Output:-
Equals (str):-
This method compares the string str with the current object. It returns true if
they are the same (that is they have the same sequence of character) and false
if they are not.
class string8
Compile:
F:\jdk\bin>javac string8.java
F:\jdk\bin>java string8
Output:-
true
equalsIgnoreCase (str):-
This method is very similar to the equals() method. But as the name suggest it
ignores the cases of the characters
class string9
System.out.println(wish.equalsIgnoreCase(wish1));
System.out.println(wish.equalsIgnoreCase(wish2));
}
Compile:
F:\jdk\bin>javac string9.java
F:\jdk\bin>java string9
Output:-
true
true
startsWith(str):-
this method check if the current object starts with the same sequence of
characters as the string str and return true if it does.
class string10
System.out.println(sample.startsWith("Working"));
Compile:
F:\jdk\bin>javac string10.java
F:\jdk\bin>java string10
Output:-
True
endsWith(str):-
Here, a value true is returned if the current object ends with the string str.
class string11
System.out.println(sample.endsWith("Strings"));
Compile:
F:\jdk\bin>javac string11.java
F:\jdk\bin>java string11
Output:-
True
Substring ():-
This method extracts a substring containing all the characters from the current
object starting from index I till the end.
class string12
System.out.println(str.substring(11));
Compile:
F:\jdk\bin>javac string12.java
F:\jdk\bin>java string12
Output:-
java
Substring (i, j):-
This method extracts a substring starting from the index position I up till the
index position j (not including the character in the j th position.
class string13
System.out.println(str.substring(3,12));
Compile:
F:\jdk\bin>javac string13.java
F:\jdk\bin>java string13
Output:-
come to j
toLowerCase():-
class string14
System.out.println(str.toLowerCase());
Compile:
F:\jdk\bin>javac string14.java
F:\jdk\bin>java string14
Output:-
java program
toupperCase()
class string15
System.out.println(str.toUpperCase());
Compile:
F:\jdk\bin>javac string15.java
F:\jdk\bin>java string15
Output:-
WELCOME TO HOME
replace(ch1,ch2):-
this method is used to replace all occurrence of character ch1 with another
character ch2 in the current object.
class string16
System.out.println(str.replace('a', 'A'));
Compile:
F:\jdk\bin>javac string16.java
F:\jdk\bin>java string16
Output:-
welcome to jAvA
charAt(i)
this method is like the charAt() used with strings. It returns the character with
the index i.
class string17
System.out.println(str_buff.charAt(6));
Compile:
F:\jdk\bin>javac string17.java
F:\jdk\bin>java string17
Output:-
Append(ch):-
class string18
name.append('j');
Compile:
F:\jdk\bin>javac string18.java
F:\jdk\bin>java string18
Output:-
Append(str)
name.append("Gandhi");
Compile:
F:\jdk\bin>javac string19.java
F:\jdk\bin>java string19
Output:-
insert(I, ch):-
class string20
{
public static void main(String args[])
sample.insert(1,'a');
System.out.println(sample);
Compile:
F:\jdk\bin>javac string20.java
F:\jdk\bin>java string20
Output:-
Java
class string21
sample.insert(0,"Welcome to ");
System.out.println(sample);
Compile:
F:\jdk\bin>javac string21.java
F:\jdk\bin>java string21
Output:-
Welcome to java
reverse():-
class string22
{
StringBuffer sample=new StringBuffer("Bill Gates");
sample.reverse();
System.out.println(sample);
Compile:
F:\jdk\bin>javac string22.java
F:\jdk\bin>java string22
Output:-
setaG lliB
setcharAt(i, ch):-
this method replace the character at index position I in the String Buffer with
the character ch.
class string23
System.out.println(sample);
Compile:
F:\jdk\bin>javac string23.java
F:\jdk\bin>java string23
Output:-
Cabbage
setLength ()
this method set the capacity of the String Buffer object to len.
class string24
test.setLength(30);
System.out.println("The New length of test is "+test.length());
Compile:
F:\jdk\bin>javac string24.java
F:\jdk\bin>java string24
Output:-
equal() versus ==
class string25
String s1="Hello";
Compile:
F:\jdk\bin>javac string25.java
F:\jdk\bin>java string25
Output:
class string25
String s1 = "javatpoint";
String s2 = "javatpoint";
String s3 = "Javatpoint";
if (s1.equals(s3))
else
System.out.println("both strings are unequal");
Compile:
F:\jdk\bin>javac string25.java
F:\jdk\bin>java string25
Output:
true
compareTO()
often it is not enough to simply know wheatear two strings are identical. For
sorting applications you need to know which is less than or equal to or greater
than the text. A string is less than another if it comes before the other in
dictionary order. A string is greater than another if it comes after the other is
dictionary order.
class string26
static String arg[]={"Now"," if "," the "," time "," for "," all "," good "," men ","
to "," come "," to "," the "," aid "," of "," their "," country "};
for(int j=0;j<arg.length;j++)
{
for(int i=j+1;i<arg.length;i++){
if(arg[i].compareTo(arg[j])<0){
String t=arg[j];
arg[j]=arg[i];
arg[i]=t;
System.out.println(arg[j]); } } }
Compile:
F:\jdk\bin>javac string26.java
F:\jdk\bin>java string26
Output:
Now
aid
all
come
country
for
good
if
men
of
the
the
their
time
to
to
trim()
the trim() method return a copy of the invoking string from which any leading
and trailing whitespace has been removed. It has the general form.
class string27
System.out.println(s1.trim());
Compile:
F:\jdk\bin>javac string27.java
F:\jdk\bin>java string27
Output:
Welcome to java