Showing posts with label Basics. Show all posts
Showing posts with label Basics. Show all posts

Java Programming Example of how to extract substring from the string?

This program accepts the string, starting index and ending index from the keyboard and extract the substring from the given string according to index.

import java.io.*;

/**
 *
 * @author JavaHotSpot
 */

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

     String s1,s2,s3,s4;
     int st,ed;
     DataInputStream d;
     try
     {
     System.out.println("Enter the String");
     d=new DataInputStream(System.in);
     s1=(String)d.readLine();
     System.out.println("Enter the Starting Index");
     s2=(String)d.readLine();
     st=Integer.parseInt(s2);
     System.out.println("Enter the Ending Index");
     s3=(String)d.readLine();
      ed=Integer.parseInt(s3);
      s4=s1.substring(st, ed);
     System.out.println("String is = "+s1+" Substring is ="+s4);
     }
     catch(Exception e)
     {
         System.out.println(e.toString());
     }
 }
}

Output:
Enter the String
JavaHotSpot
Enter the Starting Index
3
Enter the Ending Index
8

Java Programming Example of how to remove the extra spaces from the given string?

This program accepts the string from the keyboard and removes the extra spaces from both side of the given string.

import java.io.*;

/**
 *
 * @author JavaHotSpot
 */

public class removeExtraSpaces
{
    public static void main(String args[])
 {
     String s1,s2;
     int length;
     DataInputStream d;
     try
     {
     System.out.println("Enter the String");
     d=new DataInputStream(System.in);
     s1=(String)d.readLine();
     s2=s1.trim();
     System.out.println("The Given String  = "+s1);
     System.out.println("String after removing extra spaces= "+s2);
     }
     catch(Exception e)
     {
         System.out.println(e.toString());
     }
 }
}

Output:
Enter the String
      JavaHotSpot
The Given String  =       JavaHotSpot
      String after removing extra spaces= JavaHotSpot

Java Programming Example of how to convert string into bytes?

This program accepts the string from the keyboard and convert the string into bytes and displays the bytes.

import java.io.*;

/**
 *
 * @author JavaHotSpot
 */

public class stringtoBytes
{
  public static void main(String args[])
 {
     String s1;
     int length;
     byte[] b;
     DataInputStream d;
     try
     {
     System.out.println("Enter the String");
     d=new DataInputStream(System.in);
     s1=(String)d.readLine();
     b=s1.getBytes();
     System.out.println("The bytes are");
     for(int i=0;i<b.length;i++)
     {
         System.out.println(b[i]);
     }
     }
     catch(Exception e)
     {
         System.out.println(e.toString());
     }
 }
}

Output:
Enter the String
Java
The bytes are
74
97
118

Java Programming Example of how to convert String into characters and display the characters

This program accepts the string from the keyboard and convert the string into characters and displays the characters.

import java.io.*;

/**
 *
 * @author JavaHotSpot
 */

public class stringtocharacter
{
  public static void main(String args[])
 {
     String s1;
     int length;
     char[] c;
     DataInputStream d;
     try
     {
     System.out.println("Enter the String");
     d=new DataInputStream(System.in);
     s1=(String)d.readLine();
     c=s1.toCharArray();
     System.out.println("The Characters are");
     for(int i=0;i<c.length;i++)
     {
         System.out.println(c[i]);
     }
     }
     catch(Exception e)
     {
         System.out.println(e.toString());
     }
 }
}

Output:
Enter the String
JavaHotSpot
The Characters are
J
a
v
a
H
o
t
S
p