memecah String dengan kelas StringTokenizer

🙂😀

Daftar Materi Java SE

operasi memecah String dapat dilakukan dengan berbagai cara,  sekarang akan kita bahas contohnya menggunakan kelas StringTokenizer

berikut ini adalah konstruktor dari kelas StringTokenizer

  • StringTokenizer(String str)
    StringTokenizer(String str, String delimiters)
    StringTokenizer(String str, String delimiters, boolean delimAsToken)

mari kita coba

import java.util.StringTokenizer;
public class StringTokenTest {

  public static void main(String[] args) {
    String s = "{lahardi}]],{alka}]],[[{wero}";
    String[] potonganS = new String[3];
    StringTokenizer token1 = new StringTokenizer(s, ",[]{}");
    int i=0;
    while(token1.hasMoreTokens()){
      potonganS[i] = token1.nextToken();
      i++;
    }
    System.out.println(potonganS[0]);
    System.out.println(potonganS[1]);
    System.out.println(potonganS[2]);
  }
}

atau kita coba String yang lebih panjang lalu kita print potongan demi potongan

import java.util.StringTokenizer;

public class StringTokenTest {

  public static void main(String[] args) {
    String detail = "[{id_item=6; nama_item=timlo solo; alamat=jalan solo no.1;"
            + "deskripsi=menu utama:timlo dengan daging ayam; longitude=110.5675; "
            + "latitude=-75655;gambar=timlosolo.jpg; },{id_item=8; "
            + "nama_item=timlo sastro; alamat=jalan solo no.2; "
            + "deskripsi=menu utama:timlo dengan daging ayam; "
            + "longitude=110.5675; latitude=-75655; gambar=timlosastro.jpg; },"
            + "{id_item=13 ; nama_item=sate kambing; alamat=jalan solo no.3; "
            + "deskripsi=menu utama:sate dengan daging kambing tersedia pula sate ayam;"
            + " longitude=110.5675; latitude=-75655; gambar=satekambing.jpg; }]";

    StringTokenizer token = new StringTokenizer(detail, "[]{}=;,");
    while (token.hasMoreTokens()) {
      token.nextToken();
      Integer id_val = Integer.parseInt(token.nextToken().trim());
      System.out.print(" ID : " + id_val);
      token.nextToken();
      String nama_val = token.nextToken();
      System.out.print(" Nama : " + nama_val);
      token.nextToken();
      String alamat_val = token.nextToken();
      System.out.print(" Alamat : " + alamat_val);
      token.nextToken();
      String desk = token.nextToken();
      System.out.print(" Deskripsi : " + desk);
      token.nextToken();
      Float lot = Float.parseFloat(token.nextToken());
      token.nextToken();
      Float lat = Float.parseFloat(token.nextToken());
      token.nextToken();
      String gbr = token.nextToken();
      System.out.println("");
      token.nextToken();
    }
}
}

🙂😀

Daftar Materi Java SE

One comment on “memecah String dengan kelas StringTokenizer

silahkan ditanggapi :)

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s