Skip to content

Commit

Permalink
Fix #85: add support for a replaceAllEmojis function
Browse files Browse the repository at this point in the history
Adds a function that replaces all the emojis in a string by another string.
  • Loading branch information
cbedoy authored and vdurmont committed Nov 3, 2017
1 parent 75811ba commit 3664230
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/main/java/com/vdurmont/emoji/EmojiParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,23 @@ public String transform(UnicodeCandidate unicodeCandidate) {
return parseFromUnicode(input, emojiTransformer);
}

/**
* Replace all emojis with character
*
* @param str the string to process
* @param replacementString replacement the string that will replace all the emojis
* @return the string with replaced character
*/
public static String replaceAllEmojis(String str, final String replacementString) {
EmojiParser.EmojiTransformer emojiTransformer = new EmojiParser.EmojiTransformer() {
public String transform(EmojiParser.UnicodeCandidate unicodeCandidate) {
return replacementString;
}
};

return parseFromUnicode(str, emojiTransformer);
}


/**
* Replaces the emoji's aliases (between 2 ':') occurrences and the html
Expand Down
16 changes: 16 additions & 0 deletions src/test/java/com/vdurmont/emoji/EmojiParserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,22 @@ public void parseToAliases_replaces_the_emojis_by_one_of_their_aliases() {
);
}

@Test
public void replaceAllEmojis_replace_the_emojis_by_string() throws Exception {
// GIVEN
String str = "An 😀awesome 😃string with a few 😉emojis!";

// WHEN
String result = EmojiParser.replaceAllEmojis(str, ":)");

// THEN
assertEquals(
"An :)awesome :)string with a few :)emojis!",
result
);
}


@Test
public void parseToAliases_REPLACE_with_a_fitzpatrick_modifier() {
// GIVEN
Expand Down

0 comments on commit 3664230

Please sign in to comment.