chapter 8 n.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
Chapter – 8 PowerPoint Presentation
Download Presentation
Chapter – 8

Loading in 2 Seconds...

play fullscreen
1 / 36

Chapter – 8 - PowerPoint PPT Presentation


  • 143 Views
  • Uploaded on

Chapter – 8 . String Manipulation in JAVA. Character Functions 1. Character.isLetter() This function is used to check whether a given argument is an alphabet or not. It returns a boolean type value either true or false. Syntax : boolean variable = Character.isLetter(character);

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'Chapter – 8' - shasta


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
chapter 8

Chapter – 8

String Manipulation in JAVA

slide2

Character Functions

1. Character.isLetter()

This function is used to check whether a given argument is an alphabet or not.

It returns a boolean type value either true or false.

Syntax :

boolean variable = Character.isLetter(character);

e.g. boolean p = Character.isLetter(‘C’);

This function will return true to the variable p.

boolean p = Character.isLetter(‘6’);

2. Character.isDigit()

This function returns a boolean type value true if a given argument is a digit otherwise false.

slide3

Syntax : -

boolean variable = Character.isDigit(character);

e.g. boolean p = Character.isDigit(‘7’);

boolean p = Character.isDigit(‘G’);

Character.isLetterOrDigit()

This function returns true if the given argument is either a letter or a digit, false otherwise.

Syntax:-

boolean variable = Character.isLetterOr Digit(character);

e.g. boolean b = Character.isLetterOr Digit(‘A’);

boolean b = Character.isLetterOr Digit(‘8’);

boolean b = Character.isLetterOr Digit(‘*’);

slide4

3. Character.isWhiteSpace()

This function can be used to check for existing blank or gap in a String.

It will return a boolean type value (i.e. true) if the given argument is a white space (blank) and false otherwise.

Syntax:-

boolean variable = Character.isWhiteSpace(character);

e.g. boolean b = Character.isWhiteSpace(‘ ‘);

boolean b = Character.isWhiteSpace(‘*’);

4. Character.isUpperCase()

This function will return true if the argument is an upper case letter otherwise false.

Syntax:-

slide5

boolean p = Character.isUpperCase(‘A’);

boolean p = Character.usUpperCase(‘g’);

boolean p = Character.toUpperCase(‘?’);

5. Character.toLowerCase()

This function returns the given argument in lower case character. The given character remains same if it is already in lower case. It returns the same character if the character if the character used with the function is non- alphabet.

Syntax:-

Char variable = Character.toLowerCase(character);

Example :- char c = Character.toLowerCase(‘A’);

char c = Character.toLowerCase(‘g’);

char c = Character.toLowerCase(‘*’);

slide6

Conversion from characters to ASCII code and Vice-Versa.

  • Computer deals with 256 characters to perform any task.
  • Each character is specified with a numeric code called ASCII code, which ranges from 0 to 255.
  • Characters ASCII Codes
  • 0-9 48 – 57
  • A- Z 65 – 90
  • a – z 97 – 122
  • Character to ASCII code
  • Syntax: int variable1 = (int) char variable2;
  • e.g. int n = (int) x;
  • It results n as 65.
slide7

Convert ASCII code to character

Syntax: char variable1 = (char) int variable2;

e.g. int i = 98;

char c = (char) i;

It returns b to the variable c.

String Functions

length()

This function is used to return the length of a string(i.e., number of character present in the string).

The length of a string will be integer number . Hence, it returns an integer value.

Syntax: int variable = string variable.length();

e.g. String str = “Computer”;

int k = str.length.

Here, k=8, as number of characters available in the string str is 8.

slide8

charAt()

This function returns a character from the given index (position number) of the string.

Syntax: char variable = string variable.charAt(index);

e.g. String s = “COMPUTER”;

char c = s.charAt(3);

Here, c=‘p’ as the character at 3rd index is P. Please note that the index must not exceed the length of the string.

IndexOf()

This function returns the index (i.e. position ) number of the string.

Syntax:- int variable = string variable.indexOf(character);

e.g. String s = “COMPUTER”;

int n = s.indexOf ‘P’;

Hence the value returned by the function is 3, hence, n=3.

slide9

String s = “MALAYALAM”;

int n=s.indexOf (4, ‘A’);

This function will return the index of ‘A’ available in the string after 4th index. Hence, n=5.

lastIndexOf()

This function is applied to find the index of last occurrence of a character in a String.

Syntax:- int variable = String variable.lastIndexOf(character);

e.g. String s = “MALAYALAM”);

int n = s.lastIndexOf(‘A’);

It returns 7 to the integer variable n. Hence, n=7.

substring()

This function is used to extract a part of the string(i.e., simultaneous characters from one index to another)

Syntax:- String variable1 = string variable.substring(index);

slide10

e.g. String s = “COMPUTER”;

String p = s.substring(3);

The function will return all the character from the string starting from 3rd index. Hence, p = PUTER.

As the return value of the function is string so, you must use a string variable which contains the result.

toLowerCase()

This function returns the character of the string in lower case letters.

If any character is already in lowercase or the character is a special character then it remains same.

Syntax: - String variable1 = string variable.toLowerCase();

e.g. String s = “COMPUTER”;

String p = s.toLowerCase();

Here, p results in “computer”.

slide11

replace()

  • This function is used to replace a character by another character or to replace a String by another String at all its occurrence in the given String.
  • Syntax:-
  • String <variable1> = String<variable>.replace<character to replace>,<character to appear>
  • String <variable1> = String<variable>.replace<sub string to replace>,<sub string to appear>
  • e.g.: String s = “MXLXYXLXM”;
  • String p = s.replace (‘X’, ‘A’);
  • e.g. String s = “The green bottle is in green bag”;
  • String p = s.replace (“green” , “red”);
  • Hence, the new string p results in “The red bottle is in red bag”.
slide12

concat()

This function is applied to concatenate (join) two strings together.

Syntax :- String variable = string variable1.concat(string variable2);

1. String x = “COMPUTER”;

String y = “APPLICATIONS”);

String z = x.concat(y);

We can also use the ‘+’ operator to cancatenate two or more strings together.

e.g., String x = “COMPUTER”;

String y = “APPLICATIONS”;

String z = “SCIENCE”;

String p = x+y;

Here, p results in COMPUTERAPPLICATIONS

String p = x + ‘ ‘ + y + ‘ ‘+z;

Here, p results in COMPUTER APPLICATION SCIENCE.

slide13

equalsIgnoreCase()

This function is also used to compare two strings to ensure whether both are identical or not after ignoring the case (i.e., corresponding upper and lower case characters are treated to be same).

Syntax:- boolean variable = string variable1. equalsIgnoreCase(string variable2)

e.g. String x = “COMPUTER”;

String y = “computer”;

boolean p = x.equalsIgnoreCase(y);

compareTo()

It is also a type of unction which compares two strings.

It not only checks the equality of the strings but also checks whether a string is bigger or smaller than other or not.

Syntax:- int variable = string variable1 . compareTo(string variable2);

This function returns an integer value depending upon the condition.

slide14

e.g. String x = “COMPUTER”;

  • String y = “SCIENCE”;
  • int n = x.compareTo(y);
  • It always compares character by character between the corresponding positions between two strings.
  • The comparisons are based on the order of the corresponding characters of the strings but not on the basis of the length of strings.
  • trim()
  • This function is used to remove the blanks from either side of the string.
  • Other blanks which are available in between the words, remain unchanged.
  • Syntax : String variable1 = String variable.trim();
  • e.g. String s = “COMPUTER APPLICATIONS”;
  • String p = s.trim();
slide15

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 b = string variable1.endswith(string variable2);

e.g. String p = “COMPUTER IS FUN”;

String b = “FUN”;

boolean x = p.endswith (b);

startwith()

This function returns a boolean type value true if a given string is used as prefix to another string, false otherwise.

Syntax: - boolean b = string variable1.startswith(string variable2);

e.g. String p = “COMPUTER IS FUN”;

String b = “YOUR”;

Boolean x = p.startswith(b);

slide16

Conversion from String to Primitive data type

  • - String to integer type data.
  • Syntax :- (a) integer variable = Integer.parseInt(String);
  • (b) integer variable = Integer.valueOf(String);
  • e.g.
  • String s = “24”;
  • int n=Integer.parseInt(s);
  • or
  • int n= Integer.valueOf(s);
  • String to float type data.
  • Syntax :- (a) float variable = Float.parseInt(String);
  • (b) float variable = Float.valueOf(String);
  • e.g. String s = “24.35”;
  • float n = Float.parseFloat(s);
  • Or float n = Float.valueOf(s);
slide17

Conversion from Primitive data type to String

  • - Integer type data to String type.
  • Syntax :- String variable = Integer.toString(int);
  • e.g. int n = 24;
  • String s = Integer.toString(n);
  • Float type data to String type.
  • Syntax :- String variable = Float.toString(float);
  • e.g. float n = 24.35;
  • String s = Float.toString(n);
slide18

String Buffer Functions

  • We can declare an object of String Buffer type.
  • It creates reasonable space in the memory called Buffer to contain a String.
  • To create String Buffer Objects:-
  • StringBuffer m = new StringBuffer();
  • It allocates an empty space in the memory to store a string.
  • StringBuffer m = new StringBuffer(“COMPUTER”);
  • It allocates memory for variable m and initializes it with String “COMPUTER”.
  • StringBuffer m = new StringBuffer(25);
  • It allocates memory for a string upto 25 character.
slide19

Characteristics of an Object of String Buffer Type.

  • It refers specific length in memory to contain characters.
  • The length can be changed according to the need of the programmers. It can be changed using SetLength() function.
  • If String has bigger length than the size of String Buffer object, then the characters exceeding the size are truncated.
  • If a string is smaller than the size of the string buffer object then it contains extra characters as ‘\u000’ (garbage value)
slide20

append()

This function is used to add a string at the end of another string.

Syntax:- StringBuffer variable1.append(stringBuffer variable2);

e.g. StringBuffer k = new StringBuffer(“COMPUTER”);

StringBuffer p = new StringBuffer(“Application”);

k.append(p);

setCharAt()

This function can be applied to replace a character with another character at specified index.

Syntax :- StringBuffer variable.setCharAt(Index, Character);

e.g. StringBuffer s = new StringBuffer(“COMMUTER”);

s.setCharAt(3,’P’);

slide21

insert()

This function allows you to insert a string at specified index into another string.

Syntax:- StringBuffer variable1.insert(index,StringBuffer variable2);

e.g. StringBuffer s = new StringBuffer(“COMPUTER FUN”);

StringBuffer p = new StringBuffer(“IS”);

s.insert(8,p);

delete()

This function is applied to delete the characters from one index to another index in a given string.

Syntax :- StringBuffer variable.delete(index1. index2);

e.g. StringBuffer s = new StringBuffer(“COMPUTER”);

s.delete(3,5);

slide22

setLength()

  • This function allows you to set the length of a string buffer variable up to specified range.
  • If a string to be stored has more than specified length then the characters exceeding the length are truncated otherwise stores “\uoooo” (garbage value) character in the empty.
  • Syntax:- StringBuffer variable.setLength(Number of Characters);
  • e.g. p.setLength(20);
  • The variable p is set to accommodate a string up to the length 20 characters.
  • reverse()
  • This function is used to reverse the characters of a given string.
  • Syntax :- StringBuffer variable.reverse();
  • e.g. StringBuffer p = new StringBuffer(“COMPUTER”);
  • p.reverse();
  • The variable p returns reversed string RETUPMOC.
slide23

Differences between String and String Buffer Objects

  • String String Buffer
  • String type object has fixed 1. String Buffer type object has
  • length. provisions to change the length.
  • 2. New object is created to 2. Change is maintained in the same
  • produce the change object.
  • It is a general approach of 3. It is an advanced approach of
  • programming. Programming.
slide24

Program1: Write a program in JAVA to accept a string and display the number of upper case, number of lower case, number of special characters and number of digits present in the string.

import java.io.*;

public class Program1

{

public static void main(String args[]) throws IOException

{

InputStreamReader read = new InputStreamReader(System.in);

BufferedReader in = new BufferedReader(read);

String name;

int up=0, low=0, digit=0, spl=0, p,i ;

char chr;

System.out.println(“Enter your String”);

name=in.readLine();

p=name.length();

for(i=0; i<p; i++)

{

chr=name.charAt(i);

if(chr >= ‘A’ && chr <= ‘Z’)

slide25

up=up+1;

else if (chr >= ‘a’ && chr <= ‘z’)

low=low+1;

else if (chr >= ‘0’ && chr <=‘9’)

digit = digit+1;

else

spl = spl+1;

}

System.out.println(“No. of uppercase = “+up);

System.out.println(“No. of lowercase = “+low);

System.out.println(“No. of spl. character = “+spl);

System.out.println(“No. of digit = “+digit);

}

}

slide26

Program 2: Write a program to enter an alphabet. Display the new alphabet and its ASCII code after changing the case.

Sample Input: b

Sample Output : B

The ASCII value of B is 98.

import java.io.*;

public class Program2.

{

public static void main(String args[])throws IOException

{

char chr , chr1;

InputStreamReader read = new InputStreamReader(System.in);

BufferedReader in = new BufferedReader(read);

System.out.println(“Enter an Alphabet”);

chr = (Char)in.read();

if(Character .isUpperCase(chr) ==true)

{

chr1=Character.toLowerCase(chr);

System.out.println(“The lowercase of “ +chr + “is” +chr1);

slide27

System.out.println(“The ASCII value of” +chr +”is” + (int)chr1);

}

else

{

chr1 = Character.toUpperCase(chr);

System.out.println(“The uppercase of” +chr + “is” +chr1);

System.out.println(“The ASCII value of “ +chr +”is” +(int)chr1);

}

}

}

slide28

Program 3:- A program to demonstrate input statement.

import java.io.*;

public class Sample

{

public static void main(String args[]) throws IOException

{

InputStreamReader read = new InputStreamReader(System.in);

BufferedReader in = new BufferedReader(read);

char p;

System.out.println(“Enter a character”);

p=(char)(in.read());

if(Character.isLetter(p))

System.out.println(p+ “is a letter”);

else

System.out.println(p+ “is not a letter”);

}

}

slide29

Program 4:- A program to demonstrate input statement.

import java.io.*;

public class Sample

{

public static void main(String args[]) throws IOException

{

InputStreamReader read = new InputStreamReader(System.in);

BufferedReader in = new BufferedReader(read);

String p;

System.out.println(“Enter your string in upper case”);

p=in.readLine();

System.out.println(“The string in the lower case is “ +(p.toLowerCase());

}

}

slide30

Program 3:- A sample program to illustrate various string Manipulations in Java.

import java.io.*;

public class Manipulation

{

public static void main(String args[])

{

String a = “vijay”;

String b = “computer”;

String c = “ easy understanding “;

String d1 = “Vijay kumar Pandey”;

String d2 = “Vijay kumar Pandey”;

String e = “Dilip Dey”;

System.out.println(“The uppercase of vijay = “+.toUpperCase());

System.out.println(“After replacing ‘m’ with ‘n’ of computer = “+b.replace(‘m’ , ‘n’);

System.out.println(“The string after removing blank spaces = “ +c.trim());

System.out.println(“The equal string = “ +d1.equals(d2));

System.out.println(“The length of the string vijay = “+e.length());

System.out.println(“The character at 5 th position =“+b.charAt(5));

System.out.println(“The concat of the Strings= “ +a.concat(e));

slide31

Q. Write a program to accept a string in lowercase and replace ‘e’ with ‘*’ in the given String. Display the new String

import java.io.*;

public class replace

{

public static void main(String args[]) throws IOException

{

InputStreamReader read = new InputStreamReader(System.in);

BufferedReader in = new BufferedReader(read)

int a,b,p ;

String st;

char chr;

System.out.println("Enter a string");

st=in.readLine();

p=st.length();

for(a=0; a<p; a++)

{

chr= st.charAt(a);

{

slide32

if(chr == 'e')

chr = '*';

}

System.out.print(chr);

}

}

}

slide33

Q. Write a program in Java to accept a string and change the case of each character of the string display the new string.

import java.io.*;

public class Change

{

public static void main ( String args [] ) throws IOException

{

InputStreamReader read = new InputStreamReader(System.in);

BufferedReader in = new BufferedReader(read);

String name , name1 = “ “;

int a, i ;

char chr, chr1=0;

System.out.println(“Enter your Word”);

name=in.readLine();

a=name.length();

for(i=0; i<a; i++)

{

chr=name.charAt(i);

if(chr>=‘A’&& chr<=‘Z’)

{

slide34

chr1 = Character.toLowerCase(chr);

name1 = name1+chr1;

}

else if ( chr>=‘a’ && chr<=‘z’)

{

chr1= Character.toUpperCase(chr);

name1 = name1 + chr1;

}

else

name1 = name1 + chr;

}

System.out.println(name1);

}

}

slide35

Q. Write a program in Java to accept a string and find

  • Number of blank spaces in the string
  • Number of words in the string
  • Number of characters present in the string.
  • import java.io.*;
  • public class Sample
  • {
  • public static void main(String args []) throws IOException
  • {
  • InputStreamReader read = new InputStreamReader(System.in);
  • BufferedReader in = new BufferedReader(read);
  • String name ;
  • int a, i , b = 0 ; w = 0 ;
  • char chr = 0;
  • System.out.println(“Enter your String”);
  • name = in.readLine();
  • a= name.length();
  • for(i=0; i<a ; i++)
  • {
  • chr = name.charAt(i);
  • if(chr == ‘ ‘)
  • b = b + 1;
slide36

}

w= b + 1 ;

}

System.out.println(“The no. of blank spaces in the string is “ +b);

System.out.println(“The no. of words in a string is “ +w);

System.out.println(“The no. of alphabets in the string is “+(a-b));

}

}