0% found this document useful (0 votes)
1 views28 pages

String Function

The document provides an overview of various string functions in Java, including string concatenation, length, indexOf, lastIndexOf, equals, and more. Each function is explained with sample code demonstrating its usage and output. Additionally, it covers StringBuffer methods for manipulating strings, such as append, insert, and reverse.

Uploaded by

Tamil Azhagan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views28 pages

String Function

The document provides an overview of various string functions in Java, including string concatenation, length, indexOf, lastIndexOf, equals, and more. Each function is explained with sample code demonstrating its usage and output. Additionally, it covers StringBuffer methods for manipulating strings, such as append, insert, and reverse.

Uploaded by

Tamil Azhagan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 28

String Function:-

String Concatenation:-

In java, string concatenation forms a new string that is the combination of


multiple strings. There are two ways to concat string in java:

1.By + (string concatenation) operator.

2.By concat() method.

Program:1 (Program for String Concatenation):-

class string1

public static void main(String args[])

String str1="Neil Armstrong ";

String str2="was the first man";

String str3="to step on the moon";

String str=str1+" "+str2+" "+str3;

System.out.println(str);

}}

Compile:

F:\jdk\bin>javac string1.java

F:\jdk\bin>java string1

Output:-

Neil Armstrong was the first man to step on the moon

Program:2 (Program for String Concatenatioin using inbuild function):-

class string2
{

public static void main(String args[])

String s1="Sachin ";

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.

Program: 3 (Program for String length):-

class string3

public static void main(String args[])

String wish="Happy Birthday";

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

public static void main(String args[])

String message="Welcome to java";

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.

Program: 5 (Program for last index of character in string):-

class string5
{

public static void main(String args[])

String message="Welcome to java";

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.

Program: 6 (Program for index of string):-

class string6

public static void main(String args[])

{
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):-

This method returns the character at the index i.

Program: 7 (Program for character at ith value ):-

class string7

public static void main(String args[])

String message="Welcome to java";


System.out.println(message.charAt(11));

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.

Program: 8 (Program for equals of strings):-

class string8

public static void main(String args[])

String wish="Happy day";

String wish1="Happy day";


System.out.println(wish.equals(wish1));

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

Program: 9 (Program for equal in string and ignore case):-

class string9

public static void main(String args[])

String wish="Happy day";

String wish1="Happy day";

String wish2="happy day";

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.

Program: 10 (Program for starts with string):-

class string10

public static void main(String args[])

String sample="Working with Strings";

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.

Program: 11 (Program for ends with string);-

class string11

public static void main(String args[])

String sample="Working with Strings";

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.

Program: 12 (Program for substring):-

class string12

public static void main(String args[])

String str="Welcome to java";

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.

Program: 13 (Program for substring of ith and jth position):-

class string13

public static void main(String args[])

String str="welcome to java";

System.out.println(str.substring(3,12));

Compile:

F:\jdk\bin>javac string13.java

F:\jdk\bin>java string13

Output:-

come to j
toLowerCase():-

this method is used to change a string in uppercase to lowercase.

Program: 14 (Program for display to lower case of string):-

class string14

public static void main(String args[])

String str="JAVA PROGRAM";

System.out.println(str.toLowerCase());

Compile:

F:\jdk\bin>javac string14.java

F:\jdk\bin>java string14

Output:-

java program
toupperCase()

this method changes a lowercase string to its upper case equivalent.

Program: 15 (Program for display to upper case of string):-

class string15

public static void main(String args[])

String str="welcome to home";

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.

Program: 16 (Program for replace the character in string):-

class string16

public static void main(String args[])

String str="welcome to java";

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.

Program: 17 (Program for display the character at ith position):-

class string17

public static void main(String args[])

StringBuffer str_buff=new StringBuffer("Hello World");

System.out.println(str_buff.charAt(6));

Compile:

F:\jdk\bin>javac string17.java

F:\jdk\bin>java string17

Output:-

Append(ch):-

The append(ch) method is used to append a character ch to the StringBuffer.


Program: 18 (Program for append the character):-

class string18

public static void main(String args[])

StringBuffer name=new StringBuffer("Raja");

name.append('j');

System.out.println("The append string is "+name);

Compile:

F:\jdk\bin>javac string18.java

F:\jdk\bin>java string18

Output:-

The append string is Rajaj

Append(str)

This method appends the string str to the StringBuffer.

Program: 19 (Program for append the string):-


class string19

public static void main(String args[])

StringBuffer name=new StringBuffer("Rajav");

name.append("Gandhi");

System.out.println("The append string is "+name);

Compile:

F:\jdk\bin>javac string19.java

F:\jdk\bin>java string19

Output:-

The append string is RajavGandhi

insert(I, ch):-

The method is used to insert a character ch at the index position i.

Program: 20 (Program for inserting character with ith position):-

class string20

{
public static void main(String args[])

StringBuffer sample=new StringBuffer("Jva");

sample.insert(1,'a');

System.out.println(sample);

Compile:

F:\jdk\bin>javac string20.java

F:\jdk\bin>java string20

Output:-

Java

Insert (i, str):-

This method insert the string str at the index position i.

Program: 21 (Program for inserting string with ith position):-

class string21

public static void main(String args[])


{

StringBuffer sample=new StringBuffer("java");

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():-

this method reverse the character sequence of a String Buffer object.

Program: 22 (Program for reverse the string):-

class string22

public static void main(String args[])

{
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.

Program: 23 (Program for set character at ith position):-

class string23

public static void main(String args[])

StringBuffer sample=new StringBuffer("Babbage");


sample.setCharAt(0,'C');

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.

Program: 24 (Program for set length() ):-

class string24

public static void main(String args[])

StringBuffer test=new StringBuffer();

System.out.println("The length of test is"+test.length());

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:-

The length of test is0

The New length of test is 30

equal() versus ==

it is important to understand that the equal() method and the == operator


perform. Two different operations as just explained the equals() method
compares the character insides a string object. The == operator compares two
object references to see if they refer to the same instance.

Program: 25 (Program for equals versus = =)

class string25

public static void main(String args[])

String s1="Hello";

String s2=new String(s1);


System.out.println(s1+" equals "+s2+" ->"+s1.equals(s2));

System.out.println(s1+" == "+s2+" -> "+(s1==s2));

Compile:

F:\jdk\bin>javac string25.java

F:\jdk\bin>java string25

Output:

Hello equals Hello ->true

Hello == Hello -> false

Program: 25 (Program for equals )

class string25

public static void main(String args[])

String s1 = "javatpoint";

String s2 = "javatpoint";

String s3 = "Javatpoint";

System.out.println(s1.equals(s2)); // True because content is same

if (s1.equals(s3))

System.out.println("both strings are equal");

else
System.out.println("both strings are unequal");

Compile:

F:\jdk\bin>javac string25.java

F:\jdk\bin>java string25

Output:

true

both strings are equal

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.

Program: 26 (Program for compareTo() )

class string26

static String arg[]={"Now"," if "," the "," time "," for "," all "," good "," men ","
to "," come "," to "," the "," aid "," of "," their "," country "};

public static void main(String args[])

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.

Program: 27 (Program for trim() to reduce the whitespace)

class string27

public static void main(String args[])

String s1=" Welcome to java ";

System.out.println(s1.trim());

Compile:

F:\jdk\bin>javac string27.java

F:\jdk\bin>java string27

Output:

Welcome to java

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy