Java Strings Class

In this article we are reading about Java Strings Class.
Strings, which are widely used in Java programming, are a sequence of characters. In Java programming language, strings are treated as objects.
The Java platform provides the String class to create and manipulate strings.

Creating Strings

There are two ways to create string in Java:
  • String literal
String s = “SixwaresTechForum”;
  • Using new keyword
String s = new String (“SixwaresTechForum”);

String Methods
  1. int length(): Returns the number of characters in the String.
    "SixwaresTechForum".length();  // returns 16
  2. Char charAt(int i): Returns the character at ith index.
    "SixwaresTechForum".charAt(3); // returns  ‘w’
  3. String substring (int i): Return the substring from the ith  index character to end.
    "SixwaresTechForum".substring(3); // returns “waresTechForum”
  4. String substring (int i, int j): Returns the substring from i to j-1 index.
     "SixwaresTechForum".substring(2, 5); // returns “xwa”
  5. String concat( String str): Concatenates specified string to the end of this string.
     String s1 = ”Sixwares”;
     String s2 = ”TechForum”;
     String output = s1.concat(s2); // returns “SixwaresTechForum”
    
  6. int indexOf (String s): Returns the index within the string of the first occurrence of the specified string.
     String s = ”Sixwares Tech Forum”;
     int output = s.indexOf(“Tech”); // returns 9
    
  7. int indexOf (String s, int i): Returns the index within the string of the first occurrence of the specified string, starting at the specified index.
     String s = ”Sixwares Tech Forum”;
     int output = s.indexOf(‘e’,7);// returns 11
    
  8. Int lastindexOf( int ch): Returns the index within the string of the last occurrence of the specified string.
     String s = ”Sixwares Tech Forum”;
     int output = s.lastindexOf(‘e’); // returns 10
    
  9. boolean equals( Object otherObj): Compares this string to the specified object.
     Boolean out = “Sixwares”.equals(“Sixwares”); // returns true
     Boolean out = “Sixwares”.equals(“sixwares”); // returns false
    
  10. boolean  equalsIgnoreCase (String anotherString): Compares string to another string, ignoring case considerations.
     Boolean out= “Sixwares”.equalsIgnoreCase(“Sixwares”); // returns true
     Boolean out = “Sixwares”.equalsIgnoreCase(“sixwares”); // returns true
  11.  int compareTo( String anotherString): Compares two string lexicographically.
     int out = s1.compareTo(s2);  // where s1 ans s2 are
                                 // strings to be compared
    
     This returns difference s1-s2. If :
     out < 0  // s1 comes before s2
     out = 0  // s1 and s2 are equal.
     out >0   // s1 comes after s2.
    
  12. int compareToIgnoreCase( String anotherString): Compares two string lexicographically, ignoring case considerations.
     int out = s1.compareToIgnoreCase(s2);  
    // where s1 ans s2 are 
    // strings to be compared
    
     This returns difference s1-s2. If :
     out < 0  // s1 comes before s2
     out = 0   // s1 and s2 are equal.
     out >0   // s1 comes after s2.
    
    Note- In this case, it will not consider case of a letter (it will ignore whether it is uppercase or lowercase).
  13. String toLowerCase(): Converts all the characters in the String to lower case.
    String word1 = “HeLLo”;
    String word3 = word1.toLowerCase(); // returns “hello"
    
  14. String toUpperCase(): Converts all the characters in the String to upper case.
    String word1 = “HeLLo”;
    String word2 = word1.toUpperCase(); // returns “HELLO”
    
  15. String trim(): Returns the copy of the String, by removing whitespaces at both ends. It does not affect whitespaces in the middle.
    String word1 = “ Sixwares Tech Forum “;
    String word2 = word1.trim(); // returns “Sixwares Tech Forum”
    
  16.  String replace (char oldChar, char newChar): Returns new string by replacing all occurrences of oldChar with newChar.
    String s1 = “SixwaresTechForum“;
    String s2 = “SixwaresTechForum”.replace(‘e’ ,’f’); // returns “SixwarfsTfchForum”
    
    Note:- s1 is still SixwaresTechForum and s2 is SixwarfsTfchForum
So in this article we have read about “Java Strings Class”.

Comments

Popular posts from this blog

Java Number Class

Java Character Class

What are Web Resources in Microsoft Dynamics 365?