Example : A java program to display string output in various customized format .
import java.io.*;
class Examples 
{
  public static void main(String[] args) 
  {
        String Str1 = "Codershelpline is a \"simple site\".";
	String Str2 = "Codershelpline is a \'simple site\'.";
	String Str3 = "Codershelpline is a \\simple site\\.";
	
	String Str4 = "Coders\nHelpline";
	String Str5 = "Coders\rHelpline";     // \r for Carriage return
	String Str6 = "Coders\tHelpline";
	String Str7 = "Coders\bHelpline";     // \b for Backspace
	String Str8 = "Coders\fHelpline";     //\f for form feed
	
        System.out.println(Str1);
        System.out.println(Str2);	
        System.out.println(Str3);
	System.out.println(Str4);
	System.out.println(Str5);
	System.out.println(Str6);
	System.out.println(Str7);
	System.out.println(Str8);	
  }
}

Output :
Codershelpline is a "simple site".
Codershelpline is a 'simple site'.
Codershelpline is a \simple site\.

Coders
Helpline
Helpline
Coders  Helpline
CoderHelpline
CodersHelpline

Example : A Java program to show String output in desired format.

class Example
{
   public static void main(String args[])
   {
       String Str1="000011010";
       Str1 = String.format("%011d", Integer.parseInt(Str1));
       System.out.println(Str1);
	   
       String Str2="000011010";
       Str2 = String.format("%09d", Integer.parseInt(Str2));
       System.out.println(Str2);
	   
       String Str3="000011010";
       Str3 = String.format("%07d", Integer.parseInt(Str3));
       System.out.println(Str3);
	   
       String Str4="000011010";
       Str4 = String.format("%05d", Integer.parseInt(Str4));
       System.out.println(Str4);
	   
       String Str5="000011010";
       //Str5 = String.format("%02d", Integer.parseInt(Str5));
       Str5 = String.format("%2d", Integer.parseInt(Str5));
       System.out.println(Str5);

       String Str6="000011010";
       Str6 = String.format("%d", Integer.parseInt(Str6));
       System.out.println(Str6);       
   }
}

Output :
00000011010
000011010
0011010
11010
11010
11010
Example : A java program to create String using various format.
class Example
{  
   public static void main(String args[])
   { 
	String Str1 = "Codershelpline";      //Creation of String using java string literal Concept  
	char ch[]={'w','e','l','c','o','m','e'};  // Character array
	
	String Str2 = new String(ch);      //Conversion of Character Array into String  
		
	String Str3 = new String("Hello India"); //Creation of Java String using new keyword   
		
	System.out.println(Str1);  
	System.out.println(Str2);  
	System.out.println(Str3);  
   }
}

Output :
Codershelpline
welcome
Hello India
Example : A String program in java to accept single string value  from the user and display it.
import java.io.*;
import java.util.Scanner;

class Student 
{	
	public static void main(String args[]) 
	{ 
		String Str; 
		Scanner sc = new Scanner(System.in);
 		System.out.println("Enter a string"); 
 		Str = sc.nextLine();      // or Str = sc.next();  
		System.out.println("The entered String is = "+Str);
 	}
}

------------  OR  ------------
import java.io.*;
import java.util.Scanner;

class Student 
{	
	public static void main(String args[]) throws IOException 
	{ 
		String Str; 
		BufferedReader Br = new BufferedReader(new InputStreamReader(System.in));
 		System.out.println("Enter a string"); 
 		Str = Br.readLine();  
		System.out.println("The entered String is ="+Str);
 	}
}
Example : A String program in java to accept multiple string values and display it using class and object concept.
import java.io.*;
import java.util.Scanner;

class Student 
{
	String[] sname=new String[5];	
	Scanner sc=new Scanner(System.in);
	
	void input()
	{
		System.out.println("Enter five Students Name:-");		
		for(int i = 0; i < 5; i++)
                 {
                   sname[i] = sc.nextLine();   // or sname[i] = sc.next();
		 }
	}
	
	void output()
	{
		System.out.println("The stored Students name are :-");
		
		for(int i = 0; i < 5; i++)
                 {
                    System.out.println(sname[i]);
		 }
	}
	
	public static void main(String[] args) 
	{
		Student Std=new Student();		
			Std.input();
			Std.output();		
	}	
}
Example : A java program to display all the string in small letters without using String method.
import java.io.*;
class Examples 
{
  public static void main(String[] args) 
  {
        String Str1 = "CODERSHELPLINE";
        String Str2 = "";
	char ch;
		
        for (int i = 0; i < Str1.length(); i++) 
	{                      
            if (Str1.charAt(i)>= 'A' && Str1.charAt(i)<= 'Z') 
		{
                  ch = (char)(Str1.charAt(i) + 32);
                }            
            else 
	        {
                  ch = (char)(Str1.charAt(i));
                }
            
            Str2= Str2+ch; 
        }
        System.out.println(Str2);
  }
}
Output :
codershelpline
Example : A java program to display all the string in capital letters without using String method.
import java.io.*;
class Examples 
{
  public static void main(String[] args) 
  {
        String Str1 = "codershelpline";
        String Str2 = "";
	char ch;
		
        for (int i = 0; i < Str1.length(); i++) 
	    {                      
                   if (Str1.charAt(i)>= 'a' && Str1.charAt(i)<= 'z') 
		    {
                      ch = (char)(Str1.charAt(i) - 32);
                    }            
                   else 
		    {
                      ch = (char)(Str1.charAt(i));
                    }
            
                   Str2= Str2+ch; 
            }
        System.out.println(Str2);
  }
}

Output :
CODERSHELPLINE

----------   OR  ------------
import java.io.*;
class Examples 
{
   static String Uppercase(String Str1)
     {
	String Str2 = "";
	char ch;
	for (int i = 0; i < Str1.length(); i++) 
	   {						  
		if (Str1.charAt(i)>= 'a' && Str1.charAt(i)<= 'z') 
		{
		   ch = (char)(Str1.charAt(i) - 32);
		}				
		else 
		{
		   ch = (char)(Str1.charAt(i));
		}				
		   Str2= Str2+ch; 
	   }
	 return Str2;
      }  
  
   public static void main(String[] args) 
	{		
	   System.out.println(Uppercase("codershelpline"));
           System.out.println(Uppercase("CodersHelpline"));		
	}
}
Output :
CODERSHELPLINE
CODERSHELPLINE

Example : A Java program to convert character into string using String method.[ toString() ]

class CharToString
{
	public static void main(String args[])
	{
		char c='M';
		String s=Character.toString(c);
		System.out.println("String is: "+s);
	}
}

Example : A Java program to convert String into Integer using String method.[ parseInt() & valueOf() ]

class Example
{
   public static void main(String args[])
   {
	String Str="5101";	
	int num1 = Integer.parseInt(Str);
        int num2 = Integer.valueOf(Str);     //returns an object of Integer class	
	System.out.println(num1+"  "+num2);   //returns a primitive int value
   }
}
Output :
5101  5101

Example : A Java program to convert Integer into String using String method. [ valueOf() ]

class Example
{
   public static void main(String args[])
   {
	int x = 225;
	String Str1 = String.valueOf(x);
	String Str2 = Integer.toString(x);
	System.out.println(Str1 +"  "+Str2); 		
   }
}
Output :
225  225

Example : A Java program to find/display the length of String using String method. [ length() ]

import java.io.*;
class Example 
{
  public static void main(String[] args) 
  {
    String x = "Hello Codershelpline User";
    System.out.println("The length of the string is: " + x.length());    
  }
}

Output :
The length of the string is: 25
Example : A java program to display the string in small letters using String method.                   [ toLowercase() ]
import java.io.*;
class Example 
{
  public static void main(String[] args) 
  {
    String x = "Hello Codershelpline User";   
    System.out.println(x.toLowerCase());
  }
}
Output :
hello codershelpline user
Example : A java program to display the string in capital letters using String method.                 [ toUppercase() ]
import java.io.*;
class StringExamples 
{
  public static void main(String[] args) 
  {
    String x = "Hello Codershelpline User";   
    System.out.println(x.toUpperCase());    
  }
}
Output :
HELLO CODERSHELPLINE USER
Example : A java program to concatenate two/more string values using String method.             [ concat() ]
import java.io.*;
class Examples 
{
  public static void main(String[] args) 
  {
    String x = "Welcome";
    String y = "World";
    System.out.println(x +" "+ y);
	
    String x1 = "Coders";    
    x1.concat(" Helpline");
    System.out.println(x1);
	
    String x2 = "Hello";
    String y2 = " India";
    System.out.println(x2.concat(y2));

    String x3 = "12";
    String y3 = "17";
    System.out.println(x3+y3);

    int x4 = 20;
    int y4 = 10;
    System.out.println(x4+y4);
    
    String x5 = "20";
    int y5 = 10;
    System.out.println(x5+y5);	
  }
}

Output :
Welcome World
Coders   // Output is only Coders not CodersHelpline because String is by nature Immutable objects.
Hello India
1217
30
2010
Example : A java program to find a character index in a string values using String method.         [ indexOf() ]
class Examples
{
  public static void main(String[] args) 
   {
     String Str = "Codershelpline";
     System.out.println(Str.indexOf("help"));
     System.out.println(Str.indexOf("p"));
     System.out.println(Str.indexOf("k"));
   }
}

Output :
6
9
-1
Example : A java program to remove extra space from a string values using String method.     [ trim() ]      
import java.io.*;
class Examples 
{
  public static void main(String[] args) 
  {
        String Str1 = "      Codershelpline      ";	
	System.out.println(Str1.trim());	
  }
}
Output: 
Codershelpline
Example : A java program to break/split string from a delimiter symbol using String method.     [ split() ]
import java.io.*;
class Examples 
{
  public static void main(String[] args) 
  {
        String Str1 = "www.Codershelpline.com";
	String [] Str2;
	int i;
		
	Str2=Str1.split("\\.");  //To break from a delimiter symbol dot(.)
	for (i=0;i<Str2.length;i++)
          {
	      System.out.println(Str2[i]);
          }	
  }
}

Output :
www
Codershelpline
com

----------  OR  -----------
import java.io.*;
class Examples 
{
  public static void main(String[] args) 
  {
        String Str1 = "www Codershelpline com";
	String [] Str2;
	int i;
		
	Str2=Str1.split("\\ ");  //To break string from a delimiter symbol space(" ")
	for (i=0;i<Str2.length;i++)
	System.out.println(Str2[i]);	
  }
}

Output :
www
Codershelpline
com
Example : A java program to compare two strings using String method.                                         [ equals()/compareTo() ]
import java.io.*;
class Examples 
{
  public static void main(String[] args) 
  {
        String Str1 = new String("HELLO INDIA");
        String Str2 = new String("HELLO WORLD");

        //comparing strings using equals()method as Character wise/actual content of string object       
        System.out.println(Str1.equals(Str1));
	System.out.println(Str1.equals(Str2));

        //comparing strings using == operator as Reference basis
        System.out.println(Str1 == Str1);
        System.out.println(Str1 == Str2);

        //comparing strings using compareTo() method as Unicode basis 
        System.out.println(Str1.compareTo(Str1));
        System.out.println(Str1.compareTo(Str2));	
  }
}

Output :
true
false
true
false
0
-14

NB : compareTo() method compares and returns an int value 0, +iv value and -ive value. It returns
- 0, if this string are equals.
- -ive value, if this string is less than the string argument
- +ive value, if this string is greater than the string argument
Example : A java program to display customized sub string from given string using String method[ substring() ]
import java.io.*;
class Examples 
{
	public static void main(String[] args) 
	{	
	String Str1 = "Codershelpline";		
			
	System.out.println(Str1.substring(4));
	System.out.println(Str1.substring(0,5));
	System.out.println(Str1.substring(3,6));
	}
}

Output :
rshelpline
Coder
ers

Loading

Categories: Java

0 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.