1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 13:57:35 +00:00

AK: Add String::replace() functionality

This adds a replace functionality that replaces a string that contains
occurences of a "needle" by a "replacement" value. With "all_occurences"
enabled, all occurences are being replaced, otherwise only the first
occurence is being replaced.
This commit is contained in:
Emanuel Sprung 2020-04-01 21:27:39 +02:00 committed by Andreas Kling
parent 9d5d0261e1
commit 2577712a1c
3 changed files with 62 additions and 0 deletions

View file

@ -159,4 +159,29 @@ TEST_CASE(flystring)
}
}
TEST_CASE(replace)
{
String test_string = "Well, hello Friends!";
u32 replacements = test_string.replace("Friends", "Testers");
EXPECT(replacements == 1);
EXPECT(test_string == "Well, hello Testers!");
replacements = test_string.replace("ell", "e're", true);
EXPECT(replacements == 2);
EXPECT(test_string == "We're, he'reo Testers!");
replacements = test_string.replace("!", " :^)");
EXPECT(replacements == 1);
EXPECT(test_string == "We're, he'reo Testers :^)");
test_string = String("111._.111._.111");
replacements = test_string.replace("111", "|||", true);
EXPECT(replacements == 3);
EXPECT(test_string == "|||._.|||._.|||");
replacements = test_string.replace("|||", "111");
EXPECT(replacements == 1);
EXPECT(test_string == "111._.|||._.|||");
}
TEST_MAIN(String)