|
Strings |
|
1 /* 2 * Strings.java 3 * 4 * Copyright (c) 1998-2001, The University of Sheffield. 5 * 6 * This file is part of GATE (see http://gate.ac.uk/), and is free 7 * software, licenced under the GNU Library General Public License, 8 * Version 2, June 1991 (in the distribution as file licence.html, 9 * and also available at http://gate.ac.uk/gate/licence.html). 10 * 11 * Hamish Cunningham, 22/02/2000 12 * 13 * $Id: Strings.java,v 1.6 2001/11/16 13:03:35 hamish Exp $ 14 */ 15 16 package gate.util; 17 18 import java.io.*; 19 20 /** Some utilities for use with Strings. */ 21 public class Strings { 22 23 /** Debug flag */ 24 private static final boolean DEBUG = false; 25 26 /** What character to pad with. */ 27 private static char padChar = ' '; 28 29 /** Local fashion for newlines this year. */ 30 private static String newline = System.getProperty("line.separator"); 31 32 /** Get local fashion for newlines. */ 33 public static String getNl() { return newline; } 34 35 /** Local fashion for path separators. */ 36 private static String pathSep = System.getProperty("path.separator"); 37 38 /** Get local fashion for path separators (e.g. ":"). */ 39 public static String getPathSep() { return pathSep; } 40 41 /** Local fashion for file separators. */ 42 private static String fileSep = System.getProperty("file.separator"); 43 44 /** Get local fashion for file separators (e.g. "/"). */ 45 public static String getFileSep() { return fileSep; } 46 47 /** Add n pad characters to pad. */ 48 public static String addPadding(String pad, int n) { 49 StringBuffer s = new StringBuffer(pad); 50 for(int i = 0; i < n; i++) 51 s.append(padChar); 52 53 return s.toString(); 54 } // padding 55 56 /** Helper method to add line numbers to a string */ 57 public static String addLineNumbers(String text) { 58 // construct a line reader for the text 59 BufferedReader reader = new BufferedReader(new StringReader(text)); 60 String line = null; 61 StringBuffer result = new StringBuffer(); 62 63 try { 64 for(int lineNum = 1; ( line = reader.readLine() ) != null; lineNum++) { 65 String pad; 66 if(lineNum < 10) pad = " "; 67 else pad = ""; 68 result.append(pad + lineNum + " " + line + Strings.getNl()); 69 } 70 } catch(IOException ie) { } 71 72 return result.toString(); 73 } // addLineNumbers 74 75 } // class Strings 76
|
Strings |
|