Search This Blog

Saturday, 13 December 2014

String use full methods

Most useful string method in general programming

In programming, we can use this most useful String method.

1. charAt()
  • This method return string character, input position of character in method and return character.
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
public class Main {
 public static void main(String args[]) { 
  
  /**
   * charAt method
   */
  String string="Hello india, java is best programming language";
  System.out.println("Char at="+string.charAt(0));  
 }
}
Output
Char at=H










    2. compareTo(object)
  • This method use for compare string to object

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
public class Main {
 public static void main(String args[]) {
  /**
   * Compare method
   * The value 0 if the argument is a string lexicographically equal to this string; 
   * a value less than 0 if the argument is a string lexicographically greater than this string; 
   * and a value greater than 0 if the argument is a string lexicographically less than this string.
   */

  String compare_1="hello";
  String compare_2="hello";
  System.out.println("compare="+compare_1.compareTo(compare_2));

 }
}

Output
compare=0

String formatting using java method

String formatting using format method

This method is use when some value are come from dynamic basis and string was saved in property file or xml file. For example provide validation messages or messages to user.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import java.util.Calendar;
import java.util.Date;


public class Main {
 public static void main(String args[]) { 
  /**
   * Order delivery message example
   */
  Date date=Calendar.getInstance().getTime();
  String message="Your ordered delivery before %s";  
  String formate=String.format(message, date);
  System.out.println("Message="+formate);

  /**
   * another example of put link 
   */
  String link="http://powerisejava.blogspot.in/";
  String message_2="click here to check update %s";
  String formate_2=String.format(message_2,link);
  System.out.println("Link="+formate_2);
 }
}

Output
Message=Your ordered delivery before Sat Dec 13 22:37:34 IST 2014
Link=click here to check update http://powerisejava.blogspot.in/

Convert String array to String variable

Converting String array to String variable



 1
 2
 3
 4
 5
 6
 7
 8
 9
10
public class Main {
 public static void main(String args[]) { 
  /**
   * Convert String array to String variable
   */
  char[] stringArray = { 'H', 'e', 'l', 'l', 'o', '.'};
  String string = new String(stringArray);  
  System.out.println( string );
 }
}

Output
Hello.

Two String concatenating

String concatenating in java is easy, there are general two method.

  1. Using with concat method
  2. Using with + operator


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
public class Main {
 public static void main(String args[]) {  
  /**
   * String concatenating with method
   */
  String name="Krunal";
  String surname=" Indrodiya";
  String fullName=name.concat(surname);
  System.out.println("concat method Full name="+fullName);

  /**
   * String also concatenating with + operator
   */
  fullName=name+surname;
  System.out.println("+ operator Full name="+fullName);
 }
}

Output
concate method Full name=Krunal Indrodiya
+ operator Full name=Krunal Indrodiya

Friday, 12 December 2014

int to float conversation and float to int conversation

Conversation from int to float and float to int is so easy. Java provide below method for conversation.



 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
public static void main(String args[]) {

  /**
   * integer to float conversation
   */
  int i=123;
  float f=(float)i;
  System.out.println("Float variable="+f);

  /**
   * float to integer conversation
   */
  float ff=123.665f;
  int ii=(int)Math.round(ff);
  System.out.println("Intger varibale="+ii);
 }
OutPut
Float variable=123.0
Intger varibale=124



String to float conversation and float to String conversation

Conversation from String to float and float to String is so easy. Java provide below method for conversation.


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
public class Main {

 public static void main(String args[]) {  

  /**
   * String to float conversation
   */
  String strVar="123.45";
  float f=Float.parseFloat(strVar);
  System.out.println("Floar variable="+f);

  /**
   * float to String conversation
   */
  strVar=String.valueOf(f);
  System.out.println("String varibale="+f);
 }
}

OutPut
Floar variable=123.45
String varibale=123.45

String to BigDecimal conversation and BigDecimal to String conversation

Conversation from String to BigDecimal and BigDecimal to String is so easy. Java provide below method for conversation.




 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
import java.math.BigDecimal;

public class Main {
 public static void main(String args[]) {

  /**
   * String to BigDecimal conversation
   */
  String strVar1 = "123.45";
  BigDecimal bigDecimal = new BigDecimal(strVar1);
  System.out.println("BigDecimal=" + bigDecimal);

  /**
   * BigDecimal to String conversation
   */
  String strVar2 = bigDecimal.toString();
  System.out.println("String=" + strVar2);
 }
}
Output
BigDecimal=123.45
String=123.45

String to Integer conversation and Integer to String conversation

Conversation from String to integer and integer to String is so easy. Java provide for below method to conversation.






 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
public class Main {
 public static void main(String args[]) {  
  /**
   * String to integer conversation
   */
  String strVar="123";
  int intVar=Integer.parseInt(strVar);
  System.out.println("Integer variable="+intVar);

  /**
   * Integer to String conversation
   */
  int var1=123;
  String var2=null;
  var2=String.valueOf(var1);
  System.out.println("String variable="+var2);
 }
}
Output
Integer variable=123
String variable=123