Java/Development Class/Char Text — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Текущая версия на 07:00, 1 июня 2010
Содержание
Break Iterator Demo
/* From http://java.sun.ru/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
Capitalizing each word in a sentence with loop
public class Main {
public static void main(String[] a) {
String sentence = "this is a test";
String tmp = "";
String[] words = sentence.split(" ");
for (int i = 0; i < words.length; i++) {
tmp += words[i].substring(0, 1).toUpperCase();
tmp += words[i].substring(1).toLowerCase();
tmp += " ";
}
System.out.println(tmp.trim());
}
}
Capitalizing each word in a sentence with recursive function
public class Main {
public static void main(String[] args) {
String sentence = "this is a test";
System.out.println(capSentence(sentence, true));
}
public static String capSentence(String string, boolean capitalize) {
if (string.length() == 0) {
return "";
}
String c = string.substring(0, 1);
if (capitalize) {
return c.toUpperCase() + capSentence(string.substring(1), c.equals(" "));
} else {
return c.toLowerCase() + capSentence(string.substring(1), c.equals(" "));
}
}
}
Character Demo
/*
* Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* -Redistribution of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* -Redistribution in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* Neither the name of Sun Microsystems, Inc. or the names of contributors may
* be used to endorse or promote products derived from this software without
* specific prior written permission.
*
* This software is provided "AS IS," without a warranty of any kind. ALL
* EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING
* ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
* OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN MIDROSYSTEMS, INC. ("SUN")
* AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE
* AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
* DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST
* REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL,
* INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY
* OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE,
* EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
*
* You acknowledge that this software is not designed, licensed or intended
* for use in the design, construction, operation or maintenance of any
* nuclear facility.
*/
public class CharacterDemo {
public static void main(String args[]) {
Character a = new Character("a");
Character a2 = new Character("a");
Character b = new Character("b");
int difference = a.rupareTo(b);
if (difference == 0) {
System.out.println("a is equal to b.");
} else if (difference < 0) {
System.out.println("a is less than b.");
} else if (difference > 0) {
System.out.println("a is greater than b.");
}
System.out.println("a is " + ((a.equals(a2)) ? "equal" : "not equal")
+ " to a2.");
System.out.println("The character " + a.toString() + " is "
+ (Character.isUpperCase(a.charValue()) ? "upper" : "lower")
+ "case.");
}
}
Keys Demo
/* From http://java.sun.ru/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.CollationKey;
import java.text.Collator;
import java.util.Locale;
public class KeysDemo {
public static void sortArray(CollationKey[] keys) {
CollationKey tmp;
for (int i = 0; i < keys.length; i++) {
for (int j = i + 1; j < keys.length; j++) {
// Compare the keys
if (keys[i].rupareTo(keys[j]) > 0) {
// Swap keys[i] and keys[j]
tmp = keys[i];
keys[i] = keys[j];
keys[j] = tmp;
}
}
}
}
static void displayWords(CollationKey[] keys) {
for (int i = 0; i < keys.length; i++) {
System.out.println(keys[i].getSourceString());
}
}
static public void main(String[] args) {
Collator enUSCollator = Collator.getInstance(new Locale("en", "US"));
String[] words = { "peach", "apricot", "grape", "lemon" };
CollationKey[] keys = new CollationKey[words.length];
for (int k = 0; k < keys.length; k++) {
keys[k] = enUSCollator.getCollationKey(words[k]);
}
sortArray(keys);
displayWords(keys);
}
}
Rules Demo
/* From http://java.sun.ru/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.Collator;
import java.text.ParseException;
import java.text.RuleBasedCollator;
public class RulesDemo {
public static void sortStrings(Collator collator, String[] words) {
String tmp;
for (int i = 0; i < words.length; i++) {
for (int j = i + 1; j < words.length; j++) {
// Compare elements of the words array
if (collator.rupare(words[i], words[j]) > 0) {
// Swap words[i] and words[j]
tmp = words[i];
words[i] = words[j];
words[j] = tmp;
}
}
}
}
public static void printStrings(String[] words) {
for (int i = 0; i < words.length; i++) {
System.out.println(words[i]);
}
}
static public void main(String[] args) {
String englishRules = ("< a,A < b,B < c,C < d,D < e,E < f,F "
+ "< g,G < h,H < i,I < j,J < k,K < l,L "
+ "< m,M < n,N < o,O < p,P < q,Q < r,R "
+ "< s,S < t,T < u,U < v,V < w,W < x,X " + "< y,Y < z,Z");
String smallnTilde = new String("\u00F1");
String capitalNTilde = new String("\u00D1");
String traditionalSpanishRules = ("< a,A < b,B < c,C "
+ "< ch, cH, Ch, CH " + "< d,D < e,E < f,F "
+ "< g,G < h,H < i,I < j,J < k,K < l,L " + "< ll, lL, Ll, LL "
+ "< m,M < n,N " + "< " + smallnTilde + "," + capitalNTilde
+ " " + "< o,O < p,P < q,Q < r,R "
+ "< s,S < t,T < u,U < v,V < w,W < x,X " + "< y,Y < z,Z");
String[] words = { "luz", "curioso", "llama", "chalina" };
try {
RuleBasedCollator enCollator = new RuleBasedCollator(englishRules);
RuleBasedCollator spCollator = new RuleBasedCollator(
traditionalSpanishRules);
sortStrings(enCollator, words);
printStrings(words);
System.out.println();
sortStrings(spCollator, words);
printStrings(words);
} catch (ParseException pe) {
System.out.println("Parse exception for rules");
}
}
}