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

Replace String with another in java

import java.io.*;
public class Test {
   public static void main(String args[]) {
      String Str = new String("Welcome to Tutorialspoint.com");
      System.out.print("Return Value :" );
      System.out.println(Str.replace('o', 'T'));
      System.out.print("Return Value :" );
      System.out.println(Str.replace('l', 'D'));
   }
}

Convert a Java String to an Int

public class Test {
   public static void main(String args[]) {
      int x =Integer.parseInt("9");
      double c = Double.parseDouble("5");
      int b = Integer.parseInt("444",16);
      System.out.println(x);
      System.out.println(c);
      System.out.println(b);
   }
}

Difference between charAt() and indexOf() in Java

public class CharAt_IndexOf_Example {
   public static void main(String args[]){
      String str = "This is tutorialspoint";
      System.out.println("Index of letter 't' = "+ str.indexOf('t', 14));
      System.out.println("Letter at the index 8 is ::"+str.charAt(8));
   }
}

Java program to capitalize each word in the string

public class StringToUpperCaseEmp {
   public static void main(String[] args) {
      String str = "string abc touppercase ";
      String strUpper = str.toUpperCase();
      System.out.println("Original String: " + str);
      System.out.println("String changed to upper case: " + strUpper);
   }
}

Java Program to check whether one String is a rotation of another

public class Sample {
   public static void main(String args[]){
      String str1 = "gala";
      String str2 = "alag";
      String s3 = str1+str1;
      if(s3.contains(str2)) {
         System.out.println("str1 is rotation of str2");
      } else {
         System.out.println("str1 is not rotation of str2");
      }
   }
}

Java String endsWith() method

public class Test {
   public static void main(String args[]) {
      String Str = new String("This is really not immutable!!");
      boolean retVal;
      retVal = Str.endsWith( "immutable!!" );
      System.out.println("Returned Value = " + retVal );
      retVal = Str.endsWith( "immu" );
      System.out.println("Returned Value = " + retVal );
   }
}

Java program reverse tOGGLE each word in the string

import java.lang.StringBuffer;
public class ToggleReverse {
   public static void main(String args[]){
      String sample = "Hello How are you";
      String[] words = sample.split(" ");
      String result = "";
      for(String word:words){
         StringBuffer s = new StringBuffer(word);
         word = s.reverse().toString();
         String firstSub = word.substring(0, 1);
         String secondSub = word.substring(1);
         result = result+firstSub.toLowerCase()+secondSub.toUpperCase()+" ";
      }
      System.out.println(result);
   }
}

intern() method in java

import java.lang.*;
public class StringDemo {
   public static void main(String[] args) {
      String str1 = "This is TutorialsPoint";
      // returns canonical representation for the string object
      String str2 = str1.intern();
      
      // prints the string str2
      System.out.println(str2);
      
      // check if str1 and str2 are equal or not
      System.out.println("Is str1 equal to str2 ? = " + (str1 == str2));
   }
}

Convert String to Date in Java

import java.text.SimpleDateFormat;
import java.util.Date;
public class DateString {
   public static void main(String args[]) throws Exception{
      String date = "21-9-2017";
      SimpleDateFormat sdt = new SimpleDateFormat("dd-MM-YYYY");
      Date result = sdt.parse(date);
      System.out.println(result);
   }
}

Java String isEmpty() method

import java.lang.*;
public class StringDemo {
   public static void main(String[] args) {
      String str = "tutorialspoint";
      //prints length of string
      System.out.println("length of string = " + str.length());
     
      //checks if the string is empty or not
      System.out.println("is this string empty? = " + str.isEmpty());
   }
}

Advertisements
Loading...

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