Introduction

  • In several other programming languages, strings are stored in arrays of characters (such as C, C++, Pascal, etc.). However, in Java strings are a separate object type, called String.
  • Java implements strings as objects of the String class when no change in the string is needed, and objects of the StringBuffer class are used when there is a need to manipulate in the contents of the string.

Definition

  • In Java, a string is a set of characters.

Characteristics

  • In Java, there are two main different classes to represent characters & strings –

(A) For (single)character value – Character class and,

(B) For multiple characters/strings value – String and StringBuffer class.

Character class :

      • In Java, an object of the Character class can contain only a single character value.
      • This class also contains some related methods which are used to manipulate or check single-character data.
      • Character object is used in place of a primitive ‘char’ variable when an object is required such as in Vector (a data structure) programming.
      • Character objects are created as –
Syntax = Character objectname= new Character();
Example = (i) Character Obj1= new Character(); //without parameter.
(ii) Character Obj2= new Character(‘x’);  // with parameter
      • In-built Constructors & Methods of Character Class
      • Constructors of Character Class
        • There are several constructors in Java to create Character objects.
        1. Character (char) :
          • This is the Character class-only constructor.
          • It is used to create a Character object containing the value provided by the argument. But once a Character object has been created, the value it contains cannot be changed.
      • Methods  of Character Class
        • There are several built-in methods in Java to make the operation easy.

(I) compareTo (Character Object Name) :

          • This method is used to compare the values held
            by two character objects.
          • This method is applicable for the value of the object on which the method is called and the value of the object passed as an argument to the method.
          • Example – 
Character Obj1 = new Character(‘x’);
Character Obj2= new Character(‘y’);
int diff = Obj1.compareTo(Obj2);
          • This method returns an integer value after comparing two character objects which indicates whether the value in the current object is greater than, equal to, or less than the value held by the argument. If the integer value is 0 then the character matched otherwise not matched.

(II) equals(Character Object Name) :

          • This method is used to compare the value held by one/current object with the value held by another object passed as an argument.
          • This method returns true if the values held by both objects are equal.
          • Example :
Character Obj1 = new Character(‘x’);
Character Obj2= new Character(‘y’);
Obj1.equals(Obj2);

(III) to_String () :

          • This method is used to convert the character object to a string.
          • The resulting output will have one character but in String form.

(IV) char_Value() :

          • It converts a character object into its primitive char value.

(V) isUpperCase (char) :

          • This method is used to determine whether a primitive char (data type) value is in capital letters/uppercase or not.
          • It returns true if the character is in the upper case letter.

    String class :

        • In Java, the object of a String class can hold multiple character values.
        • Normally, objects of this class are used to deal with the strings that are not changed/modified (static) during processing/run time.
        • It is good practice to use the String class wherever we need fixed-length objects i.e. immutable (size cannot be altered).
        • When we create a String object and if it has the same value as another object, Java will point both object references to the same memory location.
        • String class objects are of fixed length and no modification can be done in the content of strings except replacement of characters.
      • In-built Constructors & Methods of String Class
      • Constructors of String Class
        • There are several constructors in Java to create String objects.

    (I) String() :

          • This constructor is used to create a String object that represents an empty character sequence.

    (II) String(String value) :

          • This constructor is used to create a String object with same sequence of characters as the argument.

    (III) String(char value[ ]) :

          • This constructor is used to create a new String object with the sequence of characters currently contained in the character array argument.

    (IV) String(char value[], int offset, int count) :

          • This constructor is used to create a new String object contains characters from a sub-array of the character array argument.

    (V) String(StringBuffer buffer) :

          • This constructor is used to create an object of String by using existing StringBuffer object which is passed as argument.

    Methods of String Class

        • There are several in-built methods present in Java String class to handle the String operation/String objects easy.
        • The whole java methods of String class are broadly classified into following groups –

    (1) Index Methods :

    All Index methods have return type is ‘int’.

    (I) length() :

          •  It gives the length of the string in integer form.

    (II) indexOf(int ch) :

          • It returns position/location (value) of first occurrence of given character(ch) as argument in the string.
          • if given character don’t exist in the string then it returns
            –1.

    (III) indexOf(int ch, int fromIndex) :

          • It returns location of occurrence of character(ch) in the string after fromIndex value.
          • if character(ch) don’t exist in the string it returns –1.

    (IV) lastIndexOf(int ch)

          • It returns location of last occurrence of given character (ch)in the string.
          • If character(ch) location does not exist in string it returns –1.

    (V) lastIndexOf(int ch, int fromIndex)

          • It returns last of occurrence of character(ch) in the string after location fromIndex.
          • if character (ch) does not exist in string after location fromIndex it returns –1.

    (VI) indexOf(String str)

          • It returns location of first occurrence of substring as given in ‘str’ argument from whole string.
          • If ‘str’ paramenter string does not exist in whole string then it returns –1.

    (VII) indexOf(String str, int fromIndex)

          • It returns location of first occurrence of substring ‘str’ in a given whole string after location ‘fromIndex’ value,
          • If ‘str’ substring does not exist in string then it returns –1.

    (VIII) lastIndexOf(String str)

          • It returns location of last occurrence of substring ‘str’ in
            a given string.
          • If ‘str’ substring does not exist in the given string then it returns –1.

    (IX) lastIndexOf(String str, int fromIndex)

          • It returns location of last occurrence of substring ‘str’ in
            the given string after location ‘fromIndex’ value.
          • If ‘str’ substring does not exist in the given string then it returns –1.

    (2) SubString Methods :

    (I) charAt(int index)

          • This method returns the character present at given position(index value 0 to n-1)in the string.
          • Its return type is ‘char’.

    (II) substring(int beginIndex)

          • This method returns substring from the given string from ‘beginIndex’ value to the end of string.
          • Its return type is ‘String’.

    (III) substring(int beginIndex, int endIndex)

          • This method returns a substring from a given string from ‘beginIndex’ value to the ‘endIndIndex’  as given in the parameter.
          • Its return type is ‘String’.

    (IV) concat(String str)

          • This method adds more string(as given in the ‘str’ parameter) with the given string by appending it.
          • Its return type is ‘String’.

    (V) toCharArray()

          • This method returns character array of given string.
          • Its return type is ‘char[ ]’.

    (3.) Comparison Methods :

    (I) equals(Object str)

          • This method returns true(i.e. Strings are equal/same)if strings contain the same characters in the same order considering letter case. 
          • It is case sensitive.
          • Its return type is ‘boolean’.

    (II) equalsIgnoreCase(String Str)

          • This method returns true (i.e. Strings are equal/same) if strings contain the same characters in the same order ignoring letter case. 
          • It is not case sensitive.
          • Its return type is ‘boolean’.

    (III) compareTo(String Str)

          • This method is used for identifying whether two strings are identical or not.
          • This method compares Str value (as given in the parameter) with source String value 
            • If they return less than 0 then Str < sourceString and are unequal.
            • If they return 0 then both String are equal/same.
            • If they return greater than 0 then Str >sourceString and are unequal.
          • It is case sensitive method.
          • Its return type  is ‘int’. 

    (IV) startsWith (String Str, int val)

          • It returns true if ‘Str’ occurs at index ‘val’.
          • Its return type  is ‘boolean’. 

    (V) startsWith(String Str)

          • It returns true if string value start with ‘Str’.
          • Its return type  is ‘boolean’. 

    (VI) endsWith(String Str)

          • It returns true if string value ends with ‘Str’.
          • Its return type  is ‘boolean’. 

    (4) Modifying String Methods :

    (I) replace(char oldChar, char newChar)

          • This method returns a new string value by replacing all
            ‘oldChar’ value by given ‘newChar’ value.
          • Its return type is ‘String’.

    (II) toLowerCase()

          • This method converts the given string(either in capital or small letter) finally in lowercase letter.
          • Its return type is ‘String’.

    (III) toUpperCase()

          • This method converts the given string(either in capital or small letter) finally in uppercase letter.
          • Its return type is ‘String’.

    (X) trim()

          • This method returns a new string by removing extra 
            blank/whitespace from front and back of the string both.

    (5) valueOf Methods :

          • These methods are mainly used to convert different data type values like integer, float double etc. into its respective String form.
          • valueOf() methods always returns a string value equivalent to the value which is passed in it as argument.

    (I)valueOf(boolean b)

          • This method gives string representation of the boolean argument(b).

    (II)valueOf(char c)

          • This method gives string representation of the character argument(c).

    (III)valueOf(int i)

          • This method gives string representation of the integer argument(i).

    (IV)valueOf(long l)

          • This method gives string representation of the long argument(l).

    (V)valueOf(float f)

          • This method gives string representation of the float argument(f).

    (VI)valueOf(double d)

          • This method gives string representation of the double argument(d).

    (VII)valueOf(char data[ ])

          • This method gives string representation of the character array argument(data[ ]).

    (6) Other Methods :

    String Buffer class :

        • Java StringBuffer class is used mainly when a user wants to create some changes in the given string i.e. mutable (modifiable) String objects. In other words, the StringBuffer class in Java is the same as String class except it is mutable/modifiable/content changeable i.e. using StringBuffer class methods we can change the given string or contents  easily.
        • It is a peer/advance class of String that provides much of the functionality than strings.
        • When we need strings, which can be modified, then StrinBuffer class object should be used. In another words, in the strings created by using StringBuffer class, we may insert new characters and sub-strings either in the beginning, middle or append at the end as per need.
        • In contrast to String objects, a StringBuffer object represents growable and writeable character sequences.

    In-built Constructors & Methods of String Buffer Class

      • Constructors of String Buffer Class
        • There are several constructors in Java to create String Buffer objects.

    (I) StringBuffer() :

          • This constructor creates a string buffer with no characters in it
          • It has an initial capacity of 16 characters.

    (II) StringBuffer(int size) :

          • This constructor creates a string buffer with no characters in it.
          • It has an initial capacity specified by the length of the argument.

    (III) StringBuffer(String Str) :

          • This constructor creates a string buffer so that it represents the same sequence of characters as the string argument.
          • The initial contents of the string buffer is a copy of the argument string.

    Methods of String Buffer Class

        • In addition to functionality of String class methods, SrtingBuffer also provide methods for modifying strings by inserting a substring, deleting some content of string, appending string and altering string size dynamically.

      (I) append(String s) :

            • This method is used to append/add new string at the end of previous given string.
            • The append() method is exists in several form such as  append(char), append(boolean), append(int), append(float), append(double) etc.

      (II) insert(int offset, String s) :

            • This method is used to insert/add new string with previous or existing string at the specified position or location.
            • This method exists in several form such as insert(int, char), insert(int, boolean), insert(int, int), insert(int, float), insert(int, double) etc.

      (III) replace(int startIndex, int endIndex, String str) :

            • This method is used to replace the existing string or substring from given startIndex and endIndex value.

      (IV) delete(int startIndex, int endIndex) :

            • This method is used to delete or remove the existing string or substring from given startIndex and endIndex value.

      (V) charAt(int index) :

            • This method is used to return single character from the given string from the specified position or location.

      (VI) substring(int beginIndex) :

            • This method is used to return the substring from the given string form specified or given beginIndex value.

      (VII) substring(int beginIndex, int endIndex) :

            • This method is used to return the substring from the givne string from specified or given beginIndex and endIndex value.

      (VIII) length() :

            • It returns the length of the given string i.e. total number of characters present in a string.

      (IX) reverse() :

            • This method is used to reverse the given string.

      (X) capacity() :

            • This method is used to give the current capacity of the given string.

      Categories: Java

      1 Comment

      Shabistan tashneem · June 19, 2019 at 9:41 AM

      😇

      Leave a Reply

      Your email address will not be published. Required fields are marked *

      This site uses Akismet to reduce spam. Learn how your comment data is processed.