Thursday, 17 August 2017

Java String

The Java String data type can have a sequence (string) of characters, like pearls on a string. Strings are how work with text in Java.

Strings in Java are symbolizes inside using bytes, encoded as UTF-16. UTF-16 uses 2 bytes to represent a single character. UTF is a character encoding that can represent characters from a lot of different languages (alphabets).

char[] ch={'h’,'e','l','l','o','t','n','c'};  
String s=new String(ch);

There are two ways to create String object:

  • By string literal
  • By new keyword

By string literal :

A String object is an individual instance of the java.lang.String class

String s=" techNewsNcareer ";

By new keyword

it will creates two objects and one reference variable.

String str3 = new String("techNewsNcareer");

Initialization of String:

JVM keep a memory pool for String. When you create a String, first this memory pool is scanned. If the instance previously exists then this new instance is mapped to the already existing instance. If not, a new java String instance is created in the memory pool.

String str1 = "techNewsNcareer";
String srt2 = new String();
String str3 = new String("techNewsNcareer");
String str4 = new String(char[]);
String str5 = new String(byte[]);
String str6 = new String(new StringBuffer());
String str7 = new String(new StringBuilder());

Methods of string class :

The java.lang.String class provides many useful methods to perform operations on sequence of char values.

char charAt(int index)
returns char value for the particular index
int length()
returns string length
static String format(String format, Object... args)
returns formatted string
static String format(Locale l, String format, Object... args)
returns formatted string among given locale
String substring(int beginIndex)
returns substring for given begin index
String substring(int beginIndex, int endIndex)
returns substring for given begin index and end index
boolean contains(CharSequence s)
returns true or false after similar the sequence of char value
static String join(CharSequence delimiter, CharSequence... elements)
returns a joined string
static String join(CharSequence delimiter, Iterable<? extends CharSequence> elements)
returns a joined string
boolean equals(Object another)
checks the equality of string with object
boolean isEmpty()
checks if string is empty
String concat(String str)
concatenates specified string
String replace(char old, char new)
replaces all incident of specified char value
String replace(CharSequence old, CharSequence new)
replaces all occurrences of specified CharSequence
static String equalsIgnoreCase(String another)
compares another string. It doesn't check case.
String[] split(String regex)
returns splitted string matching regex
String[] split(String regex, int limit)
returns splitted string matching regex and limit
String intern()
returns interned string
int indexOf(int ch)
returns specified char value index
int indexOf(int ch, int fromIndex)
returns specified char value index starting with given index
int indexOf(String substring)
returns specified substring index
int indexOf(String substring, int fromIndex)
returns specified substring index starting with given index
String toLowerCase()
returns string in lowercase.
String toLowerCase(Locale l)
returns string in lowercase using specified locale.
String toUpperCase()
returns string in uppercase.
String toUpperCase(Locale l)
returns string in uppercase using specified locale.
String trim()
removes beginning and ending spaces of this string.
static String valueOf(int value)
converts given type into string. It is overloaded.

Here is the example of string class :

public class str_test
   public static void main(String args[])
                String str1 = new String("tech NEWS");
                System.out.println("String length : "+str1.length());
                String str2 = "career";
                String str3 = str1+str2;
                System.out.println("append : "+str3);
                String str4 = str1.concat(str2);
                System.out.println("concat : "+str4);

                String str5 = new StringBuilder(str1).append(str2).toString();
                System.out.println("Append : "+str5);

                String substr = str1.substring(0,5);
                System.out.println("substring : "+substr);
                int index = str1.indexOf(str1);
                System.out.println("indexOf : "+index);

                System.out.println("quals : "+ str1.equals(str2) );

                System.out.println("start with : "+ str1.startsWith("tech")    );

                System.out.println( "end with : "+str1.endsWith  ("news")    );

                String text    = "  And he ran across the field   ";
                String trimmed = text.trim();
                System.out.println("trimmed : "+trimmed);

                String source   = "123abc";
                String replaced = source.replace('a', '@');
                System.out.println("replaced : "+replaced);

                Integer integer = new Integer(123);

                String intStr = integer.toString();
                System.out.println("to string : "+intStr);
                String Struprlwr = "This IS a Tech NEWS n career";

                String uppercase = Struprlwr.toUpperCase();
                String lowercase = Struprlwr.toLowerCase();
                System.out.println("uppercase : "+uppercase);
                System.out.println("lowercase "+lowercase);
String length : 9
append : tech NEWScareer
concat : tech NEWScareer
Append : tech NEWScareer
substring : tech
indexOf : 0
quals : false
start with : true
end with : false
trimmed : And he ran across the field
replaced : 123@bc
to string : 123
lowercase this is a tech news n career

In this tutorial we learned about java string that include char charAt(),length(),substring(),equals(), isEmpty(), concat(), replace(), indexOf(),
toLowerCase(),        toUpperCase(),trim() this function and its working.


Post a Comment