Wednesday, August 31, 2011

Lesson1 Strings Part1

Sample1

/**
       * @author dulldrifter
       * @throws ParseException
       */
      public static void main(String[] args) throws ParseException {
            int i = 10;
            byte b = 10;
            long l = 10L;
            float f = 10.1012F;
            double d = 10.123;
            short s = 10;
            char[] ch = { 'a', 'b', 'c', 'd' };
            String str1 = "10";
            String str2 = "Simple String";

            System.out.println(Integer.toHexString(i));// a
            System.out.println(Integer.toBinaryString(100));// 1100100
            System.out.println(String.valueOf(i));// 10
            System.out.println(String.valueOf(b));// 10
            System.out.println(String.valueOf(l)); // 10
            System.out.println(String.valueOf(f)); // 10.1012
            System.out.println(String.valueOf(d)); // 10.123
            System.out.println(String.valueOf(f)); // 10
            System.out.println(String.valueOf(ch));// abcd
            System.out.println(String.valueOf(true));// true

            System.out.println(Integer.parseInt(str1));
            // and so on

            System.out.println(str2.toLowerCase());// simple string
            System.out.println(str2.toUpperCase());// SIMPLE STRING

            SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
            Date date = dateFormat.parse("01/01/2011");
            System.out.println(dateFormat.format(date));// 01/01/2011

            String time = "10:10:15";
            DateFormat sdf = new SimpleDateFormat("hh:mm:ss");
            date = sdf.parse(time);
            System.out.println("Date and Time: " + date);// Date and Time: Thu Jan 01 10:10:15 EET 1970
           System.out.println("\b");// Backspace
            System.out.println("\f");// Form feed
            System.out.println("\n");// New line
            System.out.println("\r");// Carriage return
            System.out.println("\t");// Tab
      }
Sample2
      /**
       * @author dulldrifter
       * @throws ParseException
       */
      public static void main(String[] args) throws ParseException {
            String str = "Simple String";
            System.out.println(str.length());// 13
            System.out.println(str.concat("_"));// Simple String_
            System.out.println(str.charAt(2));// m
            System.out.println(str.lastIndexOf('i'));// 10
            System.out.println(str.lastIndexOf('A'));// -1
            System.out.println(str.replace('i', 'I'));// SImple StrIng
            System.out.println(str.substring(0, 4));// Simp

            String[] arr = str.split(" ");
            for (String s : arr) System.out.print(s);// SimpleString
           
            String text = "Hello. I am DullDrifter, who are you?";
            String[] words = text.split("[. ,?]", 0);
            for(String s: words)
            System.out.print(s);//HelloIamDullDrifterwhoareyou
               
            String str="Hello from DullDrifter";
            System.out.println(str.startsWith("Hello"));//true
            System.out.println(str.endsWith("Drifter"));//true
           
            String text = "DullDrifter";
            for(byte b: text.getBytes())
            System.out.print(b);//6811710810868114105102116101114
}

}

No comments:

Post a Comment