Advertisement
n4wn4w

STRINGS

Apr 7th, 2015
242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.43 KB | None | 0 0
  1. String str = "Hello";
  2.    
  3.     for(int i=0; i < str.length(); i++){
  4.         System.out.println(str.charAt(i));
  5.     }
  6.  
  7. /////////////////////////////////////////////////////////////////////////////////////////////////
  8.  
  9. String str = "SoftUni";
  10.         System.out.println(str);
  11.         for (int i = 0; i < str.length(); i++) {
  12.             System.out.printf("str[%d] = %s\n", i, str.charAt(i));
  13.         }
  14.         System.out.println(str.indexOf("Uni")); // 4
  15.         System.out.println(str.indexOf("uni")); // -1 (not found)
  16.         System.out.println(str.substring(4, 7)); // Uni
  17.         System.out.println(str.replace("Soft", "Hard")); // HardUni
  18.         System.out.println(str.toLowerCase()); // softuni
  19.         System.out.println(str.toUpperCase()); // SOFTUNI
  20.        
  21.         String firstName = "Steve";
  22.         String lastName = "Jobs";
  23.         int age = 56;
  24.         System.out.println(firstName + " " + lastName +
  25.             " (age: " + age + ")"); // Steve Jobs (age: 56)
  26.         String allLangs = "C#, Java; HTML, . .. . . CSS; PHP, SQL";
  27.         String[] langs = allLangs.split("[,. ;]+");
  28.         for (String lang : langs) {
  29.             System.out.println(lang);
  30.         }
  31.         System.out.println("Langs = " + String.join(", ", langs));
  32.                  
  33.         System.out.println("  \n\n Software   University         \r    ".trim());
  34.  
  35.  
  36. ////////////////////////////////////////////////////////////////////////////////////////
  37.  
  38. String[] words = "yes yes".split(" ");
  39.         System.out.println("words[0] = " + words[0]);
  40.         System.out.println("words[1] = " + words[0]);
  41.        
  42.         System.out.println(words[0] == words[1]); // false
  43.         System.out.println(words[0].equals(words[1])); // true
  44.        
  45.         if (words[0] == words[1]) {
  46.             System.out.println("words[0] == words[1]");
  47.         } else {
  48.             System.out.println("words[0] != words[1]");
  49.         }
  50.        
  51.         if (words[0] == "yes") {
  52.             System.out.println("words[0] == yes");
  53.         } else {
  54.             System.out.println("words[0] != yes");
  55.         }
  56.        
  57.         if (words[0].equals(words[1])) {
  58.             System.out.println("words[0].equals(words[1])");
  59.         }
  60.        
  61.         if (words[0].equals("yes")) {
  62.             System.out.println("words[0].equals(\"yes\")");
  63.         }
  64.        
  65.         System.out.println("Alice".compareTo("Mike")); // < 0
  66.         System.out.println("Alice".compareTo("Alice")); // == 0
  67.         System.out.println("Mike".compareTo("Alice")); // > 0
  68.  
  69.         if ("Alice".compareTo("Mike") < 0) {
  70.             System.out.println("Alice < Mike");
  71.         }
  72.        
  73.         if ("Alice".compareTo("Alice") == 0) {
  74.             System.out.println("Alice == Alice");
  75.         }      
  76.  
  77.         if ("Mike".compareTo("Alice") > 0) {
  78.             System.out.println("Mike > Alice");
  79.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement