String Handling In JAVA, It's Functions, Computer

 String Handling in Java

STRING CLASS

A String is a collection of characters in a sequence; for example a word as well as a sentence, A String is treated as object which is constant i.e., a String can not be changed once it is created in a program. A String can be declared as follows:

String str = "hello java",

or

String str = new String("hello java");

Note: In Java once a String object is created it cannot be modified or changed i.e., String objects are immutable. To enable modification of Strings we have StringBuffer class. Thus StringBuffer class is mutable.

CREATING A STRING

There are two to ways to create a String:

1. Use of String Literal

2. Use of new Keyword

String Literal

A user can create a simple String by using String literal which would be enclosed inside double quotes. When String literal is encountered in a program the Java compiler creates a String object with value of String literal.

Syntax:

String str "hello java":

String literal is a reference that can be manipulated like other String references. The reference value of the String literal can be assigned to another String reference. If we create two or more Strings with same sequence of characters then both the Strings would share same reference in memory.


Note: In Example 2 two String literals are created with the same sequence of characters. As we know that two or more String literals with the same sequence of characters share the same reference in memory pointing to the same object. Thus s1 and s2 point to the same object.

Using new Keyword 
By using String literal we can create multiple Strings. If we assign two or more String objects to literals with the same character sequence then the compiler creates a single object to point to the same object. To solve these types of problems we use new keywords. By using a new keyword we can create an object in the same way as we create other objects. In Java, the String class has 11 constructors which provide the initial value of a character.

METHODS OF STRING CLASS

In Java String class has many methods to perform the specific operations on Strings such as trimming the spaces, changing case, concatenating Strings, comparing Strings, etc. Let us study them one by one.

String trim()
String trim() method is used to avoid the white space before of any String. Trim method does not have any parameters.
Syntax:
String S1= "hello";
String S2= new String(S1.trim());
System.out.println(S1.trim());

String to LowerCase()
String toLowerCase() method is used to convert all the characters in a String to lowercase. String toLowerCase() method does not have parameters.
Syntax:
String S1= "HELLO";
String S2= new String(S1.toLowerCase()); 
System.out.printin(S1.toLowerCase());


String to UpperCase()
String to UpperCase() method is used to convert all the characters in a String to uppercase. String to UpperCase() method does not have parameters.

Syntax:

String S1 = HELLO "; 
String S2 = new String(S1.toUpperCase()); 
System.out.println(S1.toUpperCase());

 

int length()

This method returns the length of a sequence of characters as an int value.

Syntax:

String S1 = " hello "; 

System.out.println(S1.length());


char charAt (int n)

This method returns the specific character at index 'n' of a String by using an index starting from 0. Suppose we have a String "hello" and we want to find w_{0} ^ m so we have to set the index to 4 to get "0".

Syntax:

String S1=" hello";

System.out.println(S1.charAt(int index));


char charAt (int i)
This method returns the specific character at index 'n' of a String by using index starting from 0. Suppose we have a String "hello" and we want to find "o" so we have to set index to 4 to get "o".

Syntax:

String S1 =" hello "; 
System.out.println(S1.charAt(int index));

 


int IndexOf(Char ch)
This method returns the index as an int value of the first occurrence of the specified character of any String. An index would start from 0. We would declare the required character as a parameter of this method.

Syntax:

String S1 = " hello ";
System.out.println(S1.indexOf(char ch));

 int lastIndexOf(char ch)

This method returns the index of int value of last occurence of the specified character of any String. Index would start from 0. We would declare the character as a parameter of this method.

Syntax:

String S1 = " hello "; 

System.out.println(S1.lastIndexOf(char ch));



String concat(String str)
This method is used to concatenate two Strings. String concat(String str) method appends the second String at the end of first String. We define the String or String object which we want to concatenate as a parameter.

Syntax:

String S1 =" hello "; 
S1.concat(String str);


boolean equals(String str) 

This method compares a String with another String. The result will true if the characters, the case of characters and the sequence of the characters are the same. In case there is some difference even in the case (upper or lower) it will return false.

Syntax:

String S1 =" hello ";

boolean val; val=

S1.equals(String str);


boolean equalsIgnoreCase(String str)

This method compares a String to another String. The result will return true if the character and sequence of the characters is same. This method ignores case. We can write the String in upper or lower case if the sequence of characters are same it will return true otherwise return false.

Syntax:

String S1 =" hello ":

boolean val;

val = S1.equalsignoreCase(String str);

Note: The == operator is used for comparison of two integer values and the equals() operator is generally used to compare strings. The equals() operator returns true if the strings are the same else it returns false. 




int compareTo(String str)

This method is used to compare two Strings lexicographically. All the characters of both Strings are converted to Unicode for comparison. If all the characters of both Strings are the same then the result will be 0 otherwise the result will be a positive or negative value. If the first String is larger than the second it will return a positive value else it will return a negative value. We pass the String object or String which we want to compare as arguments.

Syntax:

String S1 =" hello "; 

int val; 

val = S1.compare To (String str);

ASCII Code:- Capital A-Z = 65-90, small a-z = 97-122

int compareToIgnoreCase(String str)

This method works like the compareTo(String str) method but it ignores case differences. The return value will be zero if both Strings have the same sequence even if the second String is in uppercase or lowercase the return value will remain 0.

Syntax:

String S1 =" hello ":

int val;

val= S1.compare TolgnoreCase(String str);


 String replace(char oldChar, char newChar)

This method is used to replace characters or Strings. It will replace all occurring old Char characters in String with new newChar character. We pass the old character which we want to replace or the new character with whom we want to replace the old one as argument.

Syntax:

String S1="hello";

System.out.println(S1.replace('o', 's'));

 


String substring(int beginIndex)
This method returns a substring of the main String. The substring begins with the character at a specified index. We pass the index value from where we want to start the substring as the argument.

Syntax:

String S1 =" hello"; 

System.out.println(S1.substring(int beginIndex));


 String substring(int beginIndex, int endIndex)

This method is an extended form of substring(int beginIndex). In this method, we can specify the point from which the substring would start and at which it would end. We pass the two index values (the first is from where we want to start the substring and the second is where the substring would end) as an argument.

Syntax:

String S1 =" hello";

System.out.println(S1.substring(int beginIndex, int endIndex));


boolean startsWith(String str)

This method checks the String and returns the value as true if the String begins with a specified sequence of characters passed as an argument of this method, else it returns false.

Syntax:

String S1 = "hello";

boolean val;

val = S1.startsWith(String str);


boolean ends With(String str)

This method checks the String and returns the value as true if the String ends with the specified sequence of characters passed as the argument of this method, else it returns false

Syntax:

String S1 = " hello ";

boolean val;

val=S1.endsWith(String str);


String valueOf(datatype dt)

This method returns the value of object which is passed as argument or returns the String representation which is passed as argument. The argument can be a primitive data type or String. It has the following sub-types depending on data type.

 String valueOf(int i): This Method will return the String representation of integer type argument.

▸ String value Of(float f): This Method will return the String representation of float type argument.

 String value Of(long 1): This Method will return the String representation of long type argument.

String valueOffdouble d): This Method will return the String representation of double type argument.

▸ String valueOf(char c): This Method will return the String representation of character type argument.


STRING ARRAY

In Java String array is an object which holds a fixed number of String values. It works like a normal array with a fixed number of Strings. By using a String array we can sort the String and solve many sorting and arranging problems.

Initialisation of String Array

There are two ways to initialise a String array

1. Inline Initialisation

Syntax:

String[] str_arr = {'a', 'b','c','d'};

2. Initialisation after Declaration

Syntax:

String[] str_arr = new String[4];

str_arr [0] = "a";

str_arr [1] = "b";

str_arr [2] = "c";

str_arr [3] = "d";

Example 1: Sorting a String array by using Bubble sort technique. 



Example 2: Sorting the String array by using Selection sort technique.



Note: The character array char[] uses the length keyword to determine the length of the array whereas String object uses a method called length() to calculate the total number of characters in the String Object

char[] eg1 = {'c', 'o', 'm', 'p', 'u', 't', 'e', 'r'}

System.out.printerln (eg1.length)

Output: 8

String eg2= 'Applications" 

int len = eg2.length()

Output: 12

Example 3: Search a String array by using a linear search technique.




Thank You So Much For Reading Till End!

Please share this post with your batch mates & whoever else needs this.

1 Comments

Post a Comment

Previous Post Next Post