56 Tanay JAVA Exp7
56 Tanay JAVA Exp7
7
String Operations
Aim: a. WAP to count number of occurrences of given character using string class.
Theory:
In java, string is basically an object that represents sequence of char values. An array of
characters works same as java string.
The java.lang.String class provides a lot of methods to work on string. By the help of these
methods, we can perform operations on string such as trimming, concatenating, converting,
comparing, replacing strings etc. Java String is a powerful concept because everything is
treated as a string if you submit any form in window based, web based or mobile application.
In java, string objects are immutable. Immutable simply means unmodifiable or unchangeable.
Once string object is created its data or state can't be changed but a new string object is
created.
Java String class methods
The java.lang.String class provides many useful methods to perform operations on sequence of
char values.
unchangeable. Once string object is created its data or state can't be changed but a new string
The java.lang.String class provides many useful methods to perform operations on sequence of
char values.
1 char charAt(int index) returns char value for the particular index
4 String substring(int returns substring for given begin index and end
beginIndex, int index
endIndex)
5 boolean equals(Object checks the equality of string with object
another)
6 boolean isEmpty() checks if string is empty
7 String concat(String str) concatinates specified string
8 String replace(char old, replaces all occurrences of specified char value
char new)
9 String replaces all occurrences of specified
replace(CharSequence CharSequence
old, CharSequence new)
10 String trim() returns trimmed string omitting leading and
trailing spaces
11 String intern() returns interned string
12 int indexOf(int ch) returns specified char value index
13 int indexOf(int ch, int returns specified char value index starting with
fromIndex) given index
14 int indexOf(String returns specified substring index
substring)
15 int indexOf(String returns specified substring index starting with
substring, int fromIndex) given index
16 String toLowerCase() returns string in lowercase.
17 String toUpperCase() returns string in uppercase.
18 String trim() removes beginning and ending spaces of this
string.
PROGRAM/CODE: a
Program Code: b
Conclusion:
We implemented string class operations to calculate occurences of vowels and a given character in a string.