Showing posts with label JAVA String Class. Show all posts
Showing posts with label JAVA String Class. Show all posts

Saturday, 24 August 2013

Java - String valueOf() Method

Description:

This method has followings variants which depends on the passed paramters. This method returns the string representation of the passed argument.
  • valueOf(boolean b): Returns the string representation of the boolean argument.
  • valueOf(char c) : Returns the string representation of the char argument.
  • valueOf(char[] data) : Returns the string representation of the char array argument.
  • valueOf(char[] data, int offset, int count) : Returns the string representation of a specific subarray of the char array argument.
  • valueOf(double d) : Returns the string representation of the double argument.
  • valueOf(float f) : Returns the string representation of the float argument.
  • valueOf(int i) : Returns the string representation of the int argument.
  • valueOf(long l) : Returns the string representation of the long argument.
  • valueOf(Object obj) : Returns the string representation of the Object argument.

Syntax:

Here is the syntax of this method:
static String valueOf(boolean b) 

or 

static String valueOf(char c) 

or

static String valueOf(char[] data) 

or

static String valueOf(char[] data, int offset, int count) 

or

static String valueOf(double d) 

or

static String valueOf(float f) 

or

static String valueOf(int i)

or

static String valueOf(long l)

or

static String valueOf(Object obj) 

Return Value :

  • This method returns the string representation.

Example:

import java.io.*;

public class Test{
   public static void main(String args[]){
      double d = 102939939.939;
      boolean b = true;
      long l = 1232874;
      char[] arr = {'a', 'b', 'c', 'd', 'e', 'f','g' };

      System.out.println("Return Value : " + String.valueOf(d) );
      System.out.println("Return Value : " + String.valueOf(b) );
      System.out.println("Return Value : " + String.valueOf(l) );
      System.out.println("Return Value : " + String.valueOf(arr) );
   }
}
This produces following result:
Return Value : 1.02939939939E8
Return Value : true
Return Value : 1232874
Return Value : abcdefg

Java - String trim() Method

Description:

This method returns a copy of the string, with leading and trailing whitespace omitted.

Syntax:

Here is the syntax of this method:
public String trim()

Parameters:

Here is the detail of parameters:
  • NA

Return Value:

  • It returns a copy of this string with leading and trailing white space removed, or this string if it has no leading or trailing white space.

Example:

import java.io.*;

public class Test{
   public static void main(String args[]){
      String Str = new String("   Welcome to Tutorialspoint.com   ");

      System.out.print("Return Value :" );
      System.out.println(Str.trim() );
   }
}
This produces following result:
Return Value :Welcome to Tutorialspoint.com

Java - String toUpperCase() Method

Description:

This method has two variants. First variant converts all of the characters in this String to upper case using the rules of the given Locale. This is equivalent to calling toUpperCase(Locale.getDefault()).
Second variant take locale as an argument to be used while converting into upper case.

Syntax:

Here is the syntax of this method:
public String toUpperCase()

or

public String toUpperCase(Locale locale)

Parameters:

Here is the detail of parameters:
  • NA

Return Value:

  • It returns the String, converted to uppercase.

Example:

import java.io.*;

public class Test{
   public static void main(String args[]){
      String Str = new String("Welcome to Tutorialspoint.com");

      System.out.print("Return Value :" );
      System.out.println(Str.toUpperCase() );
   }
}
This produces following result:
Return Value :WELCOME TO TUTORIALSPOINT.COM

Java - String toString() Method

Description:

This method returns itself a string

Syntax:

Here is the syntax of this method:
public String toString()

Parameters:

Here is the detail of parameters:
  • NA

Return Value:

  • This method returns the string itself.

Example:

import java.io.*;

public class Test {
   public static void main(String args[]) {
      String Str = new String("Welcome to Tutorialspoint.com");

      System.out.print("Return Value :");
      System.out.println(Str.toString());
   }
}
This produces following result:
Return Value :Welcome to Tutorialspoint.com

Java - String toLowerCase() Method

Description:

This method has two variants. First variant converts all of the characters in this String to lower case using the rules of the given Locale. This is equivalent to calling toLowerCase(Locale.getDefault()).
Second variant take locale as an argument to be used while converting into lower case.

Syntax:

Here is the syntax of this method:
public String toLowerCase()

or

public String toLowerCase(Locale locale)

Parameters:

Here is the detail of parameters:
  • NA

Return Value:

  • It returns the String, converted to lowercase.

Example:

import java.io.*;

public class Test{
   public static void main(String args[]){
      String Str = new String("Welcome to Tutorialspoint.com");

      System.out.print("Return Value :");
      System.out.println(Str.toLowerCase());
   }
}
This produces following result:
Return Value :welcome to tutorialspoint.com

Java - String toCharArray() Method

Description:

This method converts this string to a new character array.

Syntax:

Here is the syntax of this method:
public char[] toCharArray()

Parameters:

Here is the detail of parameters:
  • NA

Return Value:

  • It returns a newly allocated character array whose length is the length of this string and whose contents are initialized to contain the character sequence represented by this string.

Example:

import java.io.*;

public class Test{
   public static void main(String args[]){
      String Str = new String("Welcome to Tutorialspoint.com");

      System.out.print("Return Value :" );
      System.out.println(Str.toCharArray() );
   }
}
This produces following result:
Return Value :Welcome to Tutorialspoint.com

Java - String substring() Method

Description:

This method has two variants and returns a new string that is a substring of this string. The substring begins with the character at the specified index and extends to the end of this string or upto endIndex - 1 if second argument is given.

Syntax:

Here is the syntax of this method:
public String substring(int beginIndex)

or

public String substring(int beginIndex, int endIndex)

Parameters:

Here is the detail of parameters:
  • beginIndex -- the begin index, inclusive.
  • endIndex -- the end index, exclusive.

Return Value :

  • The specified substring.

Example:

import java.io.*;

public class Test{
   public static void main(String args[]){
      String Str = new String("Welcome to Tutorialspoint.com");

      System.out.print("Return Value :" );
      System.out.println(Str.substring(10) );

      System.out.print("Return Value :" );
      System.out.println(Str.substring(10, 15) );
   }
}
This produces following result:
Return Value : Tutorialspoint.com
Return Value : Tuto

Java - String subSequence() Method

Description:

This method returns a new character sequence that is a subsequence of this sequence.

Syntax:

Here is the syntax of this method:
public CharSequence subSequence(int beginIndex, int endIndex)

Parameters:

Here is the detail of parameters:
  • beginIndex -- the begin index, inclusive.
  • endIndex -- the end index, exclusive.

Return Value :

  • This method returns the specified subsequence.

Example:

import java.io.*;

public class Test{
   public static void main(String args[]){
      String Str = new String("Welcome to Tutorialspoint.com");

      System.out.print("Return Value :" );
      System.out.println(Str.subSequence(0, 10) );

      System.out.print("Return Value :" );
      System.out.println(Str.subSequence(10, 15) );
   }
}
This produces following result:
Return Value :Welcome to
Return Value : Tuto