Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.tvidushi.archive;
- public class StringAPI {
- public static void main(String args[]){
- str_indexOf();
- str_compare();
- str_concat();
- str_replace();
- str_substring();
- }
- public static void str_indexOf(){
- System.out.println("iVa Ta Ka Ta Ha Ha Ta".lastIndexOf('i'));
- System.out.println("Va Ta Ka Ta Ha Ha Ta".lastIndexOf("Ha"));
- System.out.println("This is lastindexof example".lastIndexOf('i', 4));
- System.out.println("This is lastindexof example 2".lastIndexOf("last", 2));
- }
- public static void str_compare(){
- //COMPARISONS
- System.out.println("-------THREE WAYS TO COMPARE STRING-------------");
- String str1 = "Hello World";
- String str2 = "Hello World";
- System.out.println("-------------COMPARSION METHODS-----------------------");
- System.out.println("comparsion with equals"+comparewithEquals(str1,str2));
- System.out.println("comparsion with =="+compareWithOperator(str1,str2));
- System.out.println( "comparsion with compareTo "+compareWithCompareTo(str1,str2));
- }
- public static void str_concat(){
- System.out.println("This is true".concat(" & must be talked about now"));
- }
- public static void str_Concat(){
- //REPLACE
- System.out.println("--------------------INDEX METHODS---------------------------------------");
- System.out.println("---This can throw java.lang.StringIndexOutOfBoundsException--indexof/lastindexof/substring-----");
- System.out.println("Takshila Vidushi".indexOf("du")); //use it find out
- System.out.println("Ta Va Da Ta Va Ta Ve".indexOf("Ta", 3)); //searching String using index |This can be used
- System.out.println("Ta Va Da Ta Va Ta Ve".indexOf("Da", 6)); //searching string using index from an index onwards
- System.out.println("Va Ta Ka Ta Ha Ha Ta".indexOf('c', 6)); //searching char using index
- }
- public static void str_replace(){
- //REPLACE
- System.out.println("------There are 4 types of replace methods in String API---------");
- System.out.println("replace(oldchar,newchar) Characther with == "+replaceChar("This is orginal String",'i','Z'));
- System.out.println("replace String with =="+replaceString("Hello World Hello He He He He","He","le"));
- System.out.println("replace Characther with =="+str_replaceFirstExample("Hello World Hello He He He He","He","le"));
- System.out.println("replace String with =="+str_replaceFirstEg("lo La lo La lo La lo La", "lo ","ho") );
- }
- public static void str_substring(){
- System.out.println("This is one index substring works".substring(5));
- System.out.println("This is one index substring works".substring(5,13));
- }
- public static String replaceChar(String str, char oldChar,char newChar){
- return str.replace(oldChar, newChar);
- }
- public static String replaceString(String str1,String str,String str2){
- return str1.replaceAll(str, str2);
- }
- public static String str_replaceFirstExample(String str1,String str,String str2){
- return str1.replaceFirst(str, str2);
- }
- public static String str_replaceFirstEg(String str,String str1,String str2){
- return str.replace(str1, str2);
- }
- public static boolean comparewithEquals(String str1,String str2) {
- return str1.equals(str2);
- }
- public static boolean compareWithOperator(String str1,String str2) {
- return (str1 ==str2);
- }
- public static int compareWithCompareTo(String str1,String str2) {
- return str1.compareTo(str2);
- }
- }
Add Comment
Please, Sign In to add comment