dzungchaos

Java: VD về String

Jul 2nd, 2020
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.30 KB | None | 0 0
  1. public class TVString {
  2.  
  3.     public static int count(String s, char c) {
  4.         int d = 0;
  5.         for(int i = 0; i < s.length(); i++)
  6.             if(s.charAt(i) == c) d++;
  7.         return d;
  8.     }
  9.  
  10.     public static int count(String s1, String s2) {
  11.         int n,d = 0;
  12.         n = s1.indexOf(s2);
  13.         while (n >= 0)
  14.         {
  15.             d++;
  16.             n = s1.indexOf(s2,n + s2.length());
  17.         }
  18.         return d;
  19.     }
  20.  
  21.     public static int countWords(String s)
  22.     {
  23.         int n,d = 0;
  24.         if (s.length()>0) d = 1;
  25.         n = s.indexOf(" ");
  26.         while (n >= 0)
  27.         {   d++;
  28.             n = s.indexOf(" ",n+1); }
  29.         return d;
  30.     }
  31.  
  32.     public static String ten(String ht)
  33.     {
  34.         int n; String kq;
  35.         ht = ht.trim();
  36.         n = ht.lastIndexOf(" ");
  37.         kq = ht.substring(n+1);
  38.         return kq;
  39.     }
  40.  
  41.     public static String ho(String ht)
  42.     {
  43.         int n; String kq;
  44.         ht = ht.trim();
  45.         n = ht.indexOf(" ");
  46.         kq = ht.substring(0,n);
  47.         return kq;
  48.     }
  49.  
  50.     public static String dem(String ht){
  51.         int m,n;
  52.         String kq;
  53.         ht = ht.trim();
  54.  
  55.         m = ht.indexOf(" ");
  56.         n = ht.lastIndexOf(" ");
  57.         kq = ht.substring(m+1, n);
  58.  
  59.         return kq;
  60.     }
  61.  
  62.  
  63. }
Add Comment
Please, Sign In to add comment