Tuesday, January 12, 2010

Basic String Class Functions In BlueJ

String class is defined in java.lang package. String objects are immutable, means once created their value can not be changed. Although there are several String functions defined in String class which can modify the value of a String object and the modified value is referred by another String object. These String functions are called String manipulating functions.

1. int length () : A function of string class returns the number of characters in the invoking string object, including space.
e.g String str=”this is java’; The output of the statement System.out.println(str); would be 12

2. String toString () function returns string version of any non string value. This function defined in Object class which is defined in java.lang package and this class is super class of all java classes. This function has several overloaded versions.

3. String valueOf () function also returns string version of primitive values. This has also several overloaded versions.

4. char charAt (int index)- This function extract a single character from a string and returns it. This function takes the location or index of the character in the string.
e.g String str=”This is java”;
char ch=str.charAt(2); will return the character ‘i’. Index always starts from 0.

5. boolean equals () - A function of string class takes one string as argument and the function is invoked on another string object. This function compares the argument and the invoking string for equality. If both are same it will return true otherwise false. This method is case sensitive. “JAVA”.equals(“java”); will return false.

6. boolean equalsIgnoreCase () - This function has all the features same as equals () String function excepting it is not case sensitive.

6. boolean startsWith ()- This function takes a string as argument and is invoked on another string. If the invoking string starts with the argument string, it returns true otherwise false.
e.g Burdwan.startsWith(“Bu”); will return true

7. boolean endsWith () function also works like startsWith () method excepting it checks whether the invoking string ends with the argument string.
e.g Burdwan.endsWith(“Bu”); will return false and Burdwan.startsWith(“an”); will return true

Difference between equals () function and == operator.

equals () function compares values of two string object whereas == operator or equality operator checks whether both the objects refers to the same instance.
e.g String str1=”Hi”; and String str2=”Hi”;
In this case str1.equals(str2) will return true but str1==str2 will return false.
Again String str1=”Hi”; and String str2=str1;
Here both str1.equals(str2) and str1==str2 will return true as both str1 and str2 refers the same location.

This page is on String functions. If you have any doubt in any program, pl. feel free to put your comments. I am here to clear your doubts.

Related Post:  BlueJ Programs on String/Sentence

18 comments:

  1. this site rocks,pls keep up the good work :D,its good for ICSE students,pls also add a post for noobs who cant figure the various cmds in bluej like me for eg- double float etc ;)

    ReplyDelete
  2. please i would like to know how can i write a source code to set up a condition like a book refernce number need to be at 3 character

    ReplyDelete
  3. Pl. check this program and give me feed back.


    import java.io.*;
    class BlueJx
    {
    public static void main(String args[])throws IOException
    {
    BlueJx ob=new BlueJx();
    ob.take();
    }
    public void take()throws IOException
    {
    String str="";
    BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
    boolean bool=true;
    while(bool)
    {
    System.out.println("Enter Book Reference Number(Must be of 3 character length):");
    str=br.readLine();
    if(str.length()==3)
    bool=false;
    }
    System.out.println("Book Refernce Number:"+ str);
    }
    }

    ReplyDelete
  4. How do i extract a specific character from a string such that it remains a string and not a character??
    Please help.

    ReplyDelete
  5. Hre is the program


    import java.io.*;
    class CharExtract
    {
    String str1,ch;
    int i,len;
    BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
    public void show()throws Exception
    {
    System.out.println("Enter the sentence:");
    str1=br.readLine();
    len=str1.length();

    for(i=0;i< len-1;i++)
    {
    ch=str1.substring(i,i+1);
    System.out.println("Extracted character="+ch+" and it's length="+ch.length());
    }
    }

    public static void main(String args[])throws Exception
    {
    new CharExtract().show();
    }
    }

    ReplyDelete
  6. plz help me by giving me a list of all the functions used in bluej

    ReplyDelete
  7. There are thousands of functions used in BlueJ. So be specific, if you want know mostly used functions of any specific class then I can help you.

    ReplyDelete
  8. could you help me out with a program where we have to capitalize the first letter of the first name,place a full stop and remove the other letters and same with the second name, but keep the surname as it is.
    eg:- 'Subhash Chandra Bose' should return 'S. C. Bose'

    ReplyDelete
  9. import java.io.*;
    public class NameShort
    {
    String name;
    int i;
    BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
    public void take() throws IOException
    {
    System.out.println("Enter Name : ");
    name=br.readLine();
    System.out.print("Name in short form="+Character.toUpperCase(name.charAt(0)));
    while(true)
    {
    i=name.indexOf(' ');
    if(i<0)
    break;
    System.out.print("."+Character.toUpperCase(name.charAt(i+1)));
    name=name.substring(i+2);
    }
    System.out.print(name);
    }

    public static void main(String args[])throws Exception
    {
    NameShort ob=new NameShort ();
    ob.take();
    }
    }

    ReplyDelete
  10. Write a program in java to input a sentence from the user and print the letter which has been used the most number of times in that sentence.

    ReplyDelete
    Replies
    1. Solution will be posted very soon. Please keep watch.

      Delete
  11. i need a program which can toggle case of any string (input by user)

    ReplyDelete
  12. could you answer this programs
    1.write a program in bluej to accept a name containing three words and display the
    surname first and then the first and middle
    sample input: MOHANDAS KARAMCHAND GANDHI
    sample output: GANDHI MOHANDAS KARAMCHAND

    ReplyDelete
    Replies
    1. Program will be posted by 6 February 2015.

      Delete
    2. Copy the words in a String array and simply print the array backwards

      Delete
  13. I wrote
    int d=a.lenght();
    But the computer said
    Cannot find symbol- method lenght
    (I had already daclared a as a string )
    Pls help

    ReplyDelete

Subscribe via email

Enter your email address:

Delivered by FeedBurner