Ads

String Handling

Strings, which are widely used in Java programming, are a sequence of characters. Basically, String is a class in Java and we work with objects of String class.

Java String literal is created by using double quotes. For Example:

String s = “Learn Java”



Some String methods with their descriptions

int length()
This function returns string length (i.e., total number of characters present in a String)

char charAt(int index)
This function returns a character value for the particular position.

String Handling in | Java Part - 1


int indexOf(char ch)
This function returns the index position of the particular character in the String.

int lastIndexOf(char ch)
This function returns the index position of the last occurrence of a character in a String.

String substring(int start_index)
This function is used to extract a set of characters simultaneously from a given index up to end of the string.

String substring(int start_index, int last_index)
It is another modification of substring function. It will return a set of characters simultaneously from start index up to the last index of the string (excluding the character present at the last index).

String toLowerCase()
This function converts all the characters (if in upper case) of a given string into lower case. If any character is already in lower case or is a special character then it will remain same.

String toUpperCase()
This function converts all the characters (if in lower case) of a given string into upper case. If any character is already in upper case or is a special character then it will remain same.

boolean isLowerCase()
This function is used to check whether all the characters of a given string are in lower case or not. It returns boolean value true if all the characters are in lower case, otherwise false.


String Handling in | Java Part - 2

boolean isUpperCase()
This function is used to check whether all the characters of a given string are in upper case or not. It returns boolean value true if all the characters are in upper case, otherwise false.

String concat(String s)
This function is used to concatenate (join) two strings together and returns in one string.

String replace(char old_char, char new_char)
This function is used to replace a character by another character in the given string and returns a new string.

String replace(old_sequence_of_characters, new_sequence_of_characters)
This function is also used to replace a sequence of characters by another sequence of characters at all its occurrences in the string.

boolean equals(String s)
This function is used to compare two strings together to check whether they are identical or not. It returns a boolean type value true if both are same, otherwise false.

boolean equalsIgnoreCase(String s)
This function is also used to compare two strings to check whether both are identical or not after ignoring the case (i.e., corresponding upper and lower case characters are treated to be the same). It also returns a boolean type value as true or false.

int compareTo(String s)
This function is also used to compare two strings. But, it not only checks the equality of two strings but also checks whether a string is bigger or smaller than the other. It will return 0 if both the strings are same, a negative value if first string is smaller and a positive value if the first string is bigger.


String Handling in Java | Part - 3

How to take String as input?

We can take String as input through either main () method or using Scanner class. The procedure of taking input are given below:

Using main() method

Step 1: write the codes like below:

Step 2: At the time of taking input use double quotes (" ").

Using Scanner class

Step 1: Import the java class java.util where the Scanner class is located.
import java.util.*;

Step 2: Create object of Scanner class.
Scanner sc = new Scanner(System.in);
Here, sc object is created of Scanner class.

Step 3: Use either next() method or nextLine() method with object of Scanner class, as your requirement. next() method is for single word and nextLine() is for multiple words.
String s1 = sc.next();
String s2 = sc.nextLine();
Here, s1 will accept only one word and s2 will accept multiple words.
SHARE

Author

Hi, Its me Hafeez. A webdesigner, blogspot developer and UI/UX Designer. I am a certified Themeforest top Author and Front-End Developer. I'am business speaker, marketer, Blogger and Javascript Programmer.

  • Image
  • Image
  • Image
  • Image
  • Image
    Blogger Comment
    Facebook Comment

0 Comments:

Post a Comment