|
|||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
ReplaceUtility.java | 50% | 93,8% | 66,7% | 80% |
|
1 | /* $Id: ReplaceUtility.java,v 1.7 2007/02/25 20:05:38 m31 Exp $ | |
2 | * | |
3 | * This file is part of the project "Hilbert II" - http://www.qedeq.org | |
4 | * | |
5 | * Copyright 2000-2007, Michael Meyling <mime@qedeq.org>. | |
6 | * | |
7 | * "Hilbert II" is free software; you can redistribute | |
8 | * it and/or modify it under the terms of the GNU General Public | |
9 | * License as published by the Free Software Foundation; either | |
10 | * version 2 of the License, or (at your option) any later version. | |
11 | * | |
12 | * This program is distributed in the hope that it will be useful, | |
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | * GNU General Public License for more details. | |
16 | */ | |
17 | ||
18 | package org.qedeq.kernel.utility; | |
19 | ||
20 | ||
21 | /** | |
22 | * A collection of useful static methods for string replacement. | |
23 | * | |
24 | * @version $Revision: 1.7 $ | |
25 | * @author Michael Meyling | |
26 | */ | |
27 | public final class ReplaceUtility { | |
28 | ||
29 | /** | |
30 | * Constructor, should never be called. | |
31 | */ | |
32 | 0 | private ReplaceUtility() { |
33 | // don't call me | |
34 | } | |
35 | ||
36 | /** | |
37 | * Replaces all occurrences of <code>search</code> in <code>text</code> | |
38 | * by <code>replace</code> and returns the result. | |
39 | * | |
40 | * @param text text to work on | |
41 | * @param search replace this text by <code>replace</code> | |
42 | * @param replace replacement for <code>search</code> | |
43 | * @return resulting string | |
44 | */ | |
45 | 10469 | public static String replace(final String text, |
46 | final String search, final String replace) { | |
47 | ||
48 | 10469 | final int len = search.length(); |
49 | 10469 | if (len == 0) { |
50 | 0 | return text; |
51 | } | |
52 | 10469 | final StringBuffer result = new StringBuffer(); |
53 | 10469 | int pos1 = 0; |
54 | 10469 | int pos2; |
55 | ? | while (0 <= (pos2 = text.indexOf(search, pos1))) { |
56 | 5451 | result.append(text.substring(pos1, pos2)); |
57 | 5451 | result.append(replace); |
58 | 5451 | pos1 = pos2 + len; |
59 | } | |
60 | 10469 | if (pos1 < text.length()) { |
61 | 9700 | result.append(text.substring(pos1)); |
62 | } | |
63 | 10469 | return result.toString(); |
64 | } | |
65 | ||
66 | ||
67 | /** | |
68 | * Replaces all occurrences of <code>search</code> in <code>text</code> | |
69 | * by <code>replace</code> and returns the result. | |
70 | * | |
71 | * @param text text to work on | |
72 | * @param search replace this text by <code>replace</code> | |
73 | * @param replace replacement for <code>search</code> | |
74 | */ | |
75 | 10465 | public static void replace(final StringBuffer text, |
76 | final String search, final String replace) { | |
77 | ||
78 | 10465 | final String result = replace(text.toString(), search, replace); |
79 | 10465 | text.setLength(0); |
80 | 10465 | text.append(result); |
81 | // TODO mime 20050205: check if the above could be replaced with: | |
82 | /* | |
83 | final StringBuffer result = new StringBuffer(); | |
84 | int pos1 = 0; | |
85 | int pos2; | |
86 | final int len = search.length(); | |
87 | while (0 <= (pos2 = text.indexOf(search, pos1))) { | |
88 | result.append(text.substring(pos1, pos2)); | |
89 | result.append(replace); | |
90 | pos1 = pos2 + len; | |
91 | } | |
92 | if (pos1 < text.length()) { | |
93 | result.append(text.substring(pos1)); | |
94 | } | |
95 | text.setLength(0); | |
96 | text.append(result); | |
97 | */ | |
98 | } | |
99 | ||
100 | ||
101 | } |
|