Java Strings Class

Strings are a sequence of characters. For example:

String greeting = "Hello Java!";

compiler creates a String object with its value in this case, “Hello Java!’. The String class has 11 constructors that allow you to provide the initial value of the string using array of characters.

Example:

public class StringDemo {

   public static void main(String args[]) {
      char[] finalArray = { 'j', 'a', 'v', 'a', '!', '.' };
      String finalString = new String(finalArray);  
      System.out.println( finalString );
   }
}

output

java!.

String Length

Methods used to obtain information about an object are known as accessor methods. One accessor method that you can use with strings is the length() method.which returns the number of characters contained in the string object.

Example:

public class StringDemo {

   public static void main(String args[]) {
      String palindrome = "ohh look at this face";
      int len = palindrome.length();
      System.out.println( "String Length is : " + len );
   }
}

output

String Length is : 21

Concatenating Strings

a method for concatenating two strings:

string1.concat(string2);

You can also use the concat() method with string literals, as in:

My name is ".concat("Jhonson");

Strings are more commonly concatenated with the + operator, as in:

Hello," + " Java" + "!"

which results in:

Hello, Java!"

See this example:

public class StringDemo {

   public static void main(String args[]) {
      String string1 = "look at this ";
      System.out.println("ohh " + string1 + " face");
   }
}

Output:

ohh look at this face

Format Strings

The String class has an equivalent class method, format(), that returns a String object rather than a PrintStream object. Using String’s static format() method allows you to create a formatted string that you can reuse, as opposed to a one-time print statement. For example, instead of:

System.out.printf("The value of the float variable is " +
                  "%f, while the value of the integer " +
                  "variable is %d, and the string " +
                  "is %s", floatVar, intVar, stringVar);

you can also write:

String fs;
fs = String.format("The value of the float variable is " +
                   "%f, while the value of the integer " +
                   "variable is %d, and the string " +
                   "is %s", floatVar, intVar, stringVar);
System.out.println(fs);

String method

char charAt(int index) – This method Returns the character at the specified index.
int compareTo(Object o) – This method Compares this String to another Object.
int compareTo(String anotherString) – This method Compares two strings lexicographically.
int compareToIgnoreCase(String str) – This method Compares two strings lexicographically, ignoring case differences.
String concat(String str) – This method Concatenates the specified string to the end of this string.
boolean contentEquals(StringBuffer sb) – Returns true if and only if this String represents the same sequence of characters as the specified StringBuffer.
static String copyValueOf(char[] data, int offset, int count) – Returns a String that represents the character sequence in the array specified.
boolean endsWith(String suffix) Tests if this string ends with the specified suffix.
boolean equals(Object anObject) – This method Compares this string to the specified object.
boolean equalsIgnoreCase(String anotherString) – Compares this String to another String, ignoring case considerations.
byte getBytes() – Encodes this String into a sequence of bytes using the platform’s default charset, storing the result into a new byte array.
byte[] getBytes(String charsetName) – Encodes this String into a sequence of bytes using the named charset, storing the result into a new byte array.
int hashCode() – Returns a hash code for this string.
int indexOf(int ch, int fromIndex) Returns the index within this string of the first occurrence of the specified character, starting the search at the specified index.
String intern() Returns the index within this string of the first occurrence of the specified substring.
int indexOf(String str, int fromIndex) Returns a canonical representation for the string object.
int lastIndexOf(int ch) – Returns the index within this string of the last occurrence of the specified character.
int lastIndexOf(String str) – Returns the index within this string of the rightmost occurrence of the specified substring.
int length() Returns the length of this string.
boolean matches(String regex) – Tells whether or not this string matches the given regular expression.
boolean regionMatches(boolean ignoreCase, int toffset, String other, int ooffset, int len) – Tests if two string regions are equal.
String replace(char oldChar, char newChar) – Returns a new string resulting from replacing all occurrences of oldChar in this string with newChar.
String replaceAll(String regex, String replacement) – Replaces each substring of this string that matches the given regular expression with the given replacement.
String replaceFirst(String regex, String replacement) – Replaces the first substring of this string that matches the given regular expression with the given replacement.
String[] split(String regex) – Splits this string around matches of the given regular expression.
String[] split(String regex, int limit) – Splits this string around matches of the given regular expression.
boolean startsWith(String prefix) – Tests if this string starts with the specified prefix.
boolean startsWith(String prefix, int toffset) – Tests if this string starts with the specified prefix beginning a specified index.
String substring(int beginIndex) – Returns a new string that is a substring of this string.
String substring(int beginIndex, int endIndex) – This method Returns a new string that is a substring of this string.
char[] toCharArray() – Converts this string to a new character array.
String toLowerCase() – Converts all of the characters in this String to lower case using the rules of the default locale.
String trim() Returns a copy of the string, with leading and trailing whitespace omitted.

Related Post