Skip to main content

String hadnling

String is the sequence of character represented by the object of String class in java. Two other classes’ string buffer and string builder can be used for same thing. String object provides some predefined method those are used to manipulate the string.
String object is said to be the immutable this means that you can not manipulate the string object. But you can work with that object. When you manipulate the string object then new string object is created and it consists of manipulated contents. But it is hard to observe by programmer without experience.
Here to solve this problem you can use the String buffer class to create the string object in which you can manipulate the string contents with the same object. That is the object of string class the mutable.
String buffer is the thread safe.

Another class to create the mutable string objects is string builder class. But it is no thread safe.

Creating String

There are many ways to create the string object

1
You can declare string reference variable and assign the string lateral to it, then string object is created automatically and the reference of that object is stored in the reference variable you declared. for example
String str=”this my first string”;

2
You can create the string object using new operator. What you want to write in string is passed in the constructor of string class. there are 11 constructor those are used to create the string through different ways
String str=new String(“this is my first string”);

3
Creating the string using character array
char[] chs = { 'h', 'e', 'l', 'l', 'o', '.'};
String srt = new String(chs);

4
Creating the string using sub character array
char[] chs = { 'h', 'e', 'l', 'l', 'o', '.'};
String srt = new String(chs,2,2);

5
If you leave the string constructor blank, the n it creates the empty string.
String str=new String();

6
There is the constructor  String(byte[] bytes) which can Constructs a new String by decoding the specified array of bytes using the platform's default charset. The length of the new String is a function of the charset, and hence may not be equal to the length of the byte array.
byte b[]={65,66,67,68,68,70};
String str3=new String(b);
System.out.println(str3);
Output: ABCDDF
7
Creating the string of the sub array of byte from specified position of specified length using the constructor  String(byte[] bytes, int offset, int length)

byte b[]={65,66,67,68,68,70};
String str6=new String(b,2,3);
System.out.println(str6);
Output: CDE
8
Creating string from sub array byte by decoding the bytes using specified CharSet. Some Character set are following.

US-ASCII Seven-bit ASCII, a.k.a. ISO646-US, a.k.a. the Basic Latin block of the Unicode character set
ISO-8859-1   ISO Latin Alphabet No. 1, a.k.a. ISO-LATIN-1
UTF-8 Eight-bit UCS Transformation Format
UTF-16BE Sixteen-bit UCS Transformation Format, big-endian byte order
UTF-16LE Sixteen-bit UCS Transformation Format, little-endian byte order
UTF-16 Sixteen-bit UCS Transformation Format, byte order identified by an optional byte-order mark
Try{
     byte b7[]={65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80};
     String str7=new String(b7,0,13,"ISO-8859-1");
     System.out.println(str7);
}catch(UnsupportedEncodingException e){System.out.println(e);}
This constructor throws an exception.
9
Creating string of whole byte array
This is same as above only difference is that we passing complete byte array in the constructor.
Try{
     byte b7[]={65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80};
     String str7=new String(b7,"ISO-8859-1");
     System.out.println(str7);
}catch(UnsupportedEncodingException e){System.out.println(e);}

10
Crating string using array of Unicode characters

int unicodes[]={'\u0041','\u0042','\u0043','\u0044','\u0045','\u0046'};
String str8=new String(unicodes,0,5);
System.out.println(str8);
Output: ABCDE
11
Creating string using anther string
        String str1="This is first string";       
        String str2=new String(str1);
        System.out.println(str2);
12
Creating string from string buffer
                        StringBuffer sb=new StringBuffer("Hemraj");
                        String str12=new String(sb);
                        System.out.println(str12);
13
Creating string from string builder
                        StringBuilder sbu=new StringBuilder("Hemraj");
                        String str13=new String(sbu);
                        System.out.println(str13);


class MyString
{
    public static void main(String args[])
    {
        String str1="This is first string";
        System.out.println(str1);
        
        String str2=new String("This is second string");
        System.out.println(str2);
        
        byte b[]={65,66,67,68,69,70};
        String str3=new String(b);
        System.out.println(str3);
        
        
        String str6=new String(b,2,3);
        System.out.println(str6);

        byte b7[]={65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80};
        try{
            String str7=new String(b7,0,13,"ISO-8859-1");
            System.out.println(str7);
        }catch(UnsupportedEncodingException e){System.out.println(e);}
        
        int unicodes[]={'\u0041','\u0042','\u0043','\u0044','\u0045','\u0046'};
        String str8=new String(unicodes,0,5);
        System.out.println(str8);
        
        String str11=new String(str8);
        System.out.println(str11);
        
        StringBuffer sb=new StringBuffer("Hemraj");
        String str12=new String(sb);
        System.out.println(str12);
        
        StringBuilder sbu=new StringBuilder("Hemraj");
        String str13=new String(sbu);
        System.out.println(str13);
    }
}

Output:

This is first string
This is second string
ABCDEF
CDE
ABCDEFGHIJKLM
ABCDE
ABCDE
Hemraj
Hemraj

Comments

Popular posts from this blog

Using HyperSQL (HSQLDB)

HSQLDB is a portable RDBMS implemented in pure java. It can be embedded with your application as well as can be used separately. It is very a small database that supports almost all features of the standard database system. It comes with small jar file that can be found in lib folder. The HSQLDB jar package is located in the /lib directory of the ZIP package and contains several components and programs. Core components of jar file are : HyperSQL RDBMS Engine (HSQLDB), HyperSQL JDBC Driver, Database Manager, and Sql Tool. Installing and Using Download: download latest release of HyperSQL database from http://hsqldb.org website and extract it. You will see following contents. Here "bin" directory contains some batch files those can be used to run a swing based GUI tool. You can use runManagerSwing.bat to connect to database, but database must be on before running it. Directory lib contains File hsqldb.jar . It is the database to be used by you. Running database First

How to handle values from dynamically generated elements in web page using struts2

Some time you will see the form containing the button " Add More " . This facility is provided for the user to get the values for unknown number of repeating for some information. for example when you are asking to get the projects details from user, you need to put the option to add the more project for the user since you don't known how many projects user have. In the HTML form, you repeat the particular section to get the multiple values for those elements. In Html page , you can put the option to add new row of elements or text fields by writing the java script or using JQuery API. Now, the question is that how to capture the values of dynamically generated text fields on the server. Using the servlet programming you can get the values by using getParameters() method that resultants the array of the parameter having the same name. But this limit you to naming the text fields in the HTML form. To ally this approach, you have to take the same name for t

Generating JTable from database table

package  process; import  javax.swing. * ; import  javax.swing.table. * ; import  java.sql. * ; /**  * This class create JTable from Database table.  * User program needs to specify database connection and corresponding atable name.  * @author Hemraj  */ public   class  TableToJTable {      //private String table;      private   Connection  con;      public   TableToJTable ( Connection  con ){          this .con = con;      }      /**      * This method return JTable object created from Database table having same data asn structure      * as in original table into database.      * @param table Name of the database table to be coverted to JTable      * @return JTable object that consist of data and structure of Database table      * @throws java.lang.Exception Original object is deferent, e.i either SQLException or NullPointerException      */      public   JTable   getTable ( String  table ) throws   Exception {          JTable  t1 = new   JTable () ;          D