5 Strings — Java Programming
5.1. Introduction
Strings play an important role in Java programming. Let us explore various classes and functions related to Strings.
5.2 Strings
A string is an array of characters. We can define string as below:
char chars[] = {'a','b','c'};
A string has a fixed number of characters. In Java, a string is an object of the class, which belongs to java.lang package. The string class is available to all classes by default. String objects are immutable- that is once the object is created, the contents cannot be changed. A variable declared as a String reference can be changed to point at some other String object at any time.
For example:
String str=”this is an example”; System.out.println(str);
Constructor is used as in the statement String s=new String();
String belong to the package java.lang.String
5.2.1 Different ways of calling the String constructor
|
String Constructor |
Description |
|
public String(); |
Instantiating a String object. |
|
public String(char value[]); |
Give a character array. |
|
public String(char value[], int offset, int count); |
Get the substring starting from offset count number of chars. |
|
public String(String value); |
Directly give the string constant as the parameter. |
5.2.2 Methods of String class
|
Method |
Description |
|
public int length(); |
Returns the length of the String. |
|
public char charAt(int index) |
Return the character at the specified index |
|
public int indexOf(int ch) public int indexOf(int ch, int fromindex) public indexOf(String s) public indexOf(string s, int f) |
Returns the index of the first occurrence of the character, ch. If unsuccessful returns -1 Returns the first occurrence of the character, ch, that is greater than or equal to from index, if not successful return -1. Returns index of the first of the first occurrence of the specified string Returns index of the first occurrence of the specified string. |
|
public int lastIndexOf(int ch) public int lastIndexOf(int c, int f) public int lastIndexOf(String s) public int lastIndexOf(String s,int f) |
Returns the index of the last occurrence of the specified character, ch. If Unsuccessful -1 is returned Return the last occurrence from the specified index, if unsuccessful returns -1. Returns the index of the last occurrence of the specified string. Returns the index of last occurrence of the specified string. |
|
public string substring(int beginIndex, int endIndex) |
Create and returns a new string that is a sub string from the specified beginIndex to the specified endindex. |
|
public boolean equals(object obj) |
The result is true if the argument is not null and is string object that represents the same sequence of characters as this object obj. |
|
public String replace(char oldch, char newch) |
Returns a new string resulting from replacing all occurrences of oldch in this string with newch. |
|
public Boolean startsWith(String prefix) |
Return true if the character sequence represented by the argument i