Thursday, March 20, 2014

String – II



String Functions – String class from package java.lang have some methods which can manipulate string literals.

String1.concat(String1) – This function used for joining two strings together. This can be done by: String1=String1+ String2.

Example:

String Str=“Hello”;
Str=Str.concat(“ Java”);  // or Str=Str + “Java” //Output Hello Java

String.trim() - Removes white space from both ends of this string.

Example:

String Str=“   Hello    ”;
System.out.println(Str.trim()); // Output -"Hello"

String.indexOf(char/string) - Returns the index within the string of the first occurrence of the specified character, if search fail then it will return -1.

Example:

String Str= “I live in India. I love my country India”
int L=Str.indexOf(“India”);  // Output - > 10 [ First occurrence of ‘India’ ]

String.indexOf(char/string, int) - Returns the index within this string of the first occurrence of the specified character, starting from the specified index, if search fail then it will return -1.

Example:

String Str= “I live in India. I love my country India”
int L=Str.indexOf(“India”,12);  // Output - > 35 [ Occurrence of ‘India’ after 12th Postion ]

String.lastIndexof(char/String) - Returns the index within the string of the last occurrence of the specified character, if search fail then it will return -1.

lastIndexOf(String Str, int StartIndex)

int lastIndexOf(String str, int StartIndex): Returns the last index position of specified string starting from start Index.

Example:

String Str="This is a demo of lastIndexOf";
System.out.println(lastIndexOf('i', 3)); // Return 2.

Example:

String Str= “I live in India. I love my country India”
int L=Str.lastIndexOf(“India”);  // Output - > 35 [ last occurrence of ‘India’ ]

String.firstIndexof(char/String) - Returns the index within the string of the first occurrence of the specified character, if search fail then it will return -1.

Example:

String Str= “I live in India. I love my country India”
int L=Str.indexOf(“India”);  // Output - > 10 [ First occurrence of ‘India’ ]

String.startswith(String) -Tests if this string starts with the specified prefix and return a Boolean value.

Example:

System.out.println(“Hello Friends”.startsWith(“A”);  // Return ‘false’

String.endsWith(String) - Tests if this string ends with the specified suffix and return a Boolean value.

Example:

System.out.println(“Hello Friends.”.startsWith(“.”);  // Return ‘true’


String.replace(char, char) or String.replace(String, String)  - Returns a new string resulting from replacing all occurrences of oldChar in this string with newChar or oldString with the newString.

Example:

String test=”You is too good”;
test=rest.replace(“is”,”are”); // New value of tes=”You are too good”

String.toLowerCase() / String.toUpperCase() - Converts the String to lowercase / Converts the String to upper case.

Example:

System.out.println(“Hello Friends”.toUpperCase();  // “HELLO FRIENDS”
System.out.println(“Hello Friends”.toLowerCase(); // “hello friends” 

Thursday, March 13, 2014

String - I




String in java is a special class of objects which represents characters. Java String is a immutable object and immutable object cannot be modify. Therefore, when a String method used that always returns a new String instead of modifying the existing. String in java has its all attributes marked as final. If we go for concatenation, another string object will be created and the value is going to assign for that object, so we are going to have two objects.

Because String and its attributes are declared as final, the system can pass the sensitive read-only information without disturbing about alteration.

String Functions Some of the most utilized function of String class are  as follows:

String.length() – to find the length of a string value.

Example:

String Str=“Hello Java”;
Int L=Str.length(); // Value of L = 10

String.charAt(int) – extract a character from the said string from given position.

Example:

String Str=“Hello Java”;
System.out.println(Str.charAt(1)); // Output -> 'e'

String1.equals(String2) – check equality between two string and return a Boolean value as result.

Example:

String Str=“Hello”;
System.out.println(Str.equals(“HELLO”));  // Output -> 'false'

String1.equalsIgnorecase(String2) - check equality between two strings while ignoring he case difference and return a Boolean value as result.

Example:

String Str=“Hello”;
System.out.println(Str.equalsIgnorecase(“HELLO”)); //Output -> 'true'

String1.compareTo(String2) – Compare between two string( relevant to the ASCII values of each character), if both equal it return 0, if String1 is smaller than String2 then it will return a negative value and if  String1 is bigger than String2 then it will return a positive value.

Example:

String Str=“Hello”;
System.out.println(Str.comapreTo(“HELLO”)); // "e - E" means 101 - 69 = -32

// it will return 32 as Ascii value of ‘e’ is 101 and the Ascii  value of 'E' is 69 is and difference is 32, therefore the value store in variable ‘Str’ is bigger than the other string.

compareToIgnoreCase(String str) 

int compareToIgnoreCase(String str) compares two strings while ignoring the case or rather compare two string lexicographically.

Example:

String Str="This is Test";
String Str1="THIS IS TEST";
int t=Str.compareToIgnoreCase(String Str1);//return 0.


String.substring(int) – Extract all the string from given position until end.

Example:

String Str=I love my contry”;
System.out.println(Str.substring(7)); // Output :  my country

String.substring(int,int) - Extract all the string from starting position up to left of end position.

Example:

String Str=I love my contry”;
System.out.println(Str.substring(0,6)); // Output :  I love

Thursday, March 6, 2014

Character Class




java.lang.Character class having some useful methods. Character class is a wrapper class for character data type.

Following function return type is boolean

isUpperCase() – Return true if given character is in Uppercase or else return false.

isLowerCase() – Return true if given character is in Lowercase or else return false.

isDigit() – Return true if given character is a digit or else return false.

isLetter() – Return true if given character is a alphabet or else return false.

isWhitespace() – Return true if given character is whitespace or else return false.

Java whitespace consists – space, tab (‘\t’),  form feed character (‘\r’).

toUpperCase() – Returns the given character in uppercase form.

toLowerCase() – Returns the given character in Lowercase form.



//Character Class

    public class Character_Fun
        {
            public static void main(String args[])
                {
                    char ch='a';
                    System.out.println("\n\tCharacter '"+ch+"' is a Letter? - "+Character.isLetter(ch));
                    ch='a';
                    System.out.println("\tCharacter '"+ch+"' is Lowercase? - "+Character.isLowerCase(ch));
                    ch='A';
                    System.out.println("\tCharacter '"+ch+"' is Uppercase? - "+Character.isUpperCase(ch));
                    ch='\t';
                    System.out.println("\tCharacter '"+ch+"' is WhiteSpace? - "+Character.isWhitespace(ch));
                    ch='9';
                    System.out.println("\tCharacter '"+ch+"' is Digit? - "+Character.isDigit(ch));
                    ch='a';
                    System.out.println("\tCharacter '"+ch+"' Convert to Uppercase - "+Character.toUpperCase(ch));
                    ch='A';
                    System.out.println("\tCharacter '"+ch+"'  Convert to Lowercase - "+Character.toLowerCase(ch));
                   
               }
       }



Output



          Character 'a' is a Letter? - true
          Character 'a' is Lowercase? - true
          Character 'A' is Uppercase? - true
          Character '   ' is WhiteSpace? - true
          Character '9' is Digit? - true
          Character 'a' Convert to Uppercase - A
          Character 'A'  Convert to Lowercase – a