You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

StringUtils.java 3.2 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. */
  17. package org.apache.tools.ant.util;
  18. import java.io.PrintWriter;
  19. import java.io.StringWriter;
  20. import java.util.Vector;
  21. /**
  22. * A set of helper methods related to string manipulation.
  23. *
  24. */
  25. public final class StringUtils {
  26. /**
  27. * constructor to stop anyone instantiating the class
  28. */
  29. private StringUtils() {
  30. }
  31. /** the line separator for this OS */
  32. public static final String LINE_SEP = System.getProperty("line.separator");
  33. /**
  34. * Splits up a string into a list of lines. It is equivalent
  35. * to <tt>split(data, '\n')</tt>.
  36. * @param data the string to split up into lines.
  37. * @return the list of lines available in the string.
  38. */
  39. public static Vector lineSplit(String data) {
  40. return split(data, '\n');
  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. * Replace occurrences into a string.
  63. * @param data the string to replace occurrences into
  64. * @param from the occurrence to replace.
  65. * @param to the occurrence to be used as a replacement.
  66. * @return the new string with replaced occurrences.
  67. */
  68. public static String replace(String data, String from, String to) {
  69. StringBuffer buf = new StringBuffer(data.length());
  70. int pos = -1;
  71. int i = 0;
  72. while ((pos = data.indexOf(from, i)) != -1) {
  73. buf.append(data.substring(i, pos)).append(to);
  74. i = pos + from.length();
  75. }
  76. buf.append(data.substring(i));
  77. return buf.toString();
  78. }
  79. /**
  80. * Convenient method to retrieve the full stacktrace from a given exception.
  81. * @param t the exception to get the stacktrace from.
  82. * @return the stacktrace from the given exception.
  83. */
  84. public static String getStackTrace(Throwable t) {
  85. StringWriter sw = new StringWriter();
  86. PrintWriter pw = new PrintWriter(sw, true);
  87. t.printStackTrace(pw);
  88. pw.flush();
  89. pw.close();
  90. return sw.toString();
  91. }
  92. }