1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package com.cosylab.util;
21
22
23
24
25
26
27
28 public class StringBufferUtilities {
29
30 private StringBufferUtilities() {
31 super();
32
33 }
34
35 public static void replace(StringBuffer buf, String replaceWhat, String replaceTo) {
36 int idxFind=buf.indexOf(replaceWhat,0);
37 int findLen=replaceWhat.length();
38 int repLen=replaceTo.length();
39 while (idxFind>=0) {
40 buf.replace(idxFind,idxFind+findLen,replaceTo);
41 idxFind=idxFind+repLen;
42 idxFind=buf.indexOf(replaceWhat, idxFind);
43 }
44 }
45
46 public static void prepend(StringBuffer buf, String beforeWhat, String toPrepend) {
47 prepend(buf,beforeWhat,toPrepend,beforeWhat.length());
48 }
49
50 public static void prepend(StringBuffer buf, String beforeWhat, String toPrepend, int offset) {
51 int idxFind=buf.indexOf(beforeWhat,0);
52 int prepLen=toPrepend.length();
53 while (idxFind>=0) {
54 buf.insert(idxFind,toPrepend);
55 idxFind=idxFind+prepLen+offset;
56 idxFind=buf.indexOf(beforeWhat, idxFind);
57 }
58 }
59
60 public static void replace(StringBuffer buf, char replaceWhat, char replaceTo) {
61 for (int i = buf.length()-1; i >= 0 ; i--) {
62 if (buf.charAt(i)==replaceWhat) {
63 buf.setCharAt(i,replaceTo);
64 }
65 }
66 }
67
68 public static void prepend(StringBuffer buf, char beforeWhat, char toPrepend) {
69 for (int i = buf.length()-1; i >= 0 ; i--) {
70 if (buf.charAt(i)==beforeWhat) {
71 buf.insert(i,toPrepend);
72 }
73 }
74 }
75
76 public static void replace(StringBuffer buf, char replaceWhat, String replaceTo) {
77 for (int i = buf.length()-1; i >= 0 ; i--) {
78 if (buf.charAt(i)==replaceWhat) {
79 buf.replace(i,i+1,replaceTo);
80 }
81 }
82 }
83
84 public static void prepend(StringBuffer buf, char beforeWhat, String toPrepend) {
85 for (int i = buf.length()-1; i >= 0 ; i--) {
86 if (buf.charAt(i)==beforeWhat) {
87 buf.insert(i,toPrepend);
88 }
89 }
90 }
91
92 public static void replace(StringBuffer buf, String replaceWhat, char replaceTo) {
93 int idxFind=buf.indexOf(replaceWhat,0);
94 int findLen=replaceWhat.length();
95 while (idxFind>=0) {
96 buf.delete(idxFind+1,idxFind+findLen);
97 buf.setCharAt(idxFind,replaceTo);
98 idxFind=idxFind+1;
99 idxFind=buf.indexOf(replaceWhat, idxFind);
100 }
101 }
102
103 public static void prepend(StringBuffer buf, String beforeWhat, char toPrepend) {
104 prepend(buf,beforeWhat,toPrepend,beforeWhat.length());
105 }
106
107 public static void prepend(StringBuffer buf, String beforeWhat, char toPrepend, int offset) {
108 int idxFind=buf.indexOf(beforeWhat,0);
109 while (idxFind>=0) {
110 buf.insert(idxFind,toPrepend);
111 idxFind=idxFind+1+offset;
112 idxFind=buf.indexOf(beforeWhat, idxFind);
113 }
114 }
115
116 public static int indexOf(StringBuffer buf, char toFind) {
117 return indexOf(buf,toFind,0);
118 }
119
120
121
122
123
124
125
126 public static int indexOf(StringBuffer buf, char toFind, int startIndex) {
127 int len=buf.length();
128 for (int i = startIndex; i < len; i++) {
129 if (buf.charAt(i)==toFind) {
130 return i;
131 }
132 }
133 return -1;
134 }
135
136 public static void deleteAll(StringBuffer buf, char toDelete) {
137 for (int i = buf.length()-1; i >= 0 ; i--) {
138 if (buf.charAt(i)==toDelete) {
139 buf.deleteCharAt(i);
140 }
141 }
142 }
143
144 public static void deleteAll(StringBuffer buf, String toDelete) {
145 int idxFind=buf.indexOf(toDelete,0);
146 int findLen=toDelete.length();
147 while (idxFind>=0) {
148 buf.delete(idxFind,idxFind+findLen);
149 idxFind=buf.indexOf(toDelete, idxFind);
150 }
151 }
152
153 public static void main(String[] args) {
154 StringBuffer buf=new StringBuffer("mihafoobar-krakra");
155 System.out.print(buf);
156 System.out.println(" "+indexOf(buf,'a'));
157 replace(buf,'a','y');
158 System.out.println(buf);
159 replace(buf,'y',"_A_");
160 System.out.println(buf);
161 replace(buf,"_A_","[[_B_]]");
162 System.out.println(buf);
163 replace(buf,"B_",'a');
164 System.out.println(buf);
165 deleteAll(buf,'_');
166 System.out.println(buf);
167 deleteAll(buf,"[[");
168 System.out.println(buf);
169 prepend(buf,"a]]",'p');
170 System.out.println(buf);
171 deleteAll(buf,"]]");
172 System.out.println(buf+" "+indexOf(buf,'a',4));
173 }
174
175 }