My question is thus:
Suppose you have String myString = "SOME_CHARACTERS_THAT_NEED_MODIFICATION";
that you would want to have look like String modifiedString = "Some Characters That Need Modification"
. The "pure String" way to do it (and the case-independent way) would (optimize this as necessary):
//obtaining the locations of all the occurrences of '_'
int activeIndex = 0;
ArrayList <Integer> indexList = new ArrayList<Integer>();
while (activeIndex != -1)
{
activeIndex = myString.indexOf('_', activeIndex + 1);
indexList.add(new Integer(activeIndex));
}
//replacing all '_' with ' '
String tempString = myString.replace('_', ' ');
//declaring empty modifiedString
String modifiedString;
//lowercasing all characters that are not first characters of a word (here, a word is defined as being terminated by '_' or newline
for (int x = 0; x < indexList.size(); x++)
{
modifiedString += tempString.substring(indexList.get(x), indexList.get(x)+1);
if (x != indexList.size() - 1)
//appending first uppercase character of word plus lowercased characters of the rest of the word
modifiedString += tempString.subString(indexList.get(x)+1,indexList.get(x+1)).toLowerCase();
else
//we are near the end of the String (as far as ' ' is concerned)
modifiedString += tempString.substring(index.get(x), tempString.length().toLowerCase());
}
//moving this modified String to modifiedString
modifiedString = tempString;
The other way I was proposing to do this would have been to dump myString into an array of characters, and then do array-based manipulation of all the characters. This would be easy in C++; a String is both an array of characters and an Object there! My question is, however, would both algorithms have the same complexity? //As a character array, I could probably do some arithmetic, assuming that the alphanumeric characters are numerically in the ASCII range (0 through 127). In fact, (int)uppercaseChar == (int)lowercaseChar - 32;
for any of the uppercaseChar ranging from A-Z and any corresponding lowercaseChar ranging from a-z.
The char[] way to do would probably be something like (may need optimization)
//declaring necessary variables and containers
int activeIndex = 0;
ArrayList<Integer> indexList = new ArrayList<Integer>();
while (activeIndex != -1)
{
//finding all '_'
activeIndex = myString.indexOf('_', activeIndex + 1);
//pushing it to indexArray
indexArray.add(new Integer(activeIndex));
}
//dumping String to char[]
char[] charArray = myString.toCharArray();
for (int x = 0; x < indexArray.size(); x++)
{
//making every '_' a ' '
charArray[indexArray.get(x)] = ' ';
//lowercasing every capitalized character that isn't a first character in a word
}