Java – String

java string

Introduction for String

String This is a ‘Non-primitive’ Data Type for Java. String; There is a group of more than one character. String is written inside double quotes (“”).
String these class; java.lang is defined in this packageIn C and C ++, character data type and array were used for String. But String object is created in Java.
String is also created in Java like C and C ++.

For example

Source Code:

public class Sample {  
public static void main (String args []) {  
char str1 [] = {'H', 'e', ​​'l', 'l', 'o', '', 'W', 'o', 'r', 'l', 'd'}; 
String s = new String (str1);
       // both are same.
String str2 = "Hello World";
 System.out.println ("String 1:" + s);
 System.out.println ("String 2:" + str2);
        }
}  

Output:

String 1: Hello World
String 2: Hello World
String Literal is close in double quotes (“”).

For eg.

String str = "Hello World";

String Object; How is the store done?

When string literal is used with string object, then string literal; There is a store on string pool.
String Pool This is a special memory to store string object.

for eg.

String str1 = "Hello World";
In the following program, two different objects are stored on the string pool.

for eg.

String str1 = "Hello";
String str2 = "Hello World"
When two objects are created with the same string, then both objects return the same string literal from the string pool.

for eg.

String str1 = "Hello World";
String str2 = "Hello World";
There are many methods for String class in Java.
Java String Methods
Return type and String MethodsDescription
boolean contentEquals (StringBuffer strbuff)String is compared to string buffer and this method returns the boolean value.
boolean endsWith (String suffix)In string, the suffix of end is returned to boolean value.
boolean equals (Object obj)The two string is compared and boolean value; Return occurs.
boolean equalsIgnoreCase (String str)The two string is compared and boolean value; Return occurs.
boolean matches (string regex)String boolean value according to whether or not a given regular expression matches. Return occurs.
boolean regionMatches (boolean ignoreCase, int startIndex, string str, int str_startIndex, int len)The substring of two string or string is compared.
boolean regionMatches (int startIndex, String str, int str_StartIndex, int len)The substring of two string or string is compared.
boolean startsWith (String prefix)boolean value by checking string prefix; Returns are made.
boolean startsWith (String prefix, int startIndex)boolean value by checking string prefix; Returns are made.
[byte] getBytes ()Byte array is returned from string.
char charAt (int index)Returns the character at the given index.
char [] toCharArray ()The string is converted into a character array.
CharSequence subSequence (int beginIndex, int endIndex)The character sequence is returned.
int compareTo (Object obj)String is compared to another object.
int compareTo (String str2)String is compared to another string.
int compareToIgnoreCase (String str2)String is compared to another string.
int indexOf (String str)index of string character or substring; Return is done in integer.
int indexOf (String str, int fromIndex)index of string character or substring from starting Index; Return is done in integer.
int indexOf (int ch)index of string character; Return is done in integer.
int indexOf (int ch, int fromIndex)index of string character or substring from starting Index; Return is done in integer.
int lastIndexOf (String str)index of string or substring; Return is done in integer.
int lastIndexOf (String str, int fromIndex)index of string character or substring from starting Index; Return is done in integer.
int lastIndexOf (int ch)index of string character; Return is done in integer.
int lastIndexOf (int ch, int fromIndex)index of string character from starting Index; Return is done in integer.
int length ()The length of string is returned in integer.
void getChars (int srcBegin, int srcEnd, char [] dest, int destBegin)string; The character array is copied.
static String copyValueOf (char [] data)character array; string is returned.
static String copyValueOf (char [] data, int offset, int count)character array; string is returned.
static String valueOf (basic data type)character array; string is returned.
String concat (String str)Different types are converted to string.
String intern ()The canonical representation of string returns.
String replace (char oldChar, char newChar)The string is replaced with the given character.
String replaceAll (String regex, String replacement)Replaces string with regular expression.
String replaceFirst (String regex, String replacement)Replaces string with regular expression.
String substring (int beginIndex)The substring is made from string.
String substring (int beginIndex, int endIndex)The substring is made from string.
String toLowerCase ()Converts all uppercase letters to lowercase.
String toUpperCase ()Converts all lowercase letters to uppercase.
String trim ()The beginning and end whitespaces are removed.
String [] split (String regex)The array of that string by creating a separate substring from the regular expression of the string; Return is done.
String [] split (String regex, int limit)The array of that string by creating a separate substring from the regular expression of the string; Return is done.