source: other-projects/trunk/gs3-release-maker/apache-ant-1.6.5/src/main/org/apache/tools/ant/util/StringUtils.java@ 14627

Last change on this file since 14627 was 14627, checked in by oranfry, 17 years ago

initial import of the gs3-release-maker

File size: 3.1 KB
Line 
1/*
2 * Copyright 2001-2002,2004 The Apache Software Foundation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *
16 */
17package org.apache.tools.ant.util;
18
19import java.io.PrintWriter;
20import java.io.StringWriter;
21import java.util.Vector;
22
23/**
24 * A set of helper methods related to string manipulation.
25 *
26 */
27public final class StringUtils {
28
29 /** the line separator for this OS */
30 public static final String LINE_SEP = System.getProperty("line.separator");
31
32 /**
33 * Splits up a string into a list of lines. It is equivalent
34 * to <tt>split(data, '\n')</tt>.
35 * @param data the string to split up into lines.
36 * @return the list of lines available in the string.
37 */
38 public static Vector lineSplit(String data) {
39 return split(data, '\n');
40 }
41
42 /**
43 * Splits up a string where elements are separated by a specific
44 * character and return all elements.
45 * @param data the string to split up.
46 * @param ch the separator character.
47 * @return the list of elements.
48 */
49 public static Vector split(String data, int ch) {
50 Vector elems = new Vector();
51 int pos = -1;
52 int i = 0;
53 while ((pos = data.indexOf(ch, i)) != -1) {
54 String elem = data.substring(i, pos);
55 elems.addElement(elem);
56 i = pos + 1;
57 }
58 elems.addElement(data.substring(i));
59 return elems;
60 }
61
62 /**
63 * Replace occurrences into a string.
64 * @param data the string to replace occurrences into
65 * @param from the occurrence to replace.
66 * @param to the occurrence to be used as a replacement.
67 * @return the new string with replaced occurrences.
68 */
69 public static String replace(String data, String from, String to) {
70 StringBuffer buf = new StringBuffer(data.length());
71 int pos = -1;
72 int i = 0;
73 while ((pos = data.indexOf(from, i)) != -1) {
74 buf.append(data.substring(i, pos)).append(to);
75 i = pos + from.length();
76 }
77 buf.append(data.substring(i));
78 return buf.toString();
79 }
80
81 /**
82 * Convenient method to retrieve the full stacktrace from a given exception.
83 * @param t the exception to get the stacktrace from.
84 * @return the stacktrace from the given exception.
85 */
86 public static String getStackTrace(Throwable t) {
87 StringWriter sw = new StringWriter();
88 PrintWriter pw = new PrintWriter(sw, true);
89 t.printStackTrace(pw);
90 pw.flush();
91 pw.close();
92 return sw.toString();
93 }
94
95}
Note: See TracBrowser for help on using the repository browser.