Break Iterator Demo : Char Text « Development Class « Java

Home
Java
1.2D Graphics GUI
2.3D
3.Advanced Graphics
4.Ant
5.Apache Common
6.Chart
7.Class
8.Collections Data Structure
9.Data Type
10.Database SQL JDBC
11.Design Pattern
12.Development Class
13.EJB3
14.Email
15.Event
16.File Input Output
17.Game
18.Generics
19.GWT
20.Hibernate
21.I18N
22.J2EE
23.J2ME
24.JavaFX
25.JDK 6
26.JDK 7
27.JNDI LDAP
28.JPA
29.JSP
30.JSTL
31.Language Basics
32.Network Protocol
33.PDF RTF
34.Reflection
35.Regular Expressions
36.Scripting
37.Security
38.Servlets
39.Spring
40.Swing Components
41.Swing JFC
42.SWT JFace Eclipse
43.Threads
44.Tiny Application
45.Velocity
46.Web Services SOA
47.XML
Java » Development Class » Char Text 




Break Iterator Demo
Break Iterator Demo

/* From http://java.sun.com/docs/books/tutorial/index.html */

/*
 * Copyright (c) 1995-1998 Sun Microsystems, Inc. All Rights Reserved.
 
 * Permission to use, copy, modify, and distribute this software and its
 * documentation for NON-COMMERCIAL purposes and without fee is hereby granted
 * provided that this copyright notice appears in all copies. Please refer to
 * the file "copyright.html" for further important copyright and licensing
 * information.
 
 * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
 * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR
 * NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY
 * LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
 * DERIVATIVES.
 */

import java.text.BreakIterator;
import java.util.Locale;

public class BreakIteratorDemo {

  static void extractWords(String target, BreakIterator wordIterator) {

    wordIterator.setText(target);
    int start = wordIterator.first();
    int end = wordIterator.next();

    while (end != BreakIterator.DONE) {
      String word = target.substring(start, end);
      if (Character.isLetterOrDigit(word.charAt(0))) {
        System.out.println(word);
      }
      start = end;
      end = wordIterator.next();
    }
  }

  static void reverseWords(String target, BreakIterator wordIterator) {

    wordIterator.setText(target);
    int end = wordIterator.last();
    int start = wordIterator.previous();

    while (start != BreakIterator.DONE) {
      String word = target.substring(start, end);
      if (Character.isLetterOrDigit(word.charAt(0)))
        System.out.println(word);
      end = start;
      start = wordIterator.previous();
    }
  }

  static void markBoundaries(String target, BreakIterator iterator) {

    StringBuffer markers = new StringBuffer();
    markers.setLength(target.length() 1);
    for (int k = 0; k < markers.length(); k++) {
      markers.setCharAt(k, ' ');
    }

    iterator.setText(target);
    int boundary = iterator.first();

    while (boundary != BreakIterator.DONE) {
      markers.setCharAt(boundary, '^');
      boundary = iterator.next();
    }

    System.out.println(target);
    System.out.println(markers);
  }

  static void formatLines(String target, int maxLength, Locale currentLocale) {

    BreakIterator boundary = BreakIterator.getLineInstance(currentLocale);
    boundary.setText(target);
    int start = boundary.first();
    int end = boundary.next();
    int lineLength = 0;

    while (end != BreakIterator.DONE) {
      String word = target.substring(start, end);
      lineLength = lineLength + word.length();
      if (lineLength >= maxLength) {
        System.out.println();
        lineLength = word.length();
      }
      System.out.print(word);
      start = end;
      end = boundary.next();
    }
  }

  static void listPositions(String target, BreakIterator iterator) {

    iterator.setText(target);
    int boundary = iterator.first();

    while (boundary != BreakIterator.DONE) {
      System.out.println(boundary);
      boundary = iterator.next();
    }
  }

  static void characterExamples() {

    BreakIterator arCharIterator = BreakIterator
        .getCharacterInstance(new Locale("ar""SA"));
    // Arabic word for "house"
    String house = "\u0628" "\u064e" "\u064a" "\u0652" "\u067a"
        "\u064f";
    listPositions(house, arCharIterator);
  }

  static void wordExamples() {

    Locale currentLocale = new Locale("en""US");
    BreakIterator wordIterator = BreakIterator
        .getWordInstance(currentLocale);
    String someText = "She stopped.  "
        "She said, \"Hello there,\" and then went on.";
    markBoundaries(someText, wordIterator);
    System.out.println();
    extractWords(someText, wordIterator);
  }

  static void sentenceExamples() {

    Locale currentLocale = new Locale("en""US");
    BreakIterator sentenceIterator = BreakIterator
        .getSentenceInstance(currentLocale);
    String someText = "She stopped.  "
        "She said, \"Hello there,\" and then went on.";
    markBoundaries(someText, sentenceIterator);
    String variousText = "He's vanished!  "
        "What will we do?  It's up to us.";
    markBoundaries(variousText, sentenceIterator);
    String decimalText = "Please add 1.5 liters to the tank.";
    markBoundaries(decimalText, sentenceIterator);
    String donneText = "\"No man is an island . . . "
        "every man . . . \"";
    markBoundaries(donneText, sentenceIterator);
    String dogText = "My friend, Mr. Jones, has a new dog.  "
        "The dog's name is Spot.";
    markBoundaries(dogText, sentenceIterator);
  }

  static void lineExamples() {

    Locale currentLocale = new Locale("en""US");
    BreakIterator lineIterator = BreakIterator
        .getLineInstance(currentLocale);
    String someText = "She stopped.  "
        "She said, \"Hello there,\" and then went on.";
    markBoundaries(someText, lineIterator);
    String hardHyphen = "There are twenty-four hours in a day.";
    markBoundaries(hardHyphen, lineIterator);
    System.out.println();
    String moreText = "She said, \"Hello there,\" and then "
        "went on down the street.  When she stopped "
        "to look at the fur coats in a shop window, "
        "her dog growled.  \"Sorry Jake,\" she said. "
        " \"I didn't know you would take it personally.\"";
    formatLines(moreText, 30, currentLocale);
    System.out.println();
  }

  static public void main(String[] args) {

    characterExamples();
    System.out.println();
    wordExamples();
    System.out.println();
    sentenceExamples();
    System.out.println();
    lineExamples();
  }

// class

           
       














Related examples in the same category
1.Capitalizing each word in a sentence with loop
2.Capitalizing each word in a sentence with recursive function
3.Rules DemoRules Demo
4.Keys DemoKeys Demo
5.Character DemoCharacter Demo
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.