Friday, October 14, 2011

                                                              STRING


Program Kelas String dalam Java
                                                                                                                 by.joel pantora
import java.lang.Character;
import java.lang.String;
import java.lang.StringBuffer;

class KelasString {
      public static void main(String args[]){
       
            //cara pengaksesan variable/objeck String
             String java1 = new String("JAVA IS FUNNY");
             String java2 = "Java is Easy";
             System.out.println("Isi variable java1 : "+java1.toLowerCase());
             System.out.println("Isi variable java2 : "+java2.toUpperCase());
             System.out.println("Isi variable java2 : "+java2.replace('A','#'));
             System.out.println("Karakter ketiga java2 : "+java2.charAt(2));
                                   
             System.out.println("\n*--------------------------------------*\n");
             
             //cara pengaksesan variable/objeck String
             StringBuffer data;
             data = new StringBuffer("Write One Run Anywhere");
             System.out.println("String asal : "+ data);
             data.replace(14,17,"Every"); //replace(awal,akhir,"pengganti")
             System.out.println("String menjadi : " + data);
             
             StringBuffer data1=new StringBuffer("I Like Java Coffee Very Much");
             System.out.println(data1);
             data1.delete(12,19);
             System.out.println("String menjadi : " + data1);
             
             System.out.println("\n*--------------------------------------*\n");
             
             //cara pengaksesan variable/objeck Char
             Character latihan = new Character('S');    
             System.out.println("Isi variable latihan : "+latihan.charValue());
      }
}


Program menghitung panjang String dalam Java

class PanjangString {
      public static void main(String arg[]) {
            String s1 = "Write Once Run Anywhere";
            String s2 = new String("I live in Java");
            int panjang;
           
            panjang = s1.length();
            System.out.println("Panjang String \""+ s1 +"\" = " + panjang);
            panjang = s2.length();
            System.out.println("Panjang String \""+ s2 +"\" = " +panjang);
      }
}


Program contoh penggunaan substring() dalam Java

class Substring {
      public static void main(String args[]) {
            String str = new String("Jang Nara");
            //Substring(awal,akhir);
            System.out.println("Jang Nara -> Substring(2) : " + str.substring(2));
            System.out.println("Jang Nara -> Substring(2,3) : "+str.substring(5,9));
           
            //Contoh lain pemakaian metode dalam objek String
            System.out.println("Sahabat(4) = "+ "Sahabat".substring(4));
            System.out.println("Sahabat(2,4) = "+ "Sahabat".substring(2,4));
      }
}


AwalAkhir.java
Program untuk memeriksa awal dan akhir suatu String

class AwalAkhir {
      public static void main(String arg[]) {
            String str = new String("Utada Hikaru");
            //apakah str diawali dengan....?
            System.out.println("String str = " + str);
            System.out.println("String diawali dengan 'Ut' ? "+
            str.startsWith("Ut"));
            System.out.println("String diawali dengan 'ut' ? "+
            str.startsWith("ut"));
            //apakah str diakhiri dengan...?
            System.out.println("String diakhiri dengan 'Ru' ? "+
            str.endsWith("Ru"));
            System.out.println("String diakhiri dengan 'ru' ? "+
            str.endsWith("ru"));
      }
}


 
Program untuk menghilangkan Whitespace di akhir dan awal string

class Trims {
      public static void main(String srgs[]) {
            String str  = "  Jang Nara ";
            String str2 = "Utada    Hikaru              ";
            System.out.println("String str = \""+ str +"\"");
            System.out.println("String trim(str) = \""+ str.trim() +"\"");
            System.out.println("\nString str2 = \""+ str2 + "\"");
            System.out.println("String trim(str2) = \""+ str2.trim() + "\"");
      }
}

Program penggunaan fungsi-fungsi Karakter dalam Java

import java.io.*;
class Karakter {
      public static void main(String ars[]) {
            BufferedReader tanya;
            char apa;
            try {
                  System.out.print("Masukkan sebuah karakter : ");
                  tanya = new BufferedReader(new InputStreamReader(System.in));
                  apa = (tanya.readLine()).charAt(0);
                 
                  //isDigit()
                  if(Character.isDigit(apa)) {
                        System.out.println("Karakter Digit");
                  }
                 
                  //isLetter()
                  if(Character.isLetter(apa)) {
                        System.out.println("Karakter Letter");
                  }
                 
                  //isWhitespace()
                  if(Character.isWhitespace(apa)) {
                        System.out.println("Karakter WhiteSpace");
                  }
                       
                  //isLowerCase()
                  if(Character.isLowerCase(apa)) {
                        System.out.println("Karakter Huruf Kecil");
                  }
                 
                  //isUpperCase()
                  if(Character.isUpperCase(apa)) {
                        System.out.println("Karakter Huruf Besar");
                  }
            } catch(IOException err) {
                  System.out.println("Error I/O");
                  System.exit(0);
            }
      }
}

Program menampilkan kode Ascii

class Ascii {
      public static void main(String args[]) {
            int i = 0;
            char c;
            for(i=0; i<256; i++) {
                  c = (char)i;            //konversi dari integer ke char
                  System.out.println(i +"\t"+ c);
            }
      }
}

String dan Number Format

import java.text.DecimalFormat;
public class FormatTest {
    public static void main(String[] args) {
        DecimalFormat myformat = new DecimalFormat("0.0000");
        for (int i=1; i<=10; i++) {
            System.out.println(myformat.format(1.0/i));
        }
    }
}   

0 komentar: