From 9a397bf03fba2673930fbf7e2ebf908f5992edaf Mon Sep 17 00:00:00 2001 From: vijaykiran Date: Wed, 17 Sep 2008 23:17:56 +0200 Subject: [PATCH] Added method stringByReplacingOccurrencesOfStringWithString and testcase for the same. --- Foundation/CPString.j | 31 +++++++++++++++++++++++++++++++ Tests/Foundation/CPStringTest.j | 12 ++++++++++++ 2 files changed, 43 insertions(+) diff --git a/Foundation/CPString.j b/Foundation/CPString.j index 4809679b2..c5ade8ee7 100644 --- a/Foundation/CPString.j +++ b/Foundation/CPString.j @@ -232,6 +232,7 @@ var CPStringHashes = new objj_dictionary(); if (difference) string += substring.substr(difference + substring.length); } +//Dividing Strings /* Tokenizes the receiver string using the specified delimiter. For example, if the receiver is: @@ -351,6 +352,36 @@ var CPStringHashes = new objj_dictionary(); return CPMakeRange(location, location == CPNotFound ? 0 : aString.length); } +//Replacing Substrings + +/* + Returns a new string in which all occurrences of a target string in the reciever are replaced by + another given string. + @param target The string to replace. + @param replacement the string with which to replace the
target
+*/
+
+- (CPString)stringByReplacingOccurrencesOfString:(CPString)target withString:(CPString)replacement
+{
+    return self.replace(new RegExp(target, "g"), replacement);
+}
+/*
+- (CPString)stringByReplacingOccurrencesOfString:(CPString)target 
+                                      withString:(CPString)replacement 
+                                         options:(int)options 
+                                           range:(CPRange)searchRange
+{
+    //TODO :Vijay implement the method
+
+}
+
+- (CPString)stringByReplacingCharactersInRange:(CPRange)range withString:(CPString)replacement
+{
+    //TODO :Vijay implement the method
+}
+*/
+
+
 // Identifying and comparing strings
 /*
     Compares the receiver to the specified string.
diff --git a/Tests/Foundation/CPStringTest.j b/Tests/Foundation/CPStringTest.j
index d61a08f5a..289663cdc 100644
--- a/Tests/Foundation/CPStringTest.j
+++ b/Tests/Foundation/CPStringTest.j
@@ -2,6 +2,18 @@ import 
 
 @implementation CPStringTest : OJTestCase
 
+
+- (void)testStringByReplacingOccurrencesOfStringWithString
+{
+    var expectedString = @"hello world. A new world!";
+    var dummyString = @"hello woold. A new woold!";
+    var actualString = [dummyString stringByReplacingOccurrencesOfString:@"woold" withString:@"world"];
+    [self assertTrue:(expectedString === actualString) 
+             message:"stringByAppendingFormat: expected:" + expectedString + " actual:" + actualString];
+    
+    
+    
+}
 - (void)testStringByAppendingFormat
 {
     var format = @"%d X %d = %d";