char c = '\n'; String s1 = "\n"; String s2 = "Jon \"Maddog\" Orwant"; String s = "This is a multiline string declaration "
+ "using the concatenation operator "
+ "to append separate parts.";
String s1 = data.substring(start, end);
String s2 = data.substring(start);
String variableString = data;
variableString = variableString.substring(0, start) + newMiddle
+ variableString.substring(start);
variableString = variableString.substring(0, start) + newTail;
StringBuffer variableStringBuffer = new StringBuffer(data);
variableStringBuffer.replace(start, end, newMiddle);
variableStringBuffer.replace(start, variableStringBuffer.length(), newTail);
try {
String[] a = unpack("A5 x3 A8 A8 A*", data);
} catch (ParseException ex) {}
try {
String[] fivers = unpack(aggregate("A5 ", data.length() / 5), data);
} catch (ParseException ex) {}
try {
String[] chars = unpack(aggregate("A1 ", data.length()), data);
} catch (ParseException ex) {}
/**
* Unpacks a string into a string array following a given format.
*/
String[] unpack(String format, String data) throws ParseException {
ArrayList result = new ArrayList();
int formatOffset = 0;
int dataOffset = 0;
int minDataOffset = 0;
int maxDataOffset = data.length();
StringTokenizer tokenizer = new StringTokenizer(format);
while (tokenizer.hasMoreTokens()) {
String token = tokenizer.nextToken();
int tokenLen = token.length();
int count = 0;
if (tokenLen == 1)
count = 1;
else if (token.charAt(1) == '*')
count = -1;
else {
try {
count = new Integer(token.substring(1)).intValue();
} catch (NumberFormatException ex) {
throw new ParseException("Unknown count token", formatOffset);
}
}
char action = token.charAt(0);
switch (action) {
case 'A':
if (count == -1) {
int start = (dataOffset < maxDataOffset) ?
dataOffset : maxDataOffset;
result.add(data.substring(start));
dataOffset = maxDataOffset;
} else {
int start = (dataOffset < maxDataOffset) ?
dataOffset : maxDataOffset;
int end = (dataOffset + count < maxDataOffset) ?
dataOffset + count : maxDataOffset;
result.add(data.substring(start, end));
dataOffset += count;
}
break;
case 'x':
if (count == -1)
dataOffset = maxDataOffset;
else
dataOffset += count;
break;
case 'X':
if (count == -1)
dataOffset = minDataOffset;
else
dataOffset -= count;
break;
default:
throw new ParseException("Unknown action token", formatOffset);
}
formatOffset += tokenLen + 1;
}
return (String[]) result.toArray(new String[result.size()]);
}
/**
* Aggregates repetitly a basic pattern.
*/
String aggregate(String pattern, int count) {
StringBuffer buffer = new StringBuffer();
for (int i=0; i < count; i++)
buffer.append(pattern);
return buffer.toString();
}
String data = "This is what you have";
String first = data.substring(0, 1);
String start = data.substring(5, 7);
String rest = data.substring(13);
String last = data.substring(data.length() - 1);
String end = data.substring(data.length() - 4);
String piece = data.substring(data.length() - 8, data.length() - 5);
String data = "This is what you have";
data = data.substring(0, 5) + "wasn't" + data.substring(7);
data = data.substring(0, data.length() - 12) + "ondrous";
data = data.substring(1);
data = data.substring(0, data.length() - 10);
import org.apache.oro.text.perl.Perl5Util;
Perl5Util util = new Perl5Util();
if (util.match("/pattern/", data.substring(data.length() - 10)))
System.out.println("Pattern matches in last 10 characters");
data = util.substitute("s/is/at/g", data.substring(0, 5)) + data.substring(5);
String data = "make a hat";
data = data.charAt(data.length() -1) + data.substring(1, data.length() - 1)
+ data.charAt(0); String data = "To be or not to be";
try {
String[] a1 = unpack("x6 A6", data); } catch (ParseException ex) {}
try {
String[] a2 = unpack("x6 A2 X5 A2", data); } catch (ParseException ex) {}
String cut2fmt(int[] positions) {
StringBuffer sb = new StringBuffer();
int end = 1;
for (int i=0; i < positions.length; i++) {
sb.append("A");
sb.append(positions[i] - end);
sb.append(" ");
end = positions[i];
}
sb.append("A*");
return sb.toString();
}
String fmt = cut2fmt(new int[] {8, 14, 20, 26, 30});
boolean b, c;
boolean a = (b || c);
boolean x, y;
x = (x || y);
Object b, c;
Object a = (b != null) ? b : c;
Object x, y;
if (x == null)
x = y;
String foo = (bar != null) ? bar : "DEFAULT VALUE";
String foo = ((bar != null) && (bar.length != 0)) ? bar : "DEFAULT VALUE";
int index = 0;
String dir = ((args.length > index) && (args[index] != null)
&& (args[index].length != 0)) ? args[index] : "/tmp";
index++;
int index = 0;
String dir = ((args.length > index) && (args[index] != null)
&& (args[index].length != 0)) ? args[index++] : "/tmp";
String dir = ((args.length != 0)) ? args[0] : "/tmp";
String key = (shell != null) ? shell : "/bin/sh";
int value = 0;
if (count.containsKey(key))
value = ((Integer) count.get(key)).intValue();
count.put(key, new Integer(++value));
String user;
Properties environment = getEnvironment();
int UID = getUID();
if ((user = environment.getProperty("USER")) == null)
if ((user = environment.getProperty("LOGNAME")) == null)
if ((user = getLogin()) == null)
if ((user = getPWUID(UID)[0]) == null)
user = "Unknown uid number " + UID;
/**
* Returns current environment.
*/
Properties getEnvironment() {
Properties environment = new Properties();
try {
Process process = Runtime.getRuntime().exec("env");
BufferedReader input =
new BufferedReader(new InputStreamReader(process.getInputStream()));
String line;
while ((line = input.readLine()) != null) {
int limit = line.indexOf('=');
String key = line.substring(0, limit);
String value = line.substring(limit + 1);
environment.setProperty(key, value);
}
} catch (IOException ex) {
System.err.println("Error occured while processing environment");
}
return environment;
}
/**
* Returns current login.
*/
String getLogin() {
String login = "";
try {
Process process = Runtime.getRuntime().exec("cat /etc/utmp");
BufferedReader input =
new BufferedReader(new InputStreamReader(process.getInputStream()));
login = input.readLine();
} catch (IOException ex) {
System.err.println("Error occured while processing login");
}
return login;
}
/**
* Returns current UID.
*/
int getUID() {
int uid = 0;
try {
Process process = Runtime.getRuntime().exec("id -u");
BufferedReader input =
new BufferedReader(new InputStreamReader(process.getInputStream()));
uid = new Integer(input.readLine()).intValue();
} catch (IOException ex) {
System.err.println("Error occured while processing login");
}
return uid;
}
/**
* Returns /etc/passwd line relative to given UID.
*/
String[] getPWUID(int uid) {
ArrayList pwuid = new ArrayList();
try {
Process process =
Runtime.getRuntime().exec("grep ^[^:]*:[^:]*:" + uid + " /etc/passwd");
BufferedReader input =
new BufferedReader(new InputStreamReader(process.getInputStream()));
StringTokenizer tokenizer = new StringTokenizer(input.readLine(), ":");
while (tokenizer.hasMoreTokens())
pwuid.add(tokenizer.nextToken());
} catch (IOException ex) {
System.err.println("Error occured while processing login");
}
return (String[]) pwuid.toArray(new String[pwuid.size()]);
}
if ((startingPoint == null) || (startingPoint.length() == 0))
startingPoint = "Greenwich";
Object[] a, b, c;
if ((a == null) || (a.length == 0))
a = b;
a = ((b != null) && (b.length != 0)) ? b : c;
int num;
char ch;
num = ch; ch = (char) num; System.out.println("Number " + num + " is character " + (char) num);
System.out.println("Character " + ch + " is number " + (int) ch);
char[] ascii = string.toCharArray();
String string = new String(ascii);
int value = 'e'; char character = 101; System.out.println("Number " + 101 + " is character " + (char) 101);
char[] ascii = "sample".toCharArray();
String s1 = new String(ascii);
String s2 = new String(new char[] {115, 97, 109, 112, 108, 101});
String hal = "HAL";
char[] ascii = hal.toCharArray();
for (int i=0; i<ascii.length; i++)
ascii[i]++; String ibm = new String(ascii);
char[] charArray = string.toCharArray();
int size = string.length();
for (int i=0; i<size; i++) {
String s = string.substring(i, i + 1);
}
for (int i=0; i<size; i++) {
char ch = string.charAt(i);
}
String string = "an apple a day";
boolean[] seen = new boolean[256];
Arrays.fill(seen, false);
char[] array = string.toCharArray();
for (int i=0; i<array.length; i++)
seen[array[i]] = true;
System.out.print("unique chars are: ");
for (int i=0; i<seen.length; i++)
if (seen[i])
System.out.print((char) i);
System.out.println();
String string = "an apple a day";
TreeSet seen = new TreeSet();
int size = string.length();
for (int i=0; i<size; i++)
seen.add(string.substring(i, i + 1));
System.out.print("unique chars are: ");
Iterator it = seen.iterator();
while (it.hasNext())
System.out.print(it.next());
System.out.println();
int sum = 0;
char[] chars = string.toCharArray();
for (int i=0; i<chars.length; i++)
sum += chars[i];
System.out.println("sum is " + sum);
package pleac.ch01.part05;
import java.io.*;
/**
* Compute 16-bit checksum of input file
* Usage: java pleac.ch01.part05.Sum <file>
*/
public class Sum {
public static void main(String args[]) {
if (args.length < 1) {
System.err.println("Usage: java pleac.ch01.part05.Sum <file>");
System.exit(1);
}
int checksum = 0;
try {
Reader input = new FileReader(args[0]);
int ch;
while ((ch = input.read()) != -1)
checksum += ch;
} catch (IOException ex) {
System.err.println("Exception occured: " + ex);
}
checksum %= (java.lang.Math.pow(2, 16) - 1);
System.out.println(checksum);
}
}
package pleac.ch01.part05;
import java.io.*;
/**
* Emulate a s l o w line printer
* Usage: java pleac.ch01.part05.Slowcat <file>
*/
public class Slowcat {
public static void main(String args[]) {
if (args.length < 1) {
System.err.println("Usage: java pleac.ch01.part05.Slowcat <file> [delay]");
System.exit(1);
}
long delay = ((args.length > 1) && (args[1] != null)) ?
1000 * new Long(args[1]).longValue() : 1000;
try {
Reader input = new FileReader(args[0]);
int ch;
while ((ch = input.read()) != -1) {
System.out.print((char) ch);
try {
Thread.sleep(delay);
} catch (InterruptedException ex) {}
}
} catch (IOException ex) {
System.err.println("Exception occured: " + ex);
}
}
}
String reverseCharacters(String string) {
int length = string.length();
char[] chars = string.toCharArray();
char[] revChars = new char[length];
for (int i=0; i < length; i++)
revChars[i] = chars[length - i];
return new String(revChars);
}
String reverseWords(String string) {
StringTokenizer tokenizer = new StringTokenizer(string, " ");
StringBuffer buffer = new StringBuffer();
while (tokenizer.hasMoreTokens())
buffer.insert(0, tokenizer.nextToken() + " ");
return buffer.toString();
}
String string = 'Yoda said, "can you see this?"';
System.out.println(reverseWords(string));
this?" see you "can said, Yoda
StringTokenizer tokenizer = new StringTokenizer(string, " ");
StringBuffer buffer = new StringBuffer();
while (tokenizer.hasMoreTokens())
buffer.insert(0, tokenizer.nextToken() + " ");
StringTokenizer tokenizer = new StringTokenizer(string, " ", true);
StringBuffer buffer = new StringBuffer();
while (tokenizer.hasMoreTokens())
buffer.insert(0, tokenizer.nextToken());
String word = "reviver";
boolean isPalindrome = word.equals(reverseCharacters(word));
String file = "/usr/share/dict/words";
int length = 5;
try {
BufferedReader input = new BufferedReader(new FileReader(file));
String word;
while ((word = input.readLine()) != null)
if ((word.length() > length) && (word.equals(reverseCharacters(word))))
System.out.println(word);
} catch (IOException ex) {
System.err.println("Exception occured: " + ex);
}
String expand(String string, int tabStop) {
int index = string.indexOf('\t');
if (index == -1)
return string;
else {
StringBuffer buffer = new StringBuffer(string);
int count = tabStop - index % tabStop;
buffer.deleteCharAt(index);
for (int i=0; i<count; i++)
buffer.insert(index, " ");
return expand(buffer.toString(), tabStop);
}
}
String unexpand(String string, int tabStop) {
StringBuffer buffer = new StringBuffer(string);
int index = 0;
while (index + tabStop < buffer.length()) {
String piece = buffer.substring(index, index + tabStop);
int count = 0;
while ((count < tabStop)
&& (Character.isWhitespace(piece.charAt(tabStop - (count + 1)))))
count++;
if (count > 0) {
piece = piece.substring(0, tabStop - count) + '\t';
buffer.replace(index, index + tabStop, piece);
index = index + tabStop - (count - 1);
} else
index = index + tabStop;
}
return buffer.toString();
}
BufferedReader input;
String line;
while ((line = input.readLine()) != null)
System.out.println(expand(line), 4);
BufferedReader input;
String line;
while ((line = input.readLine()) != null)
System.out.println(unexpand(line), 8);
String big = little.toUpperCase(); String little = big.toLowerCase(); String big = little.substring(0, 1).toUpperCase() + little.substring(1);
String little = big.substring(0, 1).toLowerCase() + big.substring(1);
String beast = "dromedary";
String capit1 = beast.substring(0, 1).toUpperCase()
+ beast.substring(1).toLowerCase();
String capit2 = Character.toUpperCase(beast.charAt(0))
+ beast.substring(1).toLowerCase();
String capall = beast.toUpperCase();
String caprest1 = beast.substring(0, 1).toLowerCase()
+ beast.substring(1).toUpperCase();
String caprest2 = Character.toLowerCase(beast.charAt(0))
+ beast.substring(1).toUpperCase();
String text = "thIS is a loNG liNE";
StringTokenizer tokenizer = new StringTokenizer(text);
while (tokenizer.hasMoreTokens()) {
String word = tokenizer.nextToken();
System.out.print(word.substring(0, 1).toUpperCase()
+ word.substring(1).toLowerCase()
+ " ");
}
String a, b;
if (a.equalsIgnoreCase(b))
System.out.println("a and b are the same");
private Random rand;
char randomCase(char ch) {
return (rand.nextInt(100) < 20) ? Character.toLowerCase(ch): ch;
}
import java.text.BreakIterator();
BreakIterator boundary = BreakIterator.getLineInstance();
package pleac.ch01.part12;
import java.text.BreakIterator;
/**
* Show how BreakIterator works
*/
public class WrapDemo {
private static BreakIterator iterator = BreakIterator.getLineInstance();
public static void main(String[] args) {
String input = "Folding and splicing is the work of an editor,"
+ " not a mere collection of silicon"
+ " and"
+ " mobile electrons!";
int column = 20;
String leadTab = " ";
String nextTab = " ";
System.out.println("01234567890123456789");
input = wrapLine(leadTab + input, column);
while (input != null)
input = wrapLine(nextTab + input, column);
}
private static String wrapLine(String line, int width) {
iterator.setText(line);
int end = iterator.first();
while ((end != BreakIterator.DONE) && (end <= width))
end = iterator.next();
if (end == BreakIterator.DONE) {
System.out.println(line);
return null;
} else {
end = iterator.previous();
System.out.println(line.substring(0, end));
return line.substring(end);
}
}
}
Perl5Util util = new Perl5Util();
var = util.substitute("s/([CHARLIST])/\\\\$1/g", var);
var = util.substitute("s/([CHARLIST])/$1$1/g", var);
string = util.substitute("s/%/%%/g", string);
string = "Mom said, \"Don't do that.\"";
string = util.substitute("s/(['\"])/\\\\$1/g", string);
string = util.substitute("s/([^A-Z])/\\\\$1/g", string);
string = string.trim();
try {
BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
String line;
while((line = input.readLine()) != null) {
line = line.trim();
System.out.println(">" + line + "<");
}
} catch (IOException ex) {
System.err.println("Exception occured: " + ex);
}
static final String PATTERN =
"m/\"([^\\\"\\\\]*(?:\\\\.[^\\\"\\\\]*)*)\",? | ([^,]+),? | ,/x";
Perl5Util util = new Perl5Util();
Iterator parseCSV(String text) {
ArrayList list = new ArrayList();
while (util.match(PATTERN, text)) {
if (util.group(1) != null)
list.add(util.group(1));
else if (util.group(2) != null)
list.add(util.group(2));
else
list.add(null);
text = util.postMatch();
};
return list.iterator();
}
String line =
"XYZZY,\"\",\"O'Reilly, Inc\",\"Wall, Larry\",\"a \\\"glug\\\" bit,\",5,\"Error, Core Dumped\"";
Iterator it = parseCSV(line);
while (it.hasNext())
System.out.println(count++ + " : " + it.next());
package pleac.ch01.part17;
import java.io.*;
import java.util.*;
/**
* Switch first set of strings from to second set according to STYLE_FILE
* Usage: see derived classes
*/
public abstract class FixStyle {
private static final String STYLE_FILE = "pleac/ch01/part17/style";
private static final String BACKUP_SUFFIX = ".orig";
private static final String VERBOSE_FLAG = "-v";
Properties changes;
FixStyle() {
changes = new Properties();
try {
InputStream input =
getClass().getClassLoader().getResourceAsStream(STYLE_FILE);
changes.load(input);
} catch (IOException ex) {
System.err.println("Style loading failed: " + ex);
}
}
void fix(String[] args) {
if (args.length < 1) {
System.out.println("usage: java " + getClass().getName()
+ " <file> [-v]");
System.exit(1);
}
String file = args[0];
boolean verbose = ((args.length > 1) && (args[1] != null)
&& (args[1].equals(VERBOSE_FLAG))) ? true: false;
fixFile(file, verbose);
}
void fixFile(String file, boolean verbose) {
try {
BufferedReader input = new BufferedReader(new FileReader(file));
BufferedWriter output =
new BufferedWriter(new FileWriter(file + BACKUP_SUFFIX));
String line;
int count = 0;
while ((line = input.readLine()) != null) {
output.write(fixLine(line, ++count, verbose));
output.newLine();
}
output.close();
} catch (IOException ex) {
System.err.println("Exception occured: " + ex);
}
}
abstract String fixLine(String line, int count, boolean verbose);
}
package pleac.ch01.part17;
import java.util.*;
import org.apache.oro.text.perl.Perl5Util;
/**
* Fix style using substitutions
* Usage: java pleac.ch01.part17.FixStyle1 <file> [-v]
*/
public class FixStyle1 extends FixStyle {
private static Perl5Util util;
FixStyle1() {
util = new Perl5Util();
}
String fixLine(String line, int count, boolean verbose) {
Enumeration enum = changes.propertyNames();
while (enum.hasMoreElements()) {
String oldWord = (String) enum.nextElement();
if (util.match("/" + oldWord + "/", line)) {
String newWord = changes.getProperty(oldWord);
if (verbose)
System.out.println("substituting " + newWord + " to "
+ oldWord + " in line " + count);
line = util.substitute("s/" + oldWord + "/" + newWord + "/g", line);
}
}
return line;
}
public static void main(String[] args) {
FixStyle fixer = new FixStyle1();
fixer.fix(args);
}
}
package pleac.ch01.part17;
import java.util.*;
import java.text.BreakIterator;
/**
* Fix style using a break iterator
* Usage: java pleac.ch01.part17.FixStyle2 <file> [-v]
*/
public class FixStyle2 extends FixStyle {
private BreakIterator iterator;
FixStyle2() {
iterator = BreakIterator.getWordInstance();
}
String fixLine(String line, int count, boolean verbose) {
StringBuffer buffer = new StringBuffer(line);
iterator.setText(line);
int offset = 0;
int start = iterator.first();
int end = iterator.first();
while ((end = iterator.next()) != BreakIterator.DONE) {
String oldWord = line.substring(start, end);
if (changes.containsKey(oldWord)) {
String newWord = changes.getProperty(oldWord);
if (verbose)
System.out.println("substituting " + newWord + " to "
+ oldWord + " in line " + count);
buffer.replace(start + offset, end + offset, newWord);