desislava_topuzakova

Demo: String

Mar 27th, 2022
766
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.05 KB | None | 0 0
  1. package TextProcessing;
  2.  
  3. import java.util.Locale;
  4. import java.util.Scanner;
  5.  
  6. public class Demo {
  7.     public static void main(String[] args) {
  8.         Scanner scanner = new Scanner(System.in);
  9.         String name = "Desi"; //['D', 'e', 's', 'i']
  10.         String lastName = scanner.nextLine();
  11.  
  12.         System.out.println(name.charAt(0)); //първа буква: 'D'
  13.         System.out.println(name.charAt(name.length() - 1)); //последна буква: 'i'
  14.  
  15.         System.out.println(name.length()); //брой на символи = 4
  16.         System.out.println(name.toUpperCase()); //DESI
  17.         System.out.println(name.toLowerCase()); //desi
  18.  
  19.         System.out.println(name.equals(lastName)); //false
  20.         System.out.println(name.equals("Desi")); //true
  21.         //true -> двата текста са еднакви
  22.         //false -> двата текста не са еднакви
  23.  
  24.         char [] symbols = name.toCharArray(); //['D', 'e', 's', 'i']
  25.  
  26.         System.out.println(name + " " + lastName); //str + str + str = str (конкатенация)
  27.         System.out.println(name + 12); //str +  int = str
  28.         System.out.println(name + 34.5); //str +  double = str
  29.  
  30.         String sentence = "The cat is under the table";
  31.         sentence = sentence.replaceAll(" ", "_");
  32.         System.out.println(sentence); //"The_cat_is_under_the_table"
  33.  
  34.         String firstWord = sentence.substring(0, 3);
  35.         System.out.println(firstWord); //The
  36.  
  37.         String lastWord = sentence.substring(21);
  38.         System.out.println(lastWord); //table
  39.  
  40.         System.out.println(sentence.contains("under")); //true
  41.         System.out.println(sentence.contains("dog")); //false
  42.         //true -> в текста имаме дадената дума
  43.         //false -> в текста нямаме дадената дума
  44.  
  45.         String[] words = sentence.split("_"); //["The", "cat", "is", "under", "the", "table"]
  46.  
  47.         System.out.println(sentence.indexOf('u'));//11
  48.         System.out.println(sentence.lastIndexOf('e')); //25
  49.  
  50.     }
  51. }
  52.  
Advertisement
Add Comment
Please, Sign In to add comment