1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78 | package Torello.Java;
import Torello.Java.ParallelArrayException;
class CharArrToCharArr
{
static String replace(
final boolean ignoreCase,
final String s,
final char[] matchCharsInput,
final char[] replaceChars
)
{
// Make sure these arrays are of equal length
//
// NOTE: I'm telling the exception-check that "matchCharsInput" is actually named
// "matchChars" - this is because the user sees that input-parameter name inside of
// the method he/she is calling. Don't worry about it.
ParallelArrayException.check
(matchCharsInput, "matchChars", replaceChars, "replaceChars");
// The methods in this class all perform the replacements by first creating an
// appropriately-sized output char[] array. The last step of each of the methods is to
// invoke the String constructor: new String(char[]) where a character array is converted
// into a String.
final char[] cArr = s.toCharArray();
final char[] matchChars;
if (ignoreCase)
{
matchChars = new char[matchCharsInput.length];
for (int i=0; i < matchChars.length; i++)
matchChars[i] = Character.toLowerCase(matchCharsInput[i]);
}
else matchChars = matchCharsInput;
if (ignoreCase)
TOP1:
for (int i=0; i < cArr.length; i++)
{
char c = Character.toLowerCase(cArr[i]);
for (int j=0; j < matchChars.length; j++)
if (c == matchChars[j])
{
cArr[i] = replaceChars[j]; // If a match was found, just replace it
continue TOP1; // This method, really is THAT EASY.
}
}
else
TOP2:
for (int i=0; i < cArr.length; i++)
{
char c = cArr[i];
for (int j=0; j < matchChars.length; j++)
if (c == matchChars[j])
{
cArr[i] = replaceChars[j]; // If a match was found, just replace it
continue TOP2; // This method, really is THAT EASY.
}
}
// Convert the character array into a String
return new String(cArr);
}
}
|