How to reverse a String?

Following example shows how to reverse a String after taking it from command line argument .The program buffers the input String using StringBuffer(String string) method, reverse the buffer and then converts the buffer into a String with the help of toString() method.

public class StringReverseExample{
   public static void main(String[] args) {
      String string = "abcdef";
      String reverse = new StringBuffer(string).reverse().toString();
      System.out.println("\nString before reverse: "+string);
      System.out.println("String after reverse: "+reverse);
   }
}

Result
The above code sample will produce the following result.

String before reverse:abcdef
String after reverse:fedcba

Example

Following another example shows how to reverse a String after taking it from command line argument

import java.io.*;
import java.util.*;

public class HelloWorld {
   public static void main(String[] args) {
      String input = "tutorialspoint";
      char[] try1 = input.toCharArray();
      for (int i = try1.length-1;i>=0;i--) System.out.print(try1[i]);
   }
}

The above code sample will produce the following result.

tniopslairotut 

How to replace a substring inside a string by another one ?

This example describes how replace method of java String class can be used to replace character or substring by new one.

public class StringReplaceEmp{
   public static void main(String args[]){
      String str = "Hello World";
      System.out.println( str.replace( 'H','W' ) );
      System.out.println( str.replaceFirst("He", "Wa") );
      System.out.println( str.replaceAll("He", "Ha") );
   }
}


Result
The above code sample will produce the following result.

Wello World
Wallo World
Hallo World

How to remove a particular character from a string ?

Following example shows hoe to remove a character from a particular position from a string with the help of removeCharAt(string,position) method.

public class Main {
   public static void main(String args[]) {
      String str = "this is Java";
      System.out.println(removeCharAt(str, 3));
   }
   public static String removeCharAt(String s, int pos) {
      return s.substring(0, pos) + s.substring(pos + 1);
   }
}


Result
The above code sample will produce the following result.

this is Java

How to search the last position of a substring ?

This example shows how to determine the last position of a substring inside a string with the help of strOrig.lastIndexOf(Stringname) method.

public class SearchlastString {
   public static void main(String[] args) {
      String strOrig = "Hello world ,Hello Reader";
      int lastIndex = strOrig.lastIndexOf("Hello");
     
      if(lastIndex == - 1){
         System.out.println("Hello not found");
      } else {
         System.out.println("Last occurrence of Hello is at index "+ lastIndex);
      }
   }
}

Result

The above code sample will produce the following result.

Last occurrence of Hello is at index 13

Example

This another example shows how to determine the last position of a substring inside a string with the help of strOrig.lastIndexOf(Stringname) method.

public class HelloWorld{
   public static void main(String []args) {
      String t1 = "Tutorialspoint";
      int index = t1.lastIndexOf("p");
      System.out.println(index);
   }
}

The above code sample will produce the following result.

9

How to compare two strings in Java ?

Following example compares two strings by using str compareTo (string), str compareToIgnoreCase(String) and str compareTo(object string) of string class and returns the ascii difference of first odd characters of compared strings.

public class StringCompareEmp{
   public static void main(String args[]){
      String str = "Hello World";
      String anotherString = "hello world";
      Object objStr = str;

      System.out.println( str.compareTo(anotherString) );
      System.out.println( str.compareToIgnoreCase(anotherString) );
      System.out.println( str.compareTo(objStr.toString()));
   }
}

Result
The above code sample will produce the following result.

-32
0
0

String compare by equals()

This method compares this string to the specified object. The result is true if and only if the argument is not null and is a String object that represents the same sequence of characters as this object.

public class StringCompareequl{
   public static void main(String []args){
      String s1 = "tutorialspoint";
      String s2 = "tutorialspoint";
      String s3 = new String ("Tutorials Point");
      System.out.println(s1.equals(s2));
      System.out.println(s2.equals(s3));
   }
}

The above code sample will produce the following result.

true 

false 

String compare by == operator
public class StringCompareequl{
   public static void main(String []args){
      String s1 = "tutorialspoint";
      String s2 = "tutorialspoint";
      String s3 = new String ("Tutorials Point");
      System.out.println(s1 == s2);
      System.out.println(s2 == s3);
   }
}

The above code sample will produce the following result.

true

false 

Environment in Java programming

Commonly used examples:-

1. How to compile a java file?

2. How to run a class file?

3. How to debug a java file?

4. How to set classpath?

5. How to view current classpath?

6. How to set destination of the class file?

7. How to run a compiled class file?

8. How to check version of java running on your system?

9. How to set classpath when class files are in .jar file?

How to set classpath when class files are in .jar file?

Following example shows how to set class path when classes are stored in a .jar or .zip file.

c:> java -classpath C:\java\myclasses.jar utility.testapp.main

Result

The above code sample will produce the following result.

Class path set.

How to check version of java running on your system?

Following example shows how to check version of java installed on your system using version argument with java command.

java -version

Result

The above code sample will produce the following result.

java version "1.6.0_13"
Java(TM) SE Runtime Environment (build 1.6.0_13-b03)
Java HotSpot(TM) Client VM (build 11.3-b02, mixed mode, sharing)

How to run a class file?

Following example shows how to run a class file from command prompt using java command.

c:\jdk\demoapp>java First 

Result

The above code sample will produce the following result.

Demo application executed.

How to set destination of the class file?

Following example shows how to set destination of the class file that will be created after compiling a java file using -d option with javac command.

c:> javac demo.java -d c:\myclasses 

Result

The above code sample will produce the following result.

Demo application executed.

How to view current classpath?

Following example shows how to view current classpath using echo command.

C:> echo %CLASSPATH%

Result

The above code sample will produce the following result.

.;C:\Program Files\Java\jre1.6.0_03\lib\ext\QTJava.zip

How to set classpath?

Following example shows how to set classpath.

C:> java -classpath C:\java\DemoClasses utility.demoapp.main

Result

The above code sample will produce the following result.

Class path set.