O'Reilly logo

Elixir Cookbook by Paulo A Pereira

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Replacing string codepoints with patterns

In this recipe, we will demonstrate how to replace codepoints in a string using a match pattern. We will use the String.replace/4 function to help with this task.

Note

A codepoint, in this context, is the binary representation of a UTF-8 encoded character.

Getting ready

This recipe will be performed inside an IEx session. Start it by executing the iex command in your command line.

How to do it…

To find a pattern in a string and replace it, follow these steps:

  1. Define a string:
    iex(1)> my_string = "user1@server.domain user2@server.domain"
    "user1@server.domain user2@server.domain"
    
  2. Define a string pattern to use with String.replace:
    iex(2)> my_pattern = "@"
    "@"
    
  3. Perform the replacement using the default options:

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required