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

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

Write a code for servlet which will display all the fields of Employee table in tabular form.MCS-051,Dec09-3a

import javax.servlet.*; import java.io.*; import java.sql.*;   import javax.servlet.http.*;   public class Employee extends HttpServlet   {   public void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException,      ServletException   {   res.setContentType("text/Html");   PrintWriter pw = res.getWriter();   try   {   Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");   Connection con=DriverManager.getConnection("jdbc:odbc:dsn");   Statement st=con.createStatement();   ResultSet rs=st.executeQuery("select emp_id,emp_name,emp_dob,mob_no,email from employee");   String tr="";   while(rs.next()){   tr=tr+"<tr>";   tr=tr+"<td>"+rs.getString(1)+"</td>";   tr=tr+"<td>"+rs.getString(2)+"</td>";   tr=tr+"<td>"+rs.getString(3)+"</td>";   tr=tr+"<td>"+rs.getString(4)+"</td>&