Home | | Sitemap ||Page number :24

String Handling in Java

In the second chapter you read about several data types. The concept of “Strings” was left unattended, as they are entirely different from any other conventional data type. In this chapter we shall study about each and every aspect of String handling.

What is a String?

You are already familiar with the name of “java.lang” package. This package contains many classes that support various runtime processes. This package is really important which you can understand after reading this---“The java.lang package is so vital for the execution of java programs that it is imported automatically in our code, thus we don’t have to do it ourselves manually each & every time”. String is a class contained by this package. Using the object of this class we can manipulate or work with series of characters.

A similar class-StringBuffer

It’s not possible to edit the contents of String once initialized. Example — String word=“Matter” The contents of String can be changed completely, i.e. String ‘word’ can contain values such as “solid”, “type” or “af12” etc. You might now wonder, why earlier it was mentioned to be ‘non-editable’? This is all because you can’t remove the characters of a string by choice. Example - Removing ‘M’& “ter” changing the sting value to “at”.

Hence sometimes we have to use StringBuffer as it is editable. Remember:: StringBuffer is a class that belongs to the java.lang package.

Creating a String

To create a String we have to follow a procedure that is somewhat similar to creating an object. General way of creating an object

Classname obj=new Classname();

Syntax for creating a String ---

String name=new String(); But if you want to declare and initialize a String simultaneously then the procedure is a bit different. Imagine creating a string and initializing it with the word “India”.

String name=“India”

Concatenation of String

Sometimes we have two strings, we can then use ‘+’ operators to join them, this is known as Concatenation. Have a look at this program…

class concat

{ public static void main(String args[]) { String name="Ankit"; int marks=93; System.out.println("Printing Details"+ " of "+"the student" ); //Concatenating Strings System.out.println(name+"'s"+" marks "+"are--"+marks); } }

df

Manipulating Strings

There are several methods associated with the “String” class that help us to manipulate it. You already know that call to a method is made by its object in this manner--- obj.method(); Thus in a similar way we will be using the methods. Example- String name=“India”; name.length(); // name is the object of String class and length() is the method.

Finding length of a String---

Length of a string is defined as the number of characters in a String. We use the “length()” method to get the length of a String. Syntax—

name.length(); Let us look at a program using this method-----

import java.io.*; class length { public static void main (String args[]) throws IOException { String a; int m;

InputStreamReader reader=new InputStreamReader(System.in); BufferedReader input=new BufferedReader(reader); System.out.println("Enter a String "); a=input.readLine(); System.out.println(a.length()); } }

zd

Remember: A space is also counted as a character.