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