HarrJ

Day 13 String 1

Aug 14th, 2023 (edited)
1,347
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.96 KB | None | 0 0
  1. public class Day13B {
  2.     public static void main(String[] args) {
  3.         String txtIn = "A Day Late and a Dollar Short";
  4.         char chSearch = 'l';
  5.         int count = 0;
  6.        
  7.         System.out.println("txtIn: " + txtIn);
  8.         System.out.println("txtIn have " + txtIn.length() + " characters");
  9.        
  10.         System.out.println("\n-----(count with charAt())-----\n");
  11.         for (int i = 0; i < txtIn.length(); i++) {
  12.             if (txtIn.toLowerCase().charAt(i) == chSearch) {
  13.                 count++;
  14.                 System.out.println(txtIn.charAt(i) + "-counted");
  15.             }
  16.         }
  17.         System.out.println(chSearch + "'s in the text: " + count);
  18.        
  19.         System.out.println("\n-----(finding chars and counting using charAt)-----\n");
  20.         count = 0;
  21.         for (int i = 0; i < txtIn.length(); i++) {
  22.             i = txtIn.toLowerCase().indexOf(chSearch,i);
  23.             if (i == -1) {
  24.                 i = txtIn.length();
  25.             } else {
  26.                 System.out.println(chSearch + " is in index " + i);
  27.                 count++;
  28.             }
  29.             //add an if else that if i == -1 then i == txtIn.length()
  30.         }
  31.         System.out.println(chSearch + "'s in the text using indexOf: " + count);
  32.     }
  33. }
  34. //----------------------------------
  35. class Day13C {
  36.     public static void main(String[] args) {
  37.         String csvText = "\"Number\",\"Team Name\",\"Member\"";
  38.         String[] csvArray = csvText.split(",");
  39.         for (String string : csvArray) {
  40.             System.out.print(string + " | ");
  41.         }
  42.        
  43.         String[] csvArray2 = {"Number,Team Name,Member"
  44.             ,"1,Team 1,PRINCESS ANN RENDON"
  45.             ,"2,Team 1,CARL REX ESPORSADO"
  46.             ,"3,Team 1,REZEL MAE LOPEZ"
  47.             ,"4,Team 1,HANNAH LYNDA BUSTILLO"
  48.             ,"5,Team 1,JOHN PETER CATINOY"
  49.             ,"6,Team 1,RONA FE PUYONG"
  50.             ,"7,Team 2,RAYMOND UBALDO"
  51.             ,"8,Team 2,JANICE ALVAREZ"
  52.             ,"9,Team 2,IRADE PIANO"
  53.             ,"10,Team 2,RAYMART NAPOLE"
  54.             ,"11,Team 2,IRISH FYE SAMPOLLO"
  55.             ,"12,Team 2,ELLEN MAE CARJASAN"
  56.             ,"13,Team 2,VANISSA PEDRO"
  57.             ,"14,Team 3,FLORA MAY ALAGAO"
  58.             ,"15,Team 3,EVANGELINE SULLAGA"
  59.             ,"16,Team 3,CLOEY MARK AMEDO"
  60.             ,"17,Team 3,NORLIN NICOLE CABIGUNDA"
  61.             ,"18,Team 3,JAYLYN GUZAREM"
  62.             ,"19,Team 3,CHRISTIAN ARQUERO"
  63.             ,"20,Team 4,JOMALYN MATANDAC"
  64.             ,"21,Team 4,JANICE BALONGA"
  65.             ,"22,Team 4,JEAN JUSTA SALINAS"
  66.             ,"23,Team 4,TINA JULIA CALAUOR"
  67.             ,"24,Team 4,ROSE ANN APOSTOL"
  68.             ,"25,Team 4,JOHN IAN SALIDO"
  69.             };
  70.         for (String str : csvArray2) {
  71.             System.out.println(str);
  72.         }
  73.  
  74.     }
  75. }
  76. //-----------------------------------------------
  77.  
  78. public class Day13D {
  79.     public static void main(String[] args) {
  80.         System.out.println("\n-----(1)-----\n");
  81.         String txt1 = "Mississippi";
  82.         System.out.println(txt1);
  83.         txt1 = txt1.replace("s", "p");
  84.         System.out.println(txt1);
  85.         System.out.println(txt1.replace("i", "p"));
  86.         //Mpppppppppp
  87.        
  88.         System.out.println("\n-----(2)-----\n");
  89.         boolean change = true;
  90.         txt1 = "A Cut Below";
  91.         String txtResult1 = "", txtResult2 = "";
  92.         for (char ch: txt1.toCharArray()) {
  93.             txtResult1 += ch + "|";
  94.             if (ch != ' ') {
  95.                 if (change) {
  96.                     txtResult2 += String.valueOf(ch).toUpperCase() + "_";
  97.                     change = false;
  98.                 } else {
  99.                     txtResult2 += String.valueOf(ch).toLowerCase()+ "_";
  100.                     change = true;
  101.                 }
  102.             } else {
  103.                 txtResult2 += ch + "_";
  104.             }
  105.         }
  106.         System.out.println("1: " + txtResult1 );
  107.         System.out.println("2: " + txtResult2 );
  108.        
  109.         System.out.println("\n-----(3)-----\n");
  110.         txt1 = "Sandwiches";
  111.         System.out.println(txt1);
  112.         System.out.println(txt1.substring(4));
  113.         System.out.println(txt1.substring(0, 4));
  114.        
  115.        
  116.         System.out.println("\n-----(4)-----\n");
  117.         int startNum = 0;
  118.         int endNum = 0;
  119.         boolean isPrevSpace = true;
  120.         txt1 = "  As    above     so  below";
  121.         System.out.println(txt1);
  122.         System.out.println("trimmed: " + txt1.trim());
  123.         for (int i = 0; i < txt1.length(); i++) {
  124.             if (txt1.charAt(i) != ' ' && isPrevSpace) {
  125.                 startNum = i;
  126.                 isPrevSpace = false;
  127.                 System.out.println("s:"+startNum);
  128.             } else if (txt1.charAt(i) == ' ' && !isPrevSpace) {
  129.                 endNum = i;
  130.                 isPrevSpace = true;
  131.                 System.out.println("e:"+endNum);
  132.                 System.out.println("|"+txt1.substring(startNum, endNum)+"|");
  133.             }
  134.         }
  135.     }
  136. }
Advertisement
Add Comment
Please, Sign In to add comment