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.

How to set destination of the class file?

Following example shows how to debug a java file using =g option with javac command.

c:> javac demo.java -g

Result

The above code sample will produce the following result.

Demo.java will debug.

How to set multiple classpath in Java?

Following example shows how to set multiple classpath. Multiple class paths are separated by a semicolon.

c:> java -classpath C:\java\MyClasse1;C:\java\MyClass2 
   utility.testapp.main

Result

The above code sample will produce the following result.

Class path set.