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

    System.out.println(str);
    System.out.println(strChar);
    System.out.println(strSubChar);
    System.out.println(strByte);
    System.out.println(strByteSub);
  }
}

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)


    22 comments:

    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

      ReplyDelete
    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!
      Thanks!

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

      .. hydroxatone

      ReplyDelete
    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

      ReplyDelete
    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

      ReplyDelete
    5. 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

      ReplyDelete
    6. 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

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

      ReplyDelete
    8. 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

      ReplyDelete
    9. 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://www.privly.org/

      ReplyDelete
    10. 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

      ReplyDelete
    11. 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

      ReplyDelete
    12. 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

      ReplyDelete
    13. 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

      ReplyDelete
    14. 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

      ReplyDelete
    15. 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

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

      my blog post: Le Parfait Reviews

      ReplyDelete
    17. 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.
      Cheers!

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

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

      ReplyDelete
    19. 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

      ReplyDelete
    20. SAP S/4HANA provides businesses with a wide range of collaboration options that can help them streamline their operations, reduce costs, and improve customer satisfaction. From procurement and finance to supply chain and sales, SAP S/4HANA offers a comprehensive set of collaboration tools that can help businesses stay ahead of the competition and drive growth in the digital age. For more information, visit our website SAP Consulting Services or call us @ 1-289-952-8845.

      ReplyDelete