Strings

26 11 2008

Java’s String class allow programmers to search strings,extract parts of them, compare them, etc. There are seven String constructors in java,

  • String str1 = new String();
  • String str2 = new String(“Sample String”);
  • char charArray[] = {‘S’, ‘t’, ‘r’, ‘i’, ‘n’ ,’g'};
  • String str3 = new String(charArray);
  • String str4 = new String(charArray, 2, 3);
  • StringBuffer buf = new StringBuffer(“buffer”);
  • String str5 = new String(buf);

In the first example, str1 is created as an empty string.The second string, str2, will hold the text “A New String”. The next two examples, str3 and str4, are constructed from a character array. In the case of str3, the entire array is placed in the string. For str4, three characters starting in array position two are copied into
the string. Because Java arrays are zero-based, this results in str4 containing “ray”. In the final example, the string str5 is constructed from a StringBuffer.

Advertisement

Actions

Information

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s




Follow

Get every new post delivered to your Inbox.