Using Strings in java

January 7, 2009 · By mahesh 

In this article, we will discuss how you can work with strings in java. We’ll look at some of the methods you can use with string class object & we’ll take sneak preview of StringBuffer class and it’s method as well.

What are strings?
Strings are chain of characters processed as one unit. Strings are immutable objects. That means you can’t change the string objects. String reference variables can change. String class is final that means you can’t override its methods.

Some points to remember about strings:

# String objects holds chain of text/data that can’t be changed hence objects are immutable.

# In java, String related operations can be handled using String, StringBuffer and StringBuilder.

# StringBuffer is thread-safe and synchronizing hence it is preferred in multi-threaded application while StringBuilder isn’t synchronized and hence not thread-safe.

# StringBuffer and StringBuilder both are mutable. In StringBuffer equals () method is not overridden so it doesn’t compare values.

# StringBuilder methods should runs faster than StringBuffer methods. Some methods for StringBuffer and StringBuilder are similar.

Let’s cut some code for you so that you can understand the concepts easily. First, let’s check out what methods are available for use to play with string class.

Creating String:
You can create string using these ways.

String st=”HelloWorld”;
String st=”new String(“Helloworld”);

Some other methods that you can use from string class.

Getting string length
Use length() method to get the length of string.

String st=”First Class”;
System.out.println(st.length());

String Concatenation
You can use concat() to merge to strings. You can use + sign as well.

String st=”Hello”
String sp=st+”World”;
System.out.println(sp);
String sn=st.concat(“Universe”);

Replacing strings
To replace character or whole string you can use replace () method.

String st=Bats eat rats”;
System.out.println(st.replace(‘B’,’C’);

Uppercase and lowercase conversion
You can convert the letters from upper to lowercase and lowercase to uppercase using toLowerCase() and toUpperCase() methods respectively.

System.out.println(“notice”.toUpperCase());
System.out.println(“DON’T SHOUT ON IRC”.toLowerCase());

StringBuffer class features

It allows you the ability to modify strings which is not possible in case of string class.

StringBuffer sb=new StringBuffer(“Cat & Rat”);
sb.replace(‘3’,’5’,”vs”);
System.out.println(sb);

Setting and getting length
You can set the length of StringBuffer using setLength() and get the length using legth() method.

sb.length();
sb.setLength(1000);

Usage of toString() method
This method returns the value of StringBuffer object that invoked the method call as a string.

StringBuffer sb=new StringBuffer(“Bat hate Cate”);
System.out.println(sb.toString());

You can explore more about StringBuffer methods like insert(), append(), and delete() if you want to get into depth of that class.

I hope the information above helped you to understand the some basics of string and StringBuffer class. If you’ve any questions, feedback and queries related to this article the please feel free to post it in comments.

Comments

One Response to “Using Strings in java”

  1. Java : Onecore on January 18th, 2009 10:22 am

    [...] Strings in Java [...]

Leave a Reply