Java String Class and its methods
Java String provides a lot of concepts that can be performed on a string such as compare, concat , equals, split, length, replace, compareTo, intern, substring etc.
In java, string is basically an object that represents sequence of char values.
Creating a String
There are two ways to create a String in Java
- String literal
- Using new keyword
String literal
In java, Strings can be created like this: Assigning a String literal to a String instance:
String string = "Welcome"; String string1 = "Here";
The problem with this approach: As I stated in the beginning that String is an object in Java. However we have not created any string object using new keyword above. The compiler does that task for us it creates a string object having the string literal (that we have provided , in this case it is “Welcome”) and assigns it to the provided string instances.
But if the object already exist in the memory it does not create a new Object rather it assigns the same old object to the new instance, that means even though we have two string instances above(string and string1) compiler only created on string object (having the value “Welcome”) and assigned the same to both the instances. For example there are 10 string instances that have same value, it means that in memory there is only one object having the value and all the 10 string instances would be pointing to the same object.
What if we want to have two different object with the same string? For that we would need to create strings using new keyword.
Using New Keyword
As we saw above that when we tried to assign the same string object to two different literals, compiler only created one object and made both of the literals to point the same object. To overcome that approach we can create strings like this:
String string = new String("Hi"); String string1 = new String("Hi");
In this case compiler would create two different object in memory having the same text.
String Methods
- char charAt(int index):
- int codePointAt(int index):
- void getChars(int srcBegin, int srcEnd, char[] dest, int destBegin):
- boolean equals(Object obj):
- boolean contentEquals(StringBuffer sb):
- boolean equalsIgnoreCase(String string):
- int compareTo(String string):
- int compareToIgnoreCase(String string):
- boolean regionMatches(int srcoffset, String dest, int destoffset, int len):
- boolean regionMatches(boolean ignoreCase, int srcoffset, String dest, int destoffset, int len):
- boolean startsWith(String prefix, int offset):
- boolean startsWith(String prefix):
- boolean endsWith(String suffix):
- int hashCode():
- int indexOf(int ch):
- int indexOf(int ch, int fromIndex):
- int lastIndexOf(int ch):
- int lastIndexOf(int ch, int fromIndex):
- int indexOf(String str):
- int lastindexOf(String str):
- String substring(int beginIndex):
- String substring(int beginIndex, int endIndex)
- String concat(String str)
- String replace(char oldChar, char newChar)
- boolean contains(CharSequence s)
- String replaceFirst(String regex, String replacement)
- String replaceAll(String regex, String replacement)
- String[] split(String regex, int limit)
- String[] split(String regex)
- String toLowerCase(Locale locale)
- String toLowerCase()
- String toUpperCase(Locale locale)
- String toUpperCase()
- String trim()
- char[] toCharArray()
- static String copyValueOf(char[] data)
- static String copyValueOf(char[] data, int offset, int count)
- static String valueOf(data type)
- byte[] getBytes(String charsetName)
- byte[] getBytes()
- int length()
- boolean ()
Comments
Post a Comment