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

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”;

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”);

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

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

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

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);
Output: ABCDDF
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);
Output: CDE
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
     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");
}catch(UnsupportedEncodingException e){System.out.println(e);}
This constructor throws an exception.
Creating string of whole byte array
This is same as above only difference is that we passing complete byte array in the constructor.
     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");
}catch(UnsupportedEncodingException e){System.out.println(e);}

Crating string using array of Unicode characters

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

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

        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");
        }catch(UnsupportedEncodingException e){System.out.println(e);}
        int unicodes[]={'\u0041','\u0042','\u0043','\u0044','\u0045','\u0046'};
        String str8=new String(unicodes,0,5);
        String str11=new String(str8);
        StringBuffer sb=new StringBuffer("Hemraj");
        String str12=new String(sb);
        StringBuilder sbu=new StringBuilder("Hemraj");
        String str13=new String(sbu);


This is first string
This is second string


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 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

In Process Mode of HSQLDB in web application.

If you want to use the database into your web application, you can use the HSQLDB in In_Process mode. In this mode, you can embed the HSQLDB into your web application and it runs as a part of your web application programm in the same JVM. In this mode, the database does not open any port to connect to the application on the hosing machine and you don't need to configure anything to access it. Database is not expposed to other application and can not be accessed from any dabase tools like dbVisualizer etc. In this mode ,database will be unknown from any other person except you. But in the 1.8.0 version, you can use Server intance for external as well as in process access.  To close the databse, you can issue SHUTDOWN command as an SQL query.   In the in-process mode, database starts from JDBC with the associated databse file provided through  connection URL. for example   DriverManager.getConnection("jdbc:hsqldb:mydatabase","SA","");   Here myd