View Source Dmp.StringUtils (diff_match_patch v0.3.0)
Java.String- and Javascript-compatible functions missing in Elixir's String module.
Summary
Functions
Returns the index within this string of the first occurrence of the specified substring, or -1 if there is no such occurence.
Returns the index within this string of the first occurrence of the specified substring, starting the search at the specified index, or -1 if there is no such occurence.
Returns the index within this string of the last occurrence of the specified substring, or -1 if there is no such occurence.
Returns the index within this string of the last occurrence of the specified substring, starting the search at the specified index, or -1 if there is no such occurence.
Returns a new string that is a substring of this string.
Returns a new string that is a substring of this string.
Unescape selected chars for compatability with JavaScript's encodeURI.
A URI encoding, but with spaces and asterisks left as is, for use with diffs.
Functions
Returns the index within this string of the first occurrence of the specified substring, or -1 if there is no such occurence.
Examples
ies> StringUtils.index_of("", "")
0
iex> StringUtils.index_of("abracadabra", "b")
1
iex> StringUtils.index_of("abracadabra", "f")
-1
@spec index_of(String.t(), String.t(), non_neg_integer()) :: integer()
Returns the index within this string of the first occurrence of the specified substring, starting the search at the specified index, or -1 if there is no such occurence.
Examples
iex> StringUtils.index_of("abracadabra", "", 2)
2
iex> StringUtils.index_of("abracadabra", "", 100)
11
iex> StringUtils.index_of("abracadabra", "b", 2)
8
iex> StringUtils.index_of("abracadabra", "f", 2)
-1
Returns the index within this string of the last occurrence of the specified substring, or -1 if there is no such occurence.
Examples
ies> StringUtils.last_index_of("", "")
0
ies> StringUtils.last_index_of("abracadabra", "")
11
iex> StringUtils.last_index_of("abracadabra", "b")
8
iex> StringUtils.last_index_of("abracadabra", "f")
-1
@spec last_index_of(String.t(), String.t(), non_neg_integer()) :: integer()
Returns the index within this string of the last occurrence of the specified substring, starting the search at the specified index, or -1 if there is no such occurence.
Examples
iex> StringUtils.index_of("abracadabra", "b", 5)
8
iex> StringUtils.index_of("abracadabra", "d", 9)
-1
iex> StringUtils.last_index_of("abcdefghijk", "fgh", 5)
5
ies> StringUtils.last_index_of("abcdefghijk", "", 5)
11
@spec substring(String.t(), non_neg_integer()) :: String.t()
Returns a new string that is a substring of this string.
The substring begins with the character at the specified index and extends to the end of this string.
Examples
iex> StringUtils.substring("abracadabra", 6)
"dabra"
iex> StringUtils.substring("abracadabra", 12)
""
@spec substring(String.t(), non_neg_integer(), non_neg_integer()) :: String.t()
Returns a new string that is a substring of this string.
The substring begins at the specified begin_index and extends to the
character at index end_index - 1. Thus the length of the substring
is end_index - begin_index.
Examples
iex> StringUtils.substring("abracadabra", 2, 6)
"raca"
iex> StringUtils.substring("abracadabra", 2, 12)
"racadabra"
Unescape selected chars for compatability with JavaScript's encodeURI.
In speed critical applications this could be dropped since the
receiving application will certainly decode these fine.
Note that this function is case-sensitive. Thus "%3f" would not be
unescaped. But this is ok because it is only called with the output of
StringUtils.uri_encode which returns uppercase hex.
Examples
iex> StringUtils.unescape_for_encode_uri_compatability("%3F")
"?"
iex> StringUtils.unescape_for_encode_uri_compatability("%3f")
"%3f"
iex> StringUtils.unescape_for_encode_uri_compatability("%24")
"$"
A URI encoding, but with spaces and asterisks left as is, for use with diffs.
Examples
iex> StringUtils.uri_encode("(")
"%28"
iex> StringUtils.uri_encode(" ")
" "
iex> StringUtils.uri_encode("*")
"*"
iex> StringUtils.uri_encode("[]")
"%5B%5D"