Strings in Java

Strings, which are generally utilized as a part of Java, for writing computer programs, are a grouping of characters. In the Java programming language, strings are like everything else, objects. The Java platform provides the String class to make and control strings.

Instantiating Strings

The most appropriate approach to make a string is to use the following statement: 

String mystring = “Hi world!”;

At whatever point it experiences a string exacting in your code, the compiler makes a String object with its value for this situation, “Hi world!’.

Similarly as with other objects, you can make Strings by utilizing a constructor and a new keyword. The String class has eleven constructors that permit you to give the starting estimation of the string utilizing diverse sources, for example, a cluster of characters.

public class myStringdemo {
  public static void main(string args[]) {
    char[] myarray = {‘h’,‘i’,‘.’};
    String mystring = new String(myarray);
    System.out.println(mystring);
  }
}

This would deliver the accompanying result: 

hi.

Note: The String class is changeless, so that once it is made a String object, its type can’t be changed. In the event that there is a need to make a great deal of alterations to Strings of characters, then you ought to utilize String Buffer & String Builder Classes.

Determining String Length

Routines used to get data about an object are known as accessor methods. One accessor technique that you can use with strings is the length() function, which furnishes a proportional payback of characters contained in the string item. This function can be utilized in the following manner:

public class mystring {
  public static void main(string args[]) {
    String newstr = “I am hungry!”;
    int strlen = newstr.length();
    System.out.println(“length = ”+strlen);
  }
}

This would create the accompanying result:

length = 12

How to Concatenate Strings

The String class incorporates a function for connecting two strings: mystring1.concat(mystring2);

This returns another string that is mystring1 with mystring2 added to it toward the end. You can likewise utilize the concat() system with string literals, as in:

“My name is “.concat(“mary”);

Strings are all the more usually concatenated with the + administrator, as in:

“Hi,” + ” world” + “!”

which brings about:

“Hi, world!”

Sample Implementation:

public class MyString {
  public static void main(string args[]) {
    String mystr = “Sorry”;
    System.out.println(“I” + “am” + mystr);
  }
}

This would deliver the accompanying result:

I am Sorry

Format Strings

You have format() and printf() functions to print the output with designed numbers. The function format() of the String class returns a String object as against a Printstream object. This function creates a formatted string that can be reused. This function can be used in the following manner:

String fstr;
fstr = String.format(“Float variable value ” + “%f, and Integer value ” + “variable is %d, and the contents of the string is ” + ” %s”, fVar, iVar, sVar);
System.out.println(fstr);

String Methods

This section contains a list of methods that are available as part of the String class.

  • int compareTo(Object obj) – This function compares the specified string with the object concerned.
  • char charAt(int chindex) – This function returns the char present at the index value ‘index.’
  • int compareToIgnoreCase(String mystr) – This function performs the lexographic comparison of the two strings. However, the case differences are ignored by this function.
  • int compareTo(String aString) – This function performs the lexographic comparison between the strings.
  • boolean contentEquals(StringBuffer strb) – This function checks if the string is same as the sequence of characters present in the StringBuffer. It returns true on success and false on failure.
  • String concat(String strnext) – This function appends the string with another string at the end.
  • static String copyValueOf(char[] mydata, int xoffset, int xcount) – This function returns a stringf, which is indicative of the character sequence in the original string.
  • static String copyValueOf(char[] newdata) – This function copies the string of characters into a character buffer in the form of a sequence of characters.
  • boolean equals(Object aObject) – This function compares the object with the string concerned.
  • boolean endsWith(String newsuffix) – This function appends the string with the specified suffix.
  • byte getBytes() – Using this function, the string can be encoded into bytes format, which are stored in a resultant array.
  • boolean equalsIgnoreCase(String aString) – This function makes a comparison of the two strings without taking the case of characters into consideration.
  • void getChars(int srcBegin, int sourceEnd, char[] dst, int destinationBegin) – This function copies characters from the specified beginning character to the end character into an array.
  • byte[] getBytes(String charsetnm) - Using this function, the string can be encoded into bytes format using named char set, which are stored in a resultant array.
  • int indexOf(int charx) – This function returns the index of first character that is same as the character specified in the function call.
  • int hashCode() – Ahash code is returned by this string.
  • int indexOf(String newstr) - This function returns the index of the first occurrence of a substring in a string.
  • int indexOf(int charx, int fromIndexloc) – This function returns the index of first character that is same as the character specified in the function call. The search starts from the specified index.
  • String intern() – A canonical representation of a string object given in the function call is returned.
  • int indexOf(String newstr, int fromIndexloc) - This function returns the index of the first occurrence of a substring in a string. The search starts from the specified index.
  • int lastIndexOf(int charx, int fromIndexloc) - This function makes a search for a character from the specified index and returns the index where the last occurrence is found.
  • int lastIndexOf(int charx) - This function makes a search for a character backwards and returns the index where the last occurrence or first occurrence in a backward search is found.
  • int lastIndexOf(String newstr, int fromIndexloc) – This function makes a search for a sub- string from the specified index and returns the index where the last occurrence is found.
  • int lastIndexOf(String newstr) - This function makes a search for a sub-string backwards and returns the index where the last occurrence or first occurrence in a backward search is found.
  • boolean matches(String aregex) - – This function checks for equality between a string region and a regular expression.
  • int length() – This function calculates and returns the string length.
  • boolean regionMatches(int totaloffset, String otherstr, int otheroffset, int strlen) – This function checks for equality between string regions.
  • boolean regionMatches(boolean ignorecharcase, int totaloffset, String otherstr, int otheroffset, int strlen) – This function checks for equality between string regions.
  • String replace(char oldCharx, char newCharx) - This function looks for a substring that matches the regular expression and then replaces all the occurrences with the specified string. The function returns the resultant string, which is obtained after making all the replacements.
  • String replaceFirst(String newregex, String newreplacement) – This function looks for a substring that matches the regular expression and then replaces the first occurrence with the specified string.
  • String replaceAll(String newregex, String xreplacement) - This function looks for a substring that matches the regular expression and then replaces all the occurrences with the specified string.
  • String[] split(String newregex, int xlimit) – This function performs splitting of the string according to the regular expression with it and the given limit.
  • String[] split(String newregex) - This function performs splitting of the string according to the regular expression with it.
  • boolean startsWith(String newprefix, int totaloffset) – This function checks if the given string has the prefix at the specified index.
  • boolean startsWith(String newprefix) – This function checks if the given string begins with the prefix sent with the function call.
  • String substring(int beginIndexloc) - This function returns a string, which is substring of the specified string. The substring is determined by the beginning index to the end of the string.
  • CharSequence subSequence(int beginIndexloc, int endIndexloc) - This function returns a character sequence, which is sub-character sequence of the specified character sequence. The substring is determined by the beginning and ending indexes.
  • char[] toCharArray() – This function performs the conversion of a string to a character array.
  • String substring(int beginIndexloc, int endIndexloc) – This function returns a string, which is substring of the specified string. The substring is determined by the beginning and ending index.
  • String toLowerCase(Locale localenew) - This function converts all the characters in the specified string to lower case using given locale rules.
  • String toLowerCase() - This function converts all the characters in the specified string to lower case using default locale rules.
  • String toUpperCase() - This function converts all the characters in the specified string to upper case using default locale rules.
  • String toString() – This function returns the string itself.
  • String toUpperCase(Locale localenew) – This function converts all the characters in the specified string to upper case using locale rules.
  • static String valueOf(primitive data type x) – A string representation is returned by this function.
  • String trim() – Omits the whitespace that trails and leads a string.