Wednesday, April 4, 2012

JAVA Programming : String Class

JAVA String 

In Java there is eight primitive data type and also a special String class in java.lang.string package for string handling.
String are like sequence of characters. String is not a primitive data type (because strings are objects) but the java language provides some special support to strings so, sometimes its seems like Strings are one of the primitive type.
String are very widely used in java and if you ever done some programming in Java than you must have created  a string in some way or the other.
For example the most direct way to create an String object is by just enclosing some characters in double quotes("").

    String str ="This is a String";    

String is an object in java so it can also be declared as any other object is declared using new keyword.

      String str = new String();     

String class has several constructors to create a String object. The above example is one of the way simplest way to create an string. The constructor used in the example is a no argument constructor or we can say a default constructor which will create a empty String i.e. str will be an instance of String with no character in it.
Below is an example which show a number of ways to create a String object.

public class StringExample {
  public static void main(String[] args) {
    // this is will create an empty String
    String str = new String();

    // Creating a String using an character array
    char ch[] 'J''a''v''a''C''o''d''e''S''p''o''t' };
    String strChar = new String(ch);

    // Creating a String from character array's subarray
    //in arguments give the source character array, 
    //starting point and number of character after that
    String strSubChar = new String(ch, 44);

    //Creating an array using byte array of ASCII Value
    byte b[] ={74,65,86,65};

    String strByte = new String(b);

    //Just like character array we can also create 
    //string form subarray of byte array
    String strByteSub = new String(b,2,2);


Output of Program:

There are some more String Constructors but we will discusses them in later tutorials.

Methods of String Class:- String class provides several methods for string manipulation and these are going to be very useful in many situation like Input validation, text analysis and file conversions etc. In this tutorial we are going to discuses most of the common methods of String class.
  • public charAt(int index) :-  
  • public int length() :-
  • public String concat(String s) :-
  • public boolean equals(String s)
  • public boolean equalsIgnoreCase(String s)
  • public String substring(int begin)
  • public String substring(int begin,int end)
  • public String toLowerCase()
  • public String toUpperCase()
  • public String trim()
  • public int indexOf(int ch)
  • public int indexOf(int ch, int startIndex)
  • public int indexOf(String str)
  • public int indexOf(String str, int startIndex)


    1. Hеllο to every bοԁy, іt's my first pay a visit of this website; this webpage contains awesome and actually fine stuff designed for readers.

      Here is my weblog; hydroxatone

      1. Hi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me.. I am a regular follower of your blog. Really very informative post you shared here. Kindly keep blogging. If anyone wants to become a Java developer learn from Java Training in Chennai. or learn thru Java Online Training in India . Nowadays Java has tons of job opportunities on various vertical industry.

    2. I ωas suggestеd this website by
      meаns of my cousіn. I'm now not certain whether or not this submit is written via him as no one else realize such special approximately my problem. You're inсredible!

      Feel fгеe to surf to mу wеb pаge .

      .. hydroxatone

    3. What's up i am kavin, its my first occasion to commenting anyplace, when i read this post i thought i could also make comment due to this brilliant paragraph.

      Feel free to surf to my blog post ... luminess air

    4. Dοеs your blog havе a contact page?
      I'm having trouble locating it but, I'd like tο shоot уοu an e-mаil.
      I've got some creative ideas for your blog you might be interested in hearing. Either way, great site and I look forward to seeing it grow over time.

      My web blog ... airbrush makeup reviews

    5. very well written and organized tutorials…its indeed a great help for beginners like me to keep up the interest and at the same time learn this important subject.

    6. Hi there, I check your blogs on a regular basis.
      Your story-telling style is awesome, keep doing what you're doing!

      Also visit my web page perfect waist to hip ratio

    7. I rarely leave a response, but i did some searching and wound up here "JAVA Programming : String Class".
      And I actually do have 2 questions for you if you don't mind. Could it be simply me or does it look like a few of these comments come across like they are left by brain dead individuals? :-P And, if you are posting on other sites, I would like to follow anything new you have to post. Would you list of every one of your communal sites like your twitter feed, Facebook page or linkedin profile?

      my site :: Skin care

    8. Hello there! This is my first comment here so I just wanted to give a quick shout out and say
      I genuinely enjoy reading through your articles.
      Can you suggest any other blogs/websites/forums that go over
      the same subjects? Thanks a ton!

      Also visit my blog Xtrasize Supplement

    9. Thanks for your personal marvelous posting! I really enjoyed reading it, you can be a great author.
      I will ensure that I bookmark your blog and definitely will
      come back later on. I want to encourage you to definitely continue your great writing,
      have a nice morning!

      My weblog ... Le parfait skin

    10. Increasingly men and women are adopting this technique which they're finding really very simple to practice.

      Feel free to visit my web blog - Https://

    11. Great blog you have here.. It's difficult to find good quality writing like yours nowadays. I honestly appreciate people like you! Take care!!

      Here is my web blog Beta Force Muscle

    12. Simply want to say your article is as astounding.
      The clearness for your post is just excellent and i can think you're an expert on this subject. Fine with your permission let me to seize your feed to keep up to date with coming near near post. Thank you a million and please keep up the gratifying work.

      My homepage; Download Muscle Maximizer

    13. Thank you for some other great post. The place else may just anybody
      get that type of info in such a perfect way of writing? I
      have a presentation next week, and I am at the look
      for such info.

      Feel free to surf to my website ... Xtrasize Review

    14. Quality content is the secret to interest the viewers to visit the web site,
      that's what this website is providing.

      Here is my webpage ... Garcinia Cambogia

    15. This web site definitely has all the information and facts
      I needed concerning this subject and didn't know who to ask.

      Also visit my blog: Saphire Ecig Review

    16. It's amazing in favor of me to have a website, which is good designed for my knowledge. thanks admin

      my blog ... anti cellulite treatment

    17. Awesome! Its really amazing article, I have got much clear idea regarding from this

      my blog post: Le Parfait Reviews

    18. Please let me know if you're looking for a author for your weblog. You have some really great posts and I feel I would be a good asset. If you ever want to take some of the load off, I'd
      love to write some content for your blog in exchange for a link back to mine.
      Please send me an e-mail if interested.

      Feel free to visit my web site - garcinia cambogia weight loss

    19. Fantastic, I'm enjoyed read this post, Every time search this type of post. Really I enjoyed. This article is really very interesting and effective. I think its must be helpful for us. Thanks for sharing your informative. Buy Twitter Followers

    20. This blog awesome and i learn a lot about programming from here.The best thing about this blog is that you doing from beginning to experts level.

      Love from

    21. Great website for Java Programming.Please try to include more topics on Java Programming. The topic explanation is very good.

    22. Being new to the blogging world I feel like there is still so much to learn. Your tips helped to clarify a few things for me as well as giving..

      VMWare Training in Chennai

      MSBI Training in Chennai

    23. Thanks for the information.I really need that information.It's helps me to learn new things. I want to be like you. keep posting new article . I'm your fan your article...
      Corporate Training Company for Testing