Thursday, March 5, 2015

OCPJP7 String Processing

1.    String.length()
       String[].length---->Array

2.    String.charAt(): start from 0
       String.concat()
       String.equalIgnoreCase()
       String.length()
       String.replace()
       String.substring(including_index, excluding_index): start from 0
       String.toLowerCase()
       String.toUpperCase()
       String.trim()
       String.toString()

3.    new StringBuilder(); default capacity 16 chars(16*16 bits)
       new StringBuilder("ab");
       new StringBuilder(int capacity);

4.    Following methods are not used in String!!!
       StringBuilder.append()
       StringBuilder.delete(inclusive, exclusive): start from 0
       StringBuilder.reverse()
       StringBuilder.replace(inclusive_index, exclusive_index, String str)
       StringBuilder.insert(offset from 0 to length, String str): start from 0,
       (runtime exception thrown if offset is out of current length)

5.    Searching
       Pattern p = Pattern.compile(target);
       Matcher m = p.matcher(source);
       while(m.find()) {
           System.out.println(m.start() + m.end() + m.group());
       }
     
       Target
        a. Metacharacter:         1) \d (0-9)
                                           2) \D (NOT \d)
                                           3) \s (\s,\t, \n, \r, \f)
                                           4) \S (NOT \s)
                                           5) \w (a-z, A-Z, 0-9, underscore)
                                           6) \W (NOT \w)

         b. Boundary Macthing: \b:  start from position of first Word character, after that record position when previous character is different with current one.
                                           \B:  (NOT \b)

         c. Range: [abc] or [a-f] or [afAF] (-is needed)

         d. Quantifiers:
                                 + one or more (greedy)
                                 * zero or more (greedy)
                                 ? zero or one (greedy)
                                 ^ negative
                                 +? one or more (reluctant)
                                 *? zero or more (reluctant)
                                 ?? zero or one (reluctant)
                                  . any one not null


6. Tokenizing:

    a) String[] tokens = source.split(tokenizing);
        for(String s: tokens) System.out.println(">" + s + "<")

    b) Scanner(default whitespace, usedelimiter(string) to set delimiter)
        Scanner s1 = new Scanner(source);
        s1.hasNext()
        s1.hasNextInt() (1.3 IS OKAY)
        s1.nextInt()
        s1.nextBoolean
        s1.next()

     c) StringTokenizer(default whitespace)
         StringTokenizer st = new StringTokenizer("source");
         st.countTokens()
         st.hasMoreTokens()
         st.nextToken()

7. System.out.println("\" \\"); ----> " \
    Source.split("\\."); search for period.

8. System.out.printf() == System.out.format()

9. %[arg_index$] [flags][width][.precision] conversion char

    flags: - left justify
             + include a sign (+/-)
             0 pad with zero
             ,  use locale-specific
             ( enclose negative numbers in parentheses )
    width:
    precision: (floating point)
    conversion : b|c|d|f|s  (Barring use of boolean, mismatch will get a run time exception)

10. System.out.printf("%d", 123.2);  ERROR!!!
      System.out.printf("%f", 123);      ERROR!!!
      System.out.printf("%b", 123.1);  GOOD!!!

No comments:

Post a Comment