serving the solutions day and night

Pages

Monday, March 21, 2011

JAVA String

  1. Convert to String
    //Integer to String
    int number = 1234;
    String strtoint = Integer.toString(number); //1234
    strtoint = "" + number; //1234
    //Double to String
    strtoint = Double.toString(number); //1234.0
    //Long to String
    strtoint = Long.toString(number); //1234
    //Flot to String
    strtoint = Float.toString(number); //1234.0
    //Binary to String
    strtoint = Integer.toBinaryString(number); //10011010010
    strtoint = Integer.toString(number,2); //10011010010
    //Hexadecimal to String
    strtoint = Integer.toHexString(number); //4d2
    strtoint = Integer.toString(number,16); //4d2
  2. Convert from String
    //String to Integer
    String number = "1234";
    int inumber = Integer.valueOf(number).intValue();
    inumber = Integer.parseInt(number);
    //String to Double
    Double dnumber = Double.valueOf(number).doubleValue();
    dnumber = Double.parseDouble(number);
    //String to Long
    Long lnumber = Long.valueOf(number).longValue();
    lnumber = Long.parseLong(number);
    //String to Flot
    Float fnumber = Float.valueOf(number).floatValue();
    fnumber = Float.parseFloat(number);
    //ASCII Code
    char c = "D";
    inumber = (int) c; //66
    String test = "ABCD";
    for( int i = 0; i < test.length(); ++i ) {
      c = test.charAt( i );
      inumber = (int) c;
      System.out.println(inumber);
    }
  3. Tokenizer
    import java.util.StringTokenizer;
    StringTokenizer st = new StringTokenizer("this is a test tokenized");
    //StringTokenizer st = new StringTokenizer("this is a test tokenized"," ");
    while(st.hasMoreTokens()){
      System.out.println(st.nextToken());
    }
    It is recommended that anyone seeking this functionality use the split method of String or the java.util.regex package instead.
    String[] result = "this is a test tokenized".split("\\s");
    for (int x=0; x < result.length; x++)
      System.out.println(result[x]);
  4. Split
    String str = "Day-Night-Soft-Blog";
    java.util.Arrays.toString(str.split("-")); //[Day, Night, Soft, Blog]
    The special character must be escaped with a "\", but in java "\" is also a special character, so need to escape it again with another "\\".
    String[] arrLines = null;
    str = "Day.Night.Soft.Blog";
    //wrong way split
    arrLines = str.split(".");
    //java.util.Arrays.toString(arrLines); //[]
    arrLines = str.split("\\.");
    ava.util.Arrays.toString(arrLines); //[Day, Night, Soft,Blog] good way split
    str = "Day|Night|Soft|Blog";
    //wrong way split
    //java.util.Arrays.toString(str.split("|")); //[, D, a, y, |, N, i, g, h, t, |, S, o, f, t, |, B, l, o, g]
    java.util.Arrays.toString(str.split("\\|")); //[Day, Night, Soft, Blog] good way split
    //to include empty strings
    str = "Day;Night;Soft;Blog;;java;;;";
    arrLines = str.split("\\;");
    //The output does not contain the empty strings between the ";" separator.
    //java.util.Arrays.toString(arrLines); //[Day, Night, Soft, Blog, , java]
    //to keep empty strings
    arrLines = str.split("\\;", -1);
    java.util.Arrays.toString(arrLines); //[Day, Night, Soft, Blog, , java, , ,]
    //muliple split
    str = "day+night*s-soft*s^2-blog-221";
    arrLines = str.split("[-+*]", -1);
    java.util.Arrays.toString(arrLines); //[day, night, s, soft, s^2, blog, 221]
  5. String Builder
    StringBuilder sb = new StringBuilder(100);
    for (int i = 0; i < 50; i++)
        sb.append(i).append('\n');
  6. Trim
    String str = " Day-Night-Soft-Blog ";
    str.trim(); //Day-Night-Soft-Blog;
  7. Regex Pattern
    String starts with digit or upper case
    import java.util.regex.Pattern;
    String str ="daynightsoft123";
    Pattern.compile("^[A-Z0-9]").matcher(str).find(); //false
    str ="123daynightsoft";
    Pattern.compile("^[A-Z0-9]").matcher(str).find(); //true
    str ="Daynightsoft123";
    Pattern.compile("^[A-Z0-9]").matcher(str).find(); //true
  8. Replace
    "Daynightsoft*s".replace( '*', '\'' ); //Daynightsoft's
    \d digit, \w work token, special charaters
    String str ="@*1#^2$@!34#5ajs67>?<{8_(9SKJDH";
    str.replaceAll("\\W", ""); //12345ajs678_9SKJDH
    str.replaceAll("\\w", ""); //@*#^$@!#>?<{(
    str.replaceAll("\\D", ""); //123456789
    str.replaceAll("\\d", ""); //@*#^$@!#ajs>?<{_(SKJDH
    "090909".replaceAll("(\\d\\d)", "XX$1"); //XX09XX09XX09
  9. Regex Matcher
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    public static String matcherFirstWordUpper(String str) {
    StringBuffer sb = new StringBuffer();
    Matcher m = Pattern.compile("([a-z])([a-z]*)",Pattern.CASE_INSENSITIVE).matcher(str);
    while (m.find()) {
    m.appendReplacement(sb, m.group(1).toUpperCase() + m.group(2).toLowerCase()) ;
    }
    return m.appendTail(sb).toString();
    }

    firstWordUpper(" #100day-night-soft-blogs"); //  #100Day-Night-Soft-Blogs
    firstWordUpper("GOOgle andRiod"); //Google Andriod
    frstWordUpper("honda o'daniel & toyoto mcmillan"); //Honda O'Daniel & Toyoto Mcmillan
  10. Format
    String s = String.format("It is a %s blog(%s)", "Java's String", "makdns.blogspot.com");
    System.out.println(s); //It is a Java's String blog(makdns.blogspot.com)
    Object obj[] = {"Java's String", "makdns.blogspot.com", java.util.Calendar.getInstance()};
    String s = String.format("It is a %s at %s blog (%3$tm %3$td, %3$tY)", obj);
    System.out.println(s); //It is a Java's String at makdns.blogspot.com blog (03 22, 2011)
    System.out.println(String.format("%4$s %3$s %2$2s %1$2s", "a", "b", "c", "d")); //d c b a
    Printf method (behavior is based on the C sprintf) - to write a formatted string to this output stream using the specified format string and arguments.
    System.out.printf("It is a %s blog(%s)", "Java's String", "makdns.blogspot.com");

No comments: