0% found this document useful (0 votes)
5 views3 pages

String Handling

The document provides an overview of string handling in Java, detailing the String class, its declaration, initialization, and various functions for manipulating strings. It includes examples of methods such as trim(), toLowerCase(), toUpperCase(), length(), and others, explaining their purposes and providing code snippets. Additionally, it covers comparisons, concatenation, and conversions related to strings.

Uploaded by

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

String Handling

The document provides an overview of string handling in Java, detailing the String class, its declaration, initialization, and various functions for manipulating strings. It includes examples of methods such as trim(), toLowerCase(), toUpperCase(), length(), and others, explaining their purposes and providing code snippets. Additionally, it covers comparisons, concatenation, and conversions related to strings.

Uploaded by

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

String Handling

String class:- String is a composite datatype which is used to store one or more than
one character enclosed in double quotes(“ “).
String is a pre-defined class present in java.lang package.
Declaration of String:-
1. String a= new String();
2. String b;
Initialization of String :-
1. String a= new String(“Computer Application”);
2. String b=”Computer Application”;
Functions of Scanner class used for taking String as Input:-
1. next()- to input single word(without space);
2. nextLine()- to input one or more than one word.

FUNCTION NAME PURPOSE EXAMPLE

String s= “ Old is Gold “;


This function is used to remove spaces
String trim() s=s.trim();
from the beginning and end of the String.
now s=”Old is Gold”;
This function coverts all the String s=”LuCKnow CitY”;
String toLowerCase() characters(letters) of the String in lower s=s.toLowerCase();
case. now s=”lucknow city”;
This function coverts all the String s=”LuCKnow CitY”;
String toUpperCase() characters(letters) of the String in upper s=s.toUpperCase();
case. now s=”LUCKNOW CITY”;
String s=”Be Good”;
This function is used to count number of
int length() int l=s.length();
characters present in the given String
now l=7
String s=”Better be late than
This function is used to extract the never”;
char charAt(int n)
character present at nth position char ch=s.charAt(5);
now ch=’r’;
This function is used to provide the first
String s=”Better be late than
occurrence/ position of the ch present in
never”;
int indexOf(char ch) the String.
int p=s.indexOf(‘e’);
Note:-If ch is not present in given String
now p=1;
then this function returns -1
This function is used to provide the last String s=”Better be late than
occurrence/ position of the ch present in never”;
int lastIndexOf(char ch)
the String. int p=s.lastIndexOf(‘e’);
Note:-If ch is not present in given String now p=23;
then this function returns -1

String
This function is used to join one String to s1=”Computer”,s2=”Application”;
String concat(String str)
other. String r=s1.concat(s2);
r=”ComputerApplication”;
This function is used to check whether two
String
Strings are equal (same) or not. This
s1=”Computer”,s2=”computer”;
boolean equals(String str) function is case sensitive i.e if alphabets
boolean r=s1.equals(s2);
are same in both String but their cases
r=false;
differ then this function will return false.
This function is used to check whether two
Strings are equal(same) or not. This
String
function is not case sensitive i.e if
boolean s1=”Computer”,s2=”computer”;
alphabets are same in both String but
equalsIgnoreCase(String str) boolean r=s1.equalsIgnoreCase(s2);
their cases differ then this function will
r=true;
return true.
Note-This function is not case sensitive.
1. String
s1=”Computer”,s2=”comput
er”;
This function compares two String
int r= s1.compareTo(s2);
lexicographically.
r= -32;
Both String same gives 0
2. String
First String if gives
s1=”Compute”,s2=”Comput
greater(Alphabetical positive
er”;
order or value
int r= s1.compareTo(s2);
int compareTo(String str) lexicographically)than
r= -1 ;
second
3. String
First String if gives
s1=”Computer”,s2=”Compu
smaller(Alphabetical negative
order or value te”;
lexicographically)than
int r= s1.compareTo(s2);
second
r= 1 ;

This function compares two String 1. String


lexicographically. s1=”Computer”,s2=”comput
Both String same gives 0 er”;
First String if gives int r= s1. compareToIgnoreCase
greater(Alphabetical positive s2);
int order or value r= 0;
compareToIgnoreCase(String lexicographically)than 2. String
str) second s1=”Compute”,s2=”Comput
First String if gives er”;
smaller(Alphabetical negative int r= s1. compareToIgnoreCase
order or value (s2);
lexicographically)than r= -1 ;
second 3. String
Note-This function is not case sensitive. s1=”Computer”,s2=”Compu
te”;

int r= s1. compareToIgnoreCase


(s2);
r= 1 ;

String s=”Better be late than


never”;
String replace(char oldch,char This function replaces all the oldch with
String r=s.replace(‘e,’s’);
newch) newch of the String
now r= Bsttsr bs lats than nsvsr”;

String s=”Better be late than


This function is used to provide a sub-part
never”;
String substring(int start) of the given String from start position to
String r=s.substring(4);
the last character of the String
r=”er be late than never”;
This function is used to provide a sub-part String s=”Better be late than
String substring(int start, int of the given String from start position to never”;
end) the (end-1)th position character of the String r=s.substring(4,8);
String r=”er b”;
String s=”Better be late than
never”;
This function is used to check whether the boolean r=s.startsWith(“Be”);
boolean startsWith(String str)
given String begins with str or not. r=true;

String s=”Better be late than


never”;
boolean r=s.endsWith(“NEVER”);
boolean x=s.endsWith(“never”);
This function is used to check whether the
boolean endsWith(String str) Now,
given String ends with str or not.
r=false;
x=true;

int a=7832;
this function is used to convert any value
String valueOf(AnyDatatype) String s=String.valueOf(a);
of any datatype into String
now s=”7832”;

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