0% found this document useful (0 votes)
13 views33 pages

String

Uploaded by

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

String

Uploaded by

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

String Manipulation in Java

DATA ITEMS

ALPHANUMERI
NUMERIC C
Java deals with alphanumeric
data under two categories
1. Characters (defined as a letter, a digit or any
special symbol enclosed within single quotes. Example – ‘A’, ‘t’, ‘9’,
‘*’ etc.

2. Strings(a set of characters enclosed within double


quotes.
Example – “2312”, “HRA”, “12-MIG” etc.
Assigning character literals to a variable-
Syntax- <Character data type> var = <Character literal>
char ch = ‘A’;
char ch1 = ‘&’;
Assigning string literals to a variable-
Syntax - <String data type> var = <String Literal>
String st = “computer”;
String st1 = “Flat no 22”;
Java also provide some built-in functions
to handle characters and strings.
ASCII Code for characters
Character Functions
1) Character.isLetter()
SYNTAX:
boolean variable = Character.isLetter(character)
boolean p = Character.isLetter(‘c’); (return true or false)
2) Character.isDigit()
SYNTAX:
boolean variable = Character.isDigit(character)
boolean p = Character.isDigit(‘c’); (return true or false)
3) Character.isLetterOrDigit()
SYNTAX:
boolean variable = Character.isLetterOrDigit(character)
boolean p = Character.isLetterOrDigit(‘*’); (return true or false)
4) Character.is Whitespace()
This function can be used to check for an existing blank or gap in a
String. It will return a boolean type value if the given argument is a
white space (blank) and, false otherwise.
SYNTAX:
boolean variable = Character.isWhitespace(character)
boolean p = Character.isWhitespace(‘ ’); (return true or false)
5) Character.isUpperCase()
SYNTAX:
boolean variable = Character.isUpperCase(character)
boolean p = Character.isLetter(‘c’); (return true or false)

6) Character.isLowerCase()
SYNTAX:
boolean variable = Character.isLowerCase(character)
boolean p = Character.isLetter(‘c’); (return true or false)
7) Character.toUpperCase()
SYNTAX:
char variable = Character.toUpperCase(character)
char c = Character.toUpperCase(‘a’)
c=A
char c = Character.toUpperCase(‘B’)
c=B

8) Character.toLowerCase()
SYNTAX:
char variable = Character.toLowerCase(character)
char c = Character.toLowerCase(‘a’)
c=a
char c = Character.toLowerCase(‘B’)
c=b
*****STRING FUNCTIONS***********
1. length()
Syntax : int variable = string variable.length();
String str = “COMPUTER”;
int k= str.length();
SOP(k); output = 8

String str = “ COMPUTER”;


int k= str.length();
SOP(k); output = 9

String str = “COM PUTER”;


int k= str.length();
SOP(k); output = 9

String str = “COMPUTER ”;


int k= str.length();
SOP(k); output = 9

NOTE – Indexing - Position of characters start from 0 th to str.length()-1


In “COMPUTER” C is at 0th position
O is at 1st position
:
:
R is at 7th position
2. charAt()
Syntax : char variable = string variable.charAt(index);
String s = “computer”;
Char c = s.charAt(3);
SOP(c); output ‘p’
Char c = s.charAt(7);
SOP(c); output ‘r’
Char c = s.charAt(8);
SOP(c);
No compilation error.
But during execution error will come
Java.lang.StringIndexOutOfBoundExceptions:
String index out of range :1(in java.lang.String)
3. indexOf()
The function returns the index (i.e position number) of a
character available in the string.
Syntax : String st = “UNDERSTANDING”;
Int p = st.indexOf(‘N’);
Output = 1; by default it returns the occurrence of the first
position of a character available in the string.
String st = “MALAYALAM”;
Int p = st.indexOf(‘A’, 4); output = 5
This function will return the index of ‘A’ available in the
string starting from 4th index onwards.
String st = “COMPUTER”;
Int p = st.indexOf(‘C’, 3);
Not available returns -1.
4. lastIndexOf()

It is applied to find the index of the last


occurrence of a character in a String.
String st = “MALAYALAM”;
Int n = st.lastIndexOf(‘A’);
Output = 7
Character is not there then return -1
5. substring()
This function is used to extract a part of string .
Syntax : String variable = string variable .substring(index);

String st = “COMPUTER”;
String p = st.substring(3);
Output = PUTER
String p = st.substring(3,6)//from 3rd to just before 6th
Output : PUT
6) toLowerCase()
This function returns the letters of the string in
lower case. If any character is already in lower
case or the character is a special character then it
returns the same.
Syntax : String variable1 = string
variable.toLowerCase();
String s = “COMPUTER”;
String p = s.toLowerCase();
Result = computer
7) toUpperCase
This function returns the letter after converting
them in upper case. If any character is already in
upper case or the character is a special case
character then it remains the same.
Syntax : String variable1 = string
variable.toUpperCase();
String s = “computer”;
String p = s.toUpperCase();
Result = COMPUTER
8) replace()
This function is used to replace a character by another character or to replace a String by another
String at all its occurrences in the given String.
Syntax :
String <variable 1> = String<variable>.replace<character to replace>,<character to appear>
String<variable 1> = String<variable>.replace<substring to replace>,<sub string to appear>
String s = “MXLXYXLXM”;
String p = s.replace(‘X’,’A’);
Result “MALAYALAM”
String s = “The green bottle is in green bag”
String p = s.replace(“green”, “red”);
Result “The red bottle is in red bag”
String s = “This is a ball”;
String p = s.replace(“is”, “was”);
Result “Thwas was a ball”
9) concat()
This function is applied to concatenate (join) two strings together.
Syntax : String variable = string variable1.concat(string variable2);
String x = “COMPUTER”
String y = “APPLICATIONS”
String z = x.concat(y);
SOP(z);
Result “COMPUTERAPPLICATIONS”
Java also uses an operator + to concatenate two or more strings
together.
String z = x + ‘ ‘ + y
SOP(z);
Result “COMPUTER APPLICATIONS”
10) equals()
This function is used to compare two strings together to check whether they are identical
or not. It returns a boolean type value if both are same, false otherwise.
SYNTAX:
boolean variable = string variable1.equals(string variable2);
Example
String x = “COMPUTER”
String y = “SCIENCE”
boolean z = x.equals(y);
Example
String x = “COMPUTER”
String y = “computer”
If(x.equals(y))
System.out.print(“Same”);
else
System.out.print(“Different”);
Output – “Different” (As Java is case sensitive)
11) equalsIgnoreCase()
SYNTAX:
boolean variable = string
variable1.equalsIgnoreCase(string variable2);
String x = “COMPUTER”
String y = “computer”
boolean p = x.equals(y)
it will return true
// A program to display the pattern
import java.util.*;
public class p1
{
public static void main()
{
Scanner sc = new Scanner(System.in);
int a, b;
String st;
System.out.println(“ENTER YOUR WORD TO PRINT A PATTERN”);
st = sc.next();
b = st.length();
System.out.println(“The pattern”);
for(a=0;a<b;a++)
{
System.out.println(st.charAt(a));
}
}
}
Write a program to display the given pattern
C
CO
COM
COMP
COMPU
COMPUT
COMPUTE
COMPUTER
12) compareTo()
It compares two strings. Apart from checking equality it also checks whether a string is
bigger or smaller than the other.
SYNTAX:
Int variable = string var1.compareTo(String var2)
1) String x = “COMPUTER”
String y = “SCIENCE”
Int v = x.compareTo(y)
Return ASCII of ‘C’ – ASCII od ‘S’= 67-83 = -16
2) String x = “COMPUTER”
String y = “COncat”
Return ASCII of ‘M’ – ASCII of ‘n’ = 77-110 = -33
If v = 0 then x and y both the strings are same
If v>0 then string x is greater than string y.
If v<0 then string x is less than string y
Here laxographic comparison (searching according to dictionary) of the strings take place. It
takes the difference of ASCII codes of the corresponding characters where they differ at the
first occurrence. If it is same throughout the strings then declared same and will return 0.
13) trim()
This function is used to remove leading and trailing
blanks from the string. Other blanks which are available
in between the words will remain unchanged.
SYNTAX:
String var1 = String variable.trim();
String s = “COMPUTER “
String p = s.trim();
System.out.println(s.length());
System.out.println(p.length());
14) endsWith()
This function is used to check whether a given
string has a specified suffix or not. It returns a
boolean type value true or false accordingly.
SYNTAX:
boolean variable = string variable1.
endswith(string variable2)
String p = “COMPUTER IS FUN”
String b = “FUN”;
boolean x = p.endswith(b)
it will return true.
15) startsWith()
This function returns a boolean type value true if a given
string is used as the prefix to another string, false otherwise.
SYNTAX:
boolean variable = string variable1.startswith(string variable
2);
String p = “COMPUTER IS FUN”
String b = “YOUR”
Boolean x = p.startswith(b)
It will return false.
Write a program to display the given pattern
COMPUTER
COMPUTE
COMPUT
COMPU
COMP
COM
CO
C

You might also like