Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
Tutorialspoint

Java Program to evaluate mathematical expressions in String

import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
public class Main {
	public static void main(String[] args) throws Exception {
		ScriptEngineManager scriptEngineManager  = new ScriptEngineManager();
		ScriptEngine scriptEngine = scriptEngineManager.getEngineByName("JavaScript");
		// JavaScript code from String
		Object ob = scriptEngine.eval("9 + 15 + 30");
		System.out.println("Result of evaluating mathematical expressions in String = "+ob);
	}
}

Java equals() method

public class Sample {
   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));
   }
}

Java Program to Compare Two Strings

import java.lang.*;
public class StringDemo {
   public static void main(String[] args) {
      String str1 = "tutorials", str2 = "point";
      // comparing str1 and str2
      int retval = str1.compareTo(str2);
      // prints the return value of the comparison
      if (retval < 0) {
         System.out.println("str1 is greater than str2");
      }
      else if (retval == 0) {
         System.out.println("str1 is equal to str2");
      }
      else {
         System.out.println("str1 is less than str2");
      }
   }
}

Java String equals() method

public class Sample{
   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));
   }
}

String compare by equals() method in Java

public class Test {
   public static void main(String args[]) {
      Integer x = 5;
      Integer y = 10;
      Integer z =5;
      Short a = 5;
      System.out.println(x.equals(y));
      System.out.println(x.equals(z));
      System.out.println(x.equals(a));
   }
}

Append method in Java

import java.lang.*;
public class StringBufferDemo {
   public static void main(String[] args) {
      StringBuffer buff = new StringBuffer("tuts ");
      System.out.println("buffer = " + buff);
     
      // appends the char argument as string to the string buffer.
      buff.append('A');
     
      // print the string buffer after appending
      System.out.println("After append = " + buff);
      buff = new StringBuffer("abcd ");
      System.out.println("buffer = " + buff);
     
      // appends the char argument as string to the string buffer.
      buff.append('!');
     
      // print the string buffer after appending
      System.out.println("After append = " + buff);
   }
}

Java string comparison sample code

import java.lang.*;
public class StringDemo {
   public static void main(String[] args) {
      String str1 = "tutorials", str2 = "point";
      // comparing str1 and str2
      int retval = str1.compareTo(str2);
      System.out.println(str1==str2);
      
      // prints the return value of the comparison
      if (retval < 0) {
      System.out.println("str1 is greater than str2");
      } else if (retval == 0) {
         System.out.println("str1 is equal to str2");
      } else {
         System.out.println("str1 is less than str2");
      }
   }
}

String in Switch Case in Java

public class Test {
   public static void main(String args[]) {
      // char grade = args[0].charAt(0);
      char grade = 'C';
      switch(grade) {
         case 'A' :
            System.out.println("Excellent!");
            break;
         case 'B' :
         case 'C' :
            System.out.println("Well done");
            break;
         case 'D' :
            System.out.println("You passed");
         case 'F' :
            System.out.println("Better try again");
            break;
         default :
            System.out.println("Invalid grade");
      }
      System.out.println("Your grade is " + grade);
   }
}

Java String toUpperCase() and toLowerCase() methods

import java.lang.*;
public class StringDemo {
   public static void main(String[] args) {
      // converts all upper case letters in to lower case letters
      String str1 = "SELF LEARNING CENTER";
      System.out.println("string value = " + str1.toLowerCase());
      str1 = "TUTORIALS POINT";
      System.out.println("string value = " + str1.toLowerCase());
      
      // converts all lower case letters in to upper case letters
      String str2 = "This is TutorialsPoint";
      System.out.println("string value = " + str2.toUpperCase());
      str2 = "www.tutorialspoint.com";
      System.out.println("string value = " + str2.toUpperCase());
   }
}

Correct way to trim a string in Java

import java.lang.*;
public class StringDemo {
   public static void main(String[] args) {
      // string with leading and trailing white space
      String str = " This is TutorialsPoint ";
      System.out.print("Before trim = ");
      System.out.println(".." + str + "..");
      
      // leading and trailing white space removed
      System.out.print("After trim = ");
      System.out.println(".." + str.trim() + "..");
   }
}

Advertisements
Loading...

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.