Skip to content

Instantly share code, notes, and snippets.

@ronaldsmartin
Created September 5, 2014 09:15
Show Gist options
  • Save ronaldsmartin/0259125c5807307cf429 to your computer and use it in GitHub Desktop.
Save ronaldsmartin/0259125c5807307cf429 to your computer and use it in GitHub Desktop.
Comparing readability of naive implementations of char/string replacement in various languages
--| Replace all instances of oldChar in a string with newChar
replaceChar :: Char -> Char -> String -> String
replaceChar oldChar newChar = map replace
where replace oldChar = newChar
replace = id
-- | Replace all instances of oldString in s with newString.
-- Using length and take make this inefficient. Return to this later.
replaceAll :: String -> String -> String
replaceAll oldString newString s
| prefix == oldString = newString : replaceAll oldString newString s
| otherwise = prefix : replaceAll oldString newString s
where prefix = take (length oldString) s
/*
* The standard String class includes replacement methods by default.
* http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#replaceAll%28java.lang.String,%20java.lang.String%29
*/
static String replaceChar(char oldChar, char newChar, String s) {
StringBuilder sb = new StringBuilder(s.length());
for (int i = 0; i < s.length(); ++i)
if (s.charAt(i) == oldChar) sb.append(newChar);
else sb.append(s.charAt(i));
return sb.toString();
}
/**
* Recursive version of iterative algorithm above
*/
static String recursiveReplace(char oldChar, char newChar, String s) {
if (s.length() == 0) return "";
else if (s.charAt(0) == oldChar) return "" + newChar + recursiveReplace(oldChar, newChar, s.substring(1));
else return "" + s.charAt(0) + recursiveReplace(oldChar, newChar, s.substring(1));
}
/*
* Recursively replace all instances of oldString in s with newString
*/
static String replaceAllR(String oldString, String newString, String s) {
if (s.length() == 0) return "";
else if (s.startsWith(oldString))
return newString + replaceAllR(oldString, newString, s.substring(oldString.length()));
else return "" + s.charAt(0) + replaceAllR(oldString, newString, s.substring(1));
}
/*
* Iterative version of above algorithm.
*/
static String replaceAll(String oldString, String newString, String s) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < s.length(); i += 1) {
if (s.startsWith(oldString)) {
sb.append(newString);
i += oldString.length();
continue;
} else {
sb.append(s.charAt(i));
}
}
return sb.toString();
}
/*
* Included by default:
* - (NSString *)stringByReplacingOccurrencesOfString:(NSString *)target
withString:(NSString *)replacement
* According to Apple refs iterating over characters is not the way to go:
* https://developer.apple.com/library/mac/documentation/cocoa/conceptual/CocoaPerformance/Articles/StringDrawing.html#//apple_ref/doc/uid/TP40001445-112378
*/
+ (NSString *)stringByReplacingOldChar:(char)oldChar
withNewChar:(char)newChar
inString:(NSString *)s
{
NSMutableString *filtered = @"";
for (int i = 0; i < [s length]; ++i) {
if ([s characterAtIndex:i] == oldChar) {
...
}
...
}
}
func replaceChar(oldChar:Character, newChar:Character, s:String) -> String {
var filtered: String = ""
for c in s {
if c == oldChar {
filtered += newChar
} else {
filtered += c
}
}
return filtered
}
# Replace is included by default. https://docs.python.org/3.3/library/stdtypes.html#str.replace
def replaceChar(oldChar, newChar, s):
filtered = ""
for c in s:
if c == oldChar:
filtered += newChar
else:
filtered += c
return filtered
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment