String Handling in java
Published: 2021-05-10 05:56:18

          String handling is described in detail. As is the case in most other programming languages, in java a string is a sequence f characters. But, unlike some other languages that implement strings as character arrays, java implements strings as object of type string.
          Implementing strings as built in objects allows java to provide a full complement of features that make string handling convenient. For example, Java has methods to compare two strings, search for a substring, concatenate two strings, and changes the case of letters within a string. Also, string objects can be constructed a number of ways making it easy to obtain a string when needed.

THE STRING CONSTRUCTORS:

            The string class supports several constructors. To create an empty string, call the default constructor. For example,

               string s = new string ();

* STRING LENGTH:
                 The length of a string is the number of characters that it contain. To obtain this value, call the length() method, shown here:
                int length()
The following fragment prints "3", since there are three characters in the string s:

           char chars[] = { 'a', 'b', 'c' };
           string s = new string(chars);
           system.out.println(s.length());

CHARACTER EXTRACTION:

            The string class provides a number of ways in which characters can be extracted from a string object. Several are examined here. Although the characters that comprises a string within a string object cannot be indexed as if they were a character array, many of the string methods employ an index into the string from their operation. Like arrays, the string indexes begin at zero.

* charAT():
           To extract a single character from a string, you can refer directly to an individual character via the charAt() method. It has this general form:
                   char charAt(int where)
Here, where is the index of the character that you want to obtain. The value of where must be nonnegative and specify a location within the string. charAt() returns the character at the specified location. For example,
             char ch;
             ch = "abc".charAt(1);

    assigns the value b to ch.

* getChars():
             if you need to extract more than one character at a time, you can use the getChars() method. It has this general form:
 
void getChars(int sourceStart, int sourceEnd, char target[], int targrtStart)

program:
    class getcharsDemo {
      public startic void main(string args[]) {
          string s = "This is a demo of the getChars mrthod.";
          int start = 10;
          int end = 14;
          char buf[] = new char[end - start];
           
           s.getChars(start, end, buf, 0);
           system.out.println(buf);
        }
      }

here is the output of this program:
      demo

* getBytes():
             There is an alternative to getChars() that stores the characters in array of bytes. This method is called getBytes(), and it uses character to byte conversions provided by the platform. Here is its simplest form:

        byte[] getBytes()

* to CharArray:
               If you want to convert all the characters in a string object into  a character array, the easiest way is to call toCharArray(). It returns an array of characters for the entire string. It has this general form:

               char[] to CharArray()

  This function is privided as a convenience, since it is possible to use getChars() to achieve the same result.