Class 11 | String | Java Programming Notes

UNIT 6
Class 11 Java Programming

String

Original Scanned PDF – View Notes

01

Introduction To String

  • A string is an object that represents a sequence of characters.
  • Java.lang.String class is used to create a string object.
  • Double quotes (" ") is used to represent string constant.
  • For example: "Ram" is a string containing a sequence of characters 'R', 'a' and 'm'.
02

String Declaration

String can be declared by two ways.

  1. By String literal
  2. By new keyword

1) By String Literal

It is direct method to create string using

Syntax

String stringname = "string";

Example

String s1 = "Ram";
String s2 = "welcome";

Java Program to Create String by String Literal

public class StringTest
{
    public static void main(String []args)
    {
        String s1 = "Ram";      // creating string
        String s2 = "Hello Java";

        System.out.println("First string is =" + s1);
        System.out.println("Second string is =" + s2);
    }
}

2) By new Keyword

  • The new keyword is used to create objects including String.
  • It is less common and typically done when we want to create string explicitly.

Syntax

String str = new String("String");

Example

String str = new String("Hello Java");

Here, a new string object str is created with the value "HelloJava". The new keyword is followed by the string constructor which takes a string literal "HelloJava" as its argument.

Program to Create String Using new Keyword

public class StringTest
{
    public static void main(String []args)
    {
        String s1 = "Java";   // creating string by string literal
        String s2 = new String("Programming");
                         // creating string using new keyword

        System.out.println("First string :" + s1);
        System.out.println("Second String :" + s2);
    }
}
03

String Manipulation Methods / Functions

Java.lang.String class includes many methods for manipulating on string.

Some methods are:

1) length() Method

  • Used to find length of given string.
  • It returns no. of characters included within string.

Syntax

String.variable.length();

Example to Demonstrate length() Method

import java.lang.String;

class Stringlength
{
    public static void main(String []args)
    {
        String str = "welcome";   // creating string
        System.out.println("String is =" + str);

        int len = str.length();   // method calling
        System.out.println("The length of String =" + len);
    }
}

2) concat() Method

This method join or concat two string and form a single string.

Syntax

str.Var1.concat(str.Var2);
Source note: The heading above the following program is written as “Example to demonstrate length() method” in the supplied scan, although the program itself uses concat(). The source wording is preserved below.

Example to Demonstrate length() Method

import java.lang.String;

public class Stringlength
{
    public static void main(String []args)
    {
        String s1 = "Ram";       // creating string
        String s2 = "Hari";

        System.out.println("First string :" + s1);
        System.out.println("Second string :" + s2);

        String s3 = s1.concat(s2);
        System.out.println("concatenated string :" + s3);
    }
}

3) equals()

  • This method is used to make comparison between two strings to check whether strings are equal or not.
  • It returns logical value either true or false.

Syntax

str1.equals(str2);

Example

Source transcription note: The example leaves the class name blank, redeclares the variable result, and the second comparison is visibly written as s1.equals(3). These details are retained rather than silently corrected.
import java.lang.String;

public class
{
    public static void main(String []args)
    {
        String s1 = "Java";
        String s2 = "Java";
        String s3 = "Programming";

        boolean result = s1.equals(s2);
        System.out.println("First and second strings are equal :" + result);

        boolean result = s1.equals(3);
        System.out.println("First and third strings are equal :" + result);
    }
}
04

StringBuffer Class

  • StringBuffer is a class included within java.io package.
  • It is used to create mutable (changable) string objects.
  • It is same as String class except it is mutable i.e. can be modified the content of object after it has been created.
  • It provides an alternative to the immutable String class.

Some Methods of StringBuffer Class Are:

  1. append()
  2. insert()
  3. replace()
  4. delete()
  5. reverse()

1) append() Method

Used to concatenate (join) the given argument with string.

Syntax

obj.append("String");

Example

import java.io.*;

public class StringTest
{
    public static void main(String []args)
    {
        StringBuffer sb = new StringBuffer("Hello");
        sb.append("Java");

        System.out.println("New string =" + sb);
    }
}

2) insert()

This method inserts the given string with this string at the given position.

Syntax

sb.insert(position, "new string");
sb → String object that holds original string. position → integer number that represents position of character in string.

Example

import java.io.*;

class program
{
    public static void main(String args[])
    {
        StringBuffer sb = new StringBuffer("Hello");
        sb.insert(1, "java");    // original string is changed

        System.out.println("New string :" + sb);
    }
}
O/P:
HJavaello

3) Replace()

This method replaces the given string from the specified begin index and endIndex − 1.

Syntax

replace(beginIndex, endIndex - 1, "string");

Example

import java.io.*;

class program
{
    public static void main(String []args)
    {
        StringBuffer sb = new StringBuffer("Hello");
        sb.replace(1, 3, "Java");

        System.out.println(sb);
    }
}
O/P:
HJavaO

4) delete() Method

This method delete the string from specified begin Index to endIndex − 1.

Syntax

delete(beginIndex, endIndex - 1)

Example

import java.io.*;

class Program
{
    public static void main(String args[])
    {
        StringBuffer sb = new StringBuffer("Hello");
        sb.delete(1, 3);

        System.out.println(sb);
    }
}
Output: The supplied scan shows an output label for this example, but no output value is written.

5) reverse() Method

This method reverse the current string.

Syntax

sb.reverse();

where sb is StringBuffer string object

Example

import java.io.*;

class Reverse
{
    public static void main(String args[])
    {
        StringBuffer sb = new StringBuffer("Hello");
        sb.reverse();

        System.out.println(sb);
    }
}
Output: The supplied scan shows an output label for this example, but no output value is written.
05

StringBuilder Class

  • It is mutable class located in java.lang package used to manipulate string.
  • It is alternative to String class.
  • Similar to StringBuffer class but differs on the basic of synchronization.

Consist of Following Methods:

  1. append() method
  2. insert() method
  3. delete() method
  4. replace() method
  5. reverse() method

1) append() Method

Used to concatenate (Join) the given argument with string.

Syntax

obj.append("String");

Example

Source transcription note: The import line is written simply as import java.lang in the scanned notes. It is preserved as shown.
import java.lang

public class Append
{
    public static void main(String args[])
    {
        StringBuilder sb = new StringBuilder("Hello");
        sb.append("Java");

        System.out.println(sb);
    }
}
Output: An O/P label is present on the next page, but no output value is written.

2) insert() Method

This method insert the given string with this string at the given position.

Syntax

sb.insert(position, "new string");
sb → string object that holds original string. position → integer number that represents position of character in string.

Example

import java.lang

class Insert
{
    public static void main(String args[])
    {
        StringBuilder sb = new StringBuilder("Hello");
        sb.insert(1, "Java");

        System.out.println("New string :" + sb);
    }
}
Output: The supplied scan shows an O/P label, but no output value is written.

3) delete() Method

This method deletes the string from specified beginIndex to endIndex.

Syntax

delete(beginIndex, endIndex - 1)

Example

import java.lang

class Delete
{
    public static void main(String args[])
    {
        StringBuilder sb = new StringBuilder("Hello");
        sb.delete(1, 3);

        System.out.println(sb);
    }
}
Output: The supplied scan shows an O/P label, but no output value is written.

4) Replace() Method

This method replaces the given string from the specified beginIndex and endIndex.

Syntax

replace(beginIndex, endIndex, "String");

Example

import java.lang

class Replace
{
    public static void main(String args[])
    {
        StringBuilder sb = new StringBuilder("Hello");
        sb.replace(1, 3, "Java");

        System.out.println(sb);
    }
}
Output: The supplied scan shows an O/P label, but no output value is written.

5) reverse() Method

This method reverse the current string.

Syntax

sb.reverse();

Example

import java.lang

class Reverse
{
    public static void main(String []args)
    {
        StringBuilder sb = new StringBuilder("Hello");
        sb.reverse();

        System.out.println(sb);
    }
}
Output: Page 17 contains only the end of this program and an O/P label. No output value is visible in the supplied scan.

Discussion

Share a helpful question, idea, or explanation with other students.

Leave a Comment

Write a clear question, answer, or helpful explanation.
Your email will not be published.

Download Our Offline App

Study class-wise notes even when internet is not available. Get the app from Play Store.

Nepal eNotes offline app preview
Get it on Google Play