Guest User

Untitled

a guest
Dec 11th, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.57 KB | None | 0 0
  1. package com.tvidushi.archive;
  2.  
  3. public class StringAPI {
  4.  
  5.  
  6. public static void main(String args[]){
  7. str_indexOf();
  8. str_compare();
  9. str_concat();
  10. str_replace();
  11. str_substring();
  12. }
  13.  
  14.  
  15.  
  16. public static void str_indexOf(){
  17.  
  18. System.out.println("iVa Ta Ka Ta Ha Ha Ta".lastIndexOf('i'));
  19. System.out.println("Va Ta Ka Ta Ha Ha Ta".lastIndexOf("Ha"));
  20. System.out.println("This is lastindexof example".lastIndexOf('i', 4));
  21. System.out.println("This is lastindexof example 2".lastIndexOf("last", 2));
  22. }
  23.  
  24.  
  25.  
  26. public static void str_compare(){
  27.  
  28. //COMPARISONS
  29. System.out.println("-------THREE WAYS TO COMPARE STRING-------------");
  30. String str1 = "Hello World";
  31. String str2 = "Hello World";
  32. System.out.println("-------------COMPARSION METHODS-----------------------");
  33. System.out.println("comparsion with equals"+comparewithEquals(str1,str2));
  34. System.out.println("comparsion with =="+compareWithOperator(str1,str2));
  35. System.out.println( "comparsion with compareTo "+compareWithCompareTo(str1,str2));
  36.  
  37.  
  38. }
  39.  
  40. public static void str_concat(){
  41. System.out.println("This is true".concat(" & must be talked about now"));
  42. }
  43.  
  44. public static void str_Concat(){
  45.  
  46.  
  47.  
  48. //REPLACE
  49. System.out.println("--------------------INDEX METHODS---------------------------------------");
  50.  
  51. System.out.println("---This can throw java.lang.StringIndexOutOfBoundsException--indexof/lastindexof/substring-----");
  52. System.out.println("Takshila Vidushi".indexOf("du")); //use it find out
  53.  
  54. System.out.println("Ta Va Da Ta Va Ta Ve".indexOf("Ta", 3)); //searching String using index |This can be used
  55.  
  56. System.out.println("Ta Va Da Ta Va Ta Ve".indexOf("Da", 6)); //searching string using index from an index onwards
  57.  
  58. System.out.println("Va Ta Ka Ta Ha Ha Ta".indexOf('c', 6)); //searching char using index
  59. }
  60.  
  61.  
  62.  
  63. public static void str_replace(){
  64. //REPLACE
  65.  
  66. System.out.println("------There are 4 types of replace methods in String API---------");
  67.  
  68.  
  69. System.out.println("replace(oldchar,newchar) Characther with == "+replaceChar("This is orginal String",'i','Z'));
  70.  
  71.  
  72. System.out.println("replace String with =="+replaceString("Hello World Hello He He He He","He","le"));
  73.  
  74.  
  75. System.out.println("replace Characther with =="+str_replaceFirstExample("Hello World Hello He He He He","He","le"));
  76.  
  77.  
  78. System.out.println("replace String with =="+str_replaceFirstEg("lo La lo La lo La lo La", "lo ","ho") );
  79.  
  80.  
  81. }
  82.  
  83. public static void str_substring(){
  84. System.out.println("This is one index substring works".substring(5));
  85. System.out.println("This is one index substring works".substring(5,13));
  86. }
  87.  
  88.  
  89.  
  90.  
  91. public static String replaceChar(String str, char oldChar,char newChar){
  92. return str.replace(oldChar, newChar);
  93. }
  94.  
  95. public static String replaceString(String str1,String str,String str2){
  96. return str1.replaceAll(str, str2);
  97. }
  98.  
  99. public static String str_replaceFirstExample(String str1,String str,String str2){
  100. return str1.replaceFirst(str, str2);
  101. }
  102.  
  103. public static String str_replaceFirstEg(String str,String str1,String str2){
  104. return str.replace(str1, str2);
  105. }
  106.  
  107.  
  108. public static boolean comparewithEquals(String str1,String str2) {
  109. return str1.equals(str2);
  110. }
  111.  
  112. public static boolean compareWithOperator(String str1,String str2) {
  113. return (str1 ==str2);
  114. }
  115.  
  116. public static int compareWithCompareTo(String str1,String str2) {
  117. return str1.compareTo(str2);
  118. }
  119.  
  120.  
  121.  
  122.  
  123. }
Add Comment
Please, Sign In to add comment