- /*
- Java String valueOf example.
- This Java String valueOf example describes how various java primitives and Object
- are converted to Java String object using String valueOf method.
- */
- public class JavaStringValueOfExample{
- public static void main(String args[]){
- /*
- Java String class defines following methods to convert various Java primitives to
- Java String object.
- 1) static String valueOf(int i)
- Converts argument int to String and returns new String object representing
- argument int.
- 2) static String valueOf(float f)
- Converts argument float to String and returns new String object representing
- argument float.
- 3) static String valueOf(long l)
- Converts argument long to String and returns new String object representing
- argument long.
- 4) static String valueOf(double i)
- Converts argument double to String and returns new String object representing
- argument double.
- 5) static String valueOf(char c)
- Converts argument char to String and returns new String object representing
- argument char.
- 6) static String valueOf(boolean b)
- Converts argument boolean to String and returns new String object representing
- argument boolean.
- 7) static String valueOf(Object o)
- Converts argument Object to String and returns new String object representing
- argument Object.
- */
- int i=10;
- float f = 10.0f;
- long l = 10;
- double d=10.0d;
- char c='a';
- boolean b = true;
- Object o = new String("Hello World");
- /* convert int to String */
- System.out.println( String.valueOf(i) );
- /* convert float to String */
- System.out.println( String.valueOf(f) );
- /* convert long to String */
- System.out.println( String.valueOf(l) );
- /* convert double to String */
- System.out.println( String.valueOf(d) );
- /* convert char to String */
- System.out.println( String.valueOf(c) );
- /* convert boolean to String */
- System.out.println( String.valueOf(b) );
- /* convert Object to String */
- System.out.println( String.valueOf(o) );
- }
- }
- /*
- OUTPUT of the above given Java String valueOf Example would be :
- 10
- 10.0
- 10
- 10.0
- a
- true
- true
- Hello World
- */
Welcome to the world of Java examples, organized by categories and Java packages. Java examples (Java sample source code) help to understand functionality of various Java classes and methods as well as various programming techniques in a simple way, which is otherwise very hard to learn by reading tutorials or Java API. So start exploring...
Showing posts with label string formats. Show all posts
Showing posts with label string formats. Show all posts
Tuesday, November 29, 2011
Java String valueOf example.
Java String split example.
- /*
- Java String split example.
- This Java String split example describes how Java String is split into multiple
- Java String objects.
- */
- public class JavaStringSplitExample{
- public static void main(String args[]){
- /*
- Java String class defines following methods to split Java String object.
- String[] split( String regularExpression )
- Splits the string according to given regular expression.
- String[] split( String reularExpression, int limit )
- Splits the string according to given regular expression. The number of resultant
- substrings by splitting the string is controlled by limit argument.
- */
- /* String to split. */
- String str = "one-two-three";
- String[] temp;
- /* delimiter */
- String delimiter = "-";
- /* given string will be split by the argument delimiter provided. */
- temp = str.split(delimiter);
- /* print substrings */
- for(int i =0; i < temp.length ; i++)
- System.out.println(temp[i]);
- /*
- IMPORTANT : Some special characters need to be escaped while providing them as
- delimiters like "." and "|".
- */
- System.out.println("");
- str = "one.two.three";
- delimiter = "\\.";
- temp = str.split(delimiter);
- for(int i =0; i < temp.length ; i++)
- System.out.println(temp[i]);
- /*
- Using second argument in the String.split() method, we can control the maximum
- number of substrings generated by splitting a string.
- */
- System.out.println("");
- temp = str.split(delimiter,2);
- for(int i =0; i < temp.length ; i++)
- System.out.println(temp[i]);
- }
- }
- /*
- OUTPUT of the above given Java String split Example would be :
- one
- two
- three
- one
- two
- three
- one
- two.three
- */
Java String compare example.
- /*
- Java String compare example.
- This Java String compare example describes how Java String is compared with another
- Java String object or Java Object.
- */
- public class JavaStringCompareExample{
- public static void main(String args[]){
- /*
- Java String class defines following methods to compare Java String object.
- 1) int compareTo( String anotherString )
- compare two string based upon the unicode value of each character in the String.
- Returns negative int if first string is less than another
- Returns positive int if first string is grater than another
- Returns 0 if both strings are same.
- 2) int compareTo( Object obj )
- Behaves exactly like compareTo ( String anotherString) if the argument object
- is of type String, otherwise throws ClassCastException.
- 3) int compareToIgnoreCase( String anotherString )
- Compares two strings ignoring the character case of the given String.
- */
- String str = "Hello World";
- String anotherString = "hello world";
- Object objStr = str;
- /* compare two strings, case sensitive */
- System.out.println( str.compareTo(anotherString) );
- /* compare two strings, ignores character case */
- System.out.println( str.compareToIgnoreCase(anotherString) );
- /* compare string with object */
- System.out.println( str.compareTo(objStr) );
- }
- }
- /*
- OUTPUT of the above given Java String compare Example would be :
- -32
- 0
- 0
- */
Java Search String using indexOf Example
- /*
- Java Search String using indexOf Example
- This example shows how we can search a word within a String object using
- indexOf method.
- */
- public class SearchStringExample {
- public static void main(String[] args) {
- //declare a String object
- String strOrig = "Hello world Hello World";
- /*
- To search a particular word in a given string use indexOf method.
- indexOf method. It returns a position index of a word within the string
- if found. Otherwise it returns -1.
- */
- int intIndex = strOrig.indexOf("Hello");
- if(intIndex == - 1){
- System.out.println("Hello not found");
- }else{
- System.out.println("Found Hello at index " + intIndex);
- }
- /*
- we can also search a word after particular position using
- indexOf(String word, int position) method.
- */
- int positionIndex = strOrig.indexOf("Hello",11);
- System.out.println("Index of Hello after 11 is " + positionIndex);
- /*
- Use lastIndexOf method to search a last occurrence of a word within string.
- */
- int lastIndex = strOrig.lastIndexOf("Hello");
- System.out.println("Last occurrence of Hello is at index " + lastIndex);
- }
- }
- /*
- Output of the program would be :
- Found Hello at index 0
- Index of Hello after 11 is 12
- Last occurrence of Hello is at index 12
- */
Subscribe to:
Posts (Atom)
How to find the most appropriate Keywords?
🔍 Step 1: Understand Your Business and Audience Define your products, services, or content . Identify your target audien...
-
/* Pause Thread Using Sleep Method Example This Java example shows how to pause currently running thread using sleep method of Jav...
-
/* Java String split example. This Java String split example describes how Java String is split into multiple Java String objects. */ ...
-
// File Name GreetingClient.java import java.net.*; import java.io.*; public class GreetingClient { public static void main(String [] ...