ReplaceStr

ReplaceStr( value, find, replace )
ReplaceStr( value, find, replace )
ReplaceStr( value, find, replace )
ReplaceStr( value, find, replace )
replace_str( value, find, replace )
ReplaceStr( value, find, replace )

Description

The ReplaceStr function returns a string which has all occurrences of the find sub-string replaced with the replace string. Punctuation in the find string is interpreted literally and not as a pattern.

Parameters

Parameter Type Definition and Requirements

value

The source String to search through.

find

The sub-string to be found in the value string.

replace

The new string that should replace the find sub-string.

Returns

A new String which has all of the find sub-strings replaced with the replace string.

Examples

The following query executes an array of independent ReplaceStr operations and returns the results in an array. The result array position matches the execution array position. The individual operations are:

  1. The string "One Fish Two Fish" is processed, replacing all instances of "Two" with "Blue". The result string "One Fish Blue Fish" is placed in the top position of the result array.

  2. The string "One Fish Two Fish" is processed, replacing all instances of "Fish" with "CAT". The resultant string "One CAT Two CAT" is placed in the second position of the result array.

  3. The string "One Fis? Two Fish" is processed, replacing all instances of the "Fis?" with "Fish". The resultant String "One Fish Two Fish" is placed in the third position of the result array.

try
{
    Value result = await client.Query(
        Arr(
            ReplaceStr("One Fish Two Fish", "Two", "Blue"),
            ReplaceStr("One Fish Two Fish", "Fish", "CAT"),
            ReplaceStr("One Fis? Two Fish", "Fis?", "Fish")
        )
    );

    Console.WriteLine(result);
}
catch (Exception e)
{
    Console.WriteLine($"ERROR: {e.Message}");
}
Arr(StringV(One Fish Blue Fish), StringV(One CAT Two CAT), StringV(One Fish Two Fish))
result, err := client.Query(
	f.Arr{
		f.ReplaceStr("One Fish Two Fish", "Two", "Blue"),
		f.ReplaceStr("One Fish Two Fish", "Fish", "CAT"),
		f.ReplaceStr("One Fis? Two Fish", "Fis?", "Fish"),
	})

if err != nil {
	fmt.Fprintln(os.Stderr, err)
} else {
	fmt.Println(result)
}
[One Fish Blue Fish One CAT Two CAT One Fish Two Fish]
System.out.println(
    client.query(
        Arr(
            ReplaceStr("One Fish Two Fish", "Two", "Blue"),
            ReplaceStr("One Fish Two Fish", "Fish", "CAT"),
            ReplaceStr("One Fis? Two Fish", "Fis?", "Fish")
        )
    ).get());
["One Fish Blue Fish", "One CAT Two CAT", "One Fish Two Fish"]
client.query(
  [
    q.ReplaceStr('One Fish Two Fish', 'Two', 'Blue'),
    q.ReplaceStr('One Fish Two Fish', 'Fish', 'CAT'),
    q.ReplaceStr('One Fis? Two Fish', 'Fis?', 'Fish'),
  ]
)
.then((ret) => console.log(ret))
.catch((err) => console.error(
  'Error: [%s] %s: %s',
  err.name,
  err.message,
  err.errors()[0].description,
))
[ 'One Fish Blue Fish', 'One CAT Two CAT', 'One Fish Two Fish' ]
result = client.query(
  [
    q.replace_str("One Fish Two Fish", "Two", "Blue"),
    q.replace_str("One Fish Two Fish", "Fish", "CAT"),
    q.replace_str("One Fis? Two Fish", "Fis?", "Fish"),
  ]
)
print(result)
['One Fish Blue Fish', 'One CAT Two CAT', 'One Fish Two Fish']
[
  ReplaceStr('One Fish Two Fish', 'Two', 'Blue'),
  ReplaceStr('One Fish Two Fish', 'Fish', 'CAT'),
  ReplaceStr('One Fis? Two Fish', 'Fis?', 'Fish'),
]
[ 'One Fish Blue Fish', 'One CAT Two CAT', 'One Fish Two Fish' ]
Query metrics:
  •    bytesIn: 197

  •   bytesOut:  73

  • computeOps:   1

  •    readOps:   0

  •   writeOps:   0

  •  readBytes:   0

  • writeBytes:   0

  •  queryTime: 2ms

  •    retries:   0

Is this article helpful? 

Tell Fauna how the article can be improved:
Visit Fauna's forums or email docs@fauna.com

Thank you for your feedback!