Krug.StringUtil (Krug v1.1.40) View Source

Utilitary secure module to provide helpful methods to string manipulation, for general use.

Link to this section Summary

Functions

Convert a value to string with all words capitalized.

Convert a received value to a string. If this string is not empty return these value. Otherwise return the value_if_empty_or_nil parameter value.

Merge 2 strings, A and B using a join_string as a join connector. If A is nil a receive a empty string value, making the same process to B and to join_string.

Convert a value to string, and verify if this value contains one value present on received array of values. Each value on array of values is converted to string before realize the comparison.

Decodes a URI replacing the "+" codes to right " " chars, preserving non URI "+" chars.

Convert a value to string, returning "" (empty string) if value is nil.

Extract a parameter value of an parameter values array.

Searches the position of first occurency of a substring on a string. Returns -1 for no result found, or if one of parameters is null value.

Receives a value, force to string and completes the value with left spaces until the value size == size parameter value received.

Receives a value, force to string and completes the value with left zeros until the value size == size parameter value received.

Convert a string value in raw binary format <<xxx,xx,xx>> to string

Replace searched string value by replace_to string value, into target string. Replaces recursively all occurences if is not present the recursion throwble. Otherwise replace one single time all occurencies without recursive calls when recursion throwble is detected.

Replaces all occurrences of each one element in searched_array into target, by replace_to string value.

Receives a value, force to string and completes the value with right spaces until the value size == size parameter value received.

Receives a value, force to string and completes the value with right zeros until the value size == size parameter value received.

Obtain a substring of a string begining the cut at start_position position and finishing cut at end_position. Same expected parameters and results as String.slice/3 function, however more performatic than.

Receive a string target and split it to an array of strings.

Convert a numeric value char_code_string received to the correspondent character alfanumeric of any alphabet of any language.

Convert a string character value alfanumeric, of any alphabet of any language, contained in array received to the correspondent char code.

Convert a value to string, returning the value without left and right spaces.

Link to this section Functions

Convert a value to string with all words capitalized.

Examples

iex > Krug.StringUtil.capitalize(nil)
""
iex > Krug.StringUtil.capitalize("")
""
iex > Krug.StringUtil.capitalize(" ")
" "
iex > Krug.StringUtil.capitalize(" a e i ou ")
" A E I Ou "
iex > Krug.StringUtil.capitalize(" this is a method that capitalize ")
" This Is A Method That Capitalize "
Link to this function

coalesce(value, value_if_empty_or_nil)

View Source

Convert a received value to a string. If this string is not empty return these value. Otherwise return the value_if_empty_or_nil parameter value.

If value_if_empty_or_nil is nil return the received value or a empty string case the received value is nil.

Useful to forces a default value, to validations for example.

The parameter unsafe can be set to true when you have total sure that all parameters are in binary format and not null/empty. This will improve some performance.

Examples

iex > Krug.StringUtil.coalesce(nil,nil)
""
iex > Krug.StringUtil.coalesce(nil,"")
""
iex > Krug.StringUtil.coalesce("",nil)
""
iex > Krug.StringUtil.coalesce(" ",nil)
" "
iex > Krug.StringUtil.coalesce("A",nil)
"A"
iex > Krug.StringUtil.coalesce(nil,"A")
"A"
iex > Krug.StringUtil.coalesce("","A")
"A"
iex > Krug.StringUtil.coalesce(" ","A")
"A"
Link to this function

concat(string_a, string_b, join_string)

View Source

Merge 2 strings, A and B using a join_string as a join connector. If A is nil a receive a empty string value, making the same process to B and to join_string.

If A and B are empty then return empty string.

Examples

iex > Krug.StringUtil.concat(nil,nil,nil)
""
iex > Krug.StringUtil.concat(nil,nil,"-")
""
iex > Krug.StringUtil.concat("A",nil,"-")
"A"
iex > Krug.StringUtil.concat(nil,"B","-")
"B"
iex > Krug.StringUtil.concat("A","B","-")
"A-B"
iex > Krug.StringUtil.concat(" ","B","-")
" -B"
Link to this function

contains_one_element_of_array(target, array, unsafe \\ false)

View Source

Convert a value to string, and verify if this value contains one value present on received array of values. Each value on array of values is converted to string before realize the comparison.

If array is nil/empty return false.

The parameter unsafe can be set to true when you have total sure that all parameters are in binary format and not null/empty. This will improve some performance.

Examples

iex > Krug.StringUtil.contains_one_element_of_array(nil,nil)
false
iex > Krug.StringUtil.contains_one_element_of_array("",nil)
false
iex > Krug.StringUtil.contains_one_element_of_array(" ",nil)
false
iex > Krug.StringUtil.contains_one_element_of_array("abcdef[]",[0,1,2,[]])
false
iex > Krug.StringUtil.contains_one_element_of_array("abcdef5",[0,1,2,[5,7]])
false
iex > Krug.StringUtil.contains_one_element_of_array("abcdef5,7",[0,1,2,[5,7]])
false
iex > Krug.StringUtil.contains_one_element_of_array("abcdef[5,7]",[0,1,2,[5,7]])
false
iex > Krug.StringUtil.contains_one_element_of_array("abcdef[]",[0,1,2,[],"]"])
true
iex > Krug.StringUtil.contains_one_element_of_array("abcdef[]",[0,1,2,[],"bc"])
true
iex > Krug.StringUtil.contains_one_element_of_array("abcdef[]",[0,1,2,[],"def["])
true
iex > Krug.StringUtil.contains_one_element_of_array("abcdef8[]",[0,1,2,[],8])
true
Link to this function

contains_one_element_of_array2(target, array, unsafe)

View Source
Link to this function

contains_one_element_of_array3(target, array, unsafe)

View Source

Decodes a URI replacing the "+" codes to right " " chars, preserving non URI "+" chars.

Example

iex > Krug.StringUtil.decode_uri("these ++ is ++ a ++ http://example.com/short+uri+example ++++")
"these ++ is ++ a ++ http://example.com/short uri example +   "

Convert a value to string, returning "" (empty string) if value is nil.

Examples

iex > Krug.StringUtil.empty_if_nil(nil)
""
iex > Krug.StringUtil.empty_if_nil("")
""
iex > Krug.StringUtil.empty_if_nil(" ")
" "
iex > Krug.StringUtil.empty_if_nil("A")
"A"
iex > Krug.StringUtil.empty_if_nil(10)
"10"
iex > Krug.StringUtil.empty_if_nil(10.05)
"10.05"
iex > Krug.StringUtil.empty_if_nil(-10.05)
"-10.05"
Link to this function

get_decoded_value_param(array_params, param, separator)

View Source

Extract a parameter value of an parameter values array.

Useful in some situations, to handle parameters received from a api call for example.

Examples

iex > array_params = ["name=Johann Backend","age=54","address=404 street"]
iex > Krug.StringUtil.get_decoded_value_param(array_params,"name","=")
"Johann Backend"
iex > array_params = ["name=Johann Backend","age=54","address=404 street"]
iex > Krug.StringUtil.get_decoded_value_param(array_params,"address","=")
"404 street"
Link to this function

index_of(string, substring, skip_verification \\ false)

View Source (since 1.1.38)

Searches the position of first occurency of a substring on a string. Returns -1 for no result found, or if one of parameters is null value.

You could use "skip_verification" parameter as true, if you are sure that values already were verified, this will improve performance.

Examples

iex > Krug.StringUtil.index_of("my full string text","today",true)
-1
iex > Krug.StringUtil.index_of(nil,"today",true)
throw exception
iex > Krug.StringUtil.index_of("my full string text",nil,true)
throw exception
iex > Krug.StringUtil.index_of(nil,"today")
-1
iex > Krug.StringUtil.index_of("my full string text",nil)
-1
iex > Krug.StringUtil.index_of("my full string text","today")
-1
iex > Krug.StringUtil.index_of("my full string text","my")
0
iex > Krug.StringUtil.index_of("my full string text","my full")
0
iex > Krug.StringUtil.index_of("my full string text","full")
3
Link to this function

left_spaces(string, size, unsafe \\ false)

View Source

Receives a value, force to string and completes the value with left spaces until the value size == size parameter value received.

Useful for visual formatting and bank services for example.

If size is nil or <= 0, return the value received.

If size is < that value received size, return the value received truncated to size.

The parameter unsafe can be set to true when you have total sure that all parameters are in binary format and not null/empty. This will improve some performance.

Examples

iex > Krug.StringUtil.left_spaces(nil,5)
"     "
iex > Krug.StringUtil.left_spaces("",5)
"     "
iex > Krug.StringUtil.left_spaces(" ",5)
"     "
iex > Krug.StringUtil.left_spaces("A",5)
"    A"
iex > Krug.StringUtil.left_spaces("AB",5)
"   AB"
iex > Krug.StringUtil.left_spaces(33,5)
"   33"
iex > Krug.StringUtil.left_spaces(33.4,5)
" 33.4"
iex > Krug.StringUtil.left_spaces(33.45,5)
"33.45"
iex > Krug.StringUtil.left_spaces(33.456,5)
"33.45"
iex > Krug.StringUtil.left_spaces(33.4567,5)
"33.45"
iex > Krug.StringUtil.left_spaces(33.45678,5)
"33.45"
Link to this function

left_zeros(string, size, unsafe \\ false)

View Source

Receives a value, force to string and completes the value with left zeros until the value size == size parameter value received.

Useful for visual formatting and bank services for example.

If size is nil or <= 0, return the value received.

If size is < that value received size, return the value received truncated to size.

The parameter unsafe can be set to true when you have total sure that all parameters are in binary format and not null/empty. This will improve some performance.

Examples

iex > Krug.StringUtil.left_zeros(nil,5)
"00000"
iex > Krug.StringUtil.left_zeros("",5)
"00000"
iex > Krug.StringUtil.left_zeros(" ",5)
"0000 "
iex > Krug.StringUtil.left_zeros("A",5)
"0000A"
iex > Krug.StringUtil.left_zeros("AB",5)
"000AB"
iex > Krug.StringUtil.left_zeros(33,5)
"00033"
iex > Krug.StringUtil.left_zeros(33.4,5)
"033.4"
iex > Krug.StringUtil.left_zeros(33.45,5)
"33.45"
iex > Krug.StringUtil.left_zeros(33.456,5)
"33.45"
iex > Krug.StringUtil.left_zeros(33.4567,5)
"33.45"
iex > Krug.StringUtil.left_zeros(33.45678,5)
"33.45"
Link to this function

raw_binary_to_string(raw_string)

View Source (since 1.1.7)

Convert a string value in raw binary format <<xxx,xx,xx>> to string

Examples

iex > Krug.StringUtil.raw_binary_to_string(<<65,241,111,32,100,101,32,70,97,99,116>>)
"Año de Fact"
Link to this function

replace(target, searched, replace_to, unsafe \\ false)

View Source

Replace searched string value by replace_to string value, into target string. Replaces recursively all occurences if is not present the recursion throwble. Otherwise replace one single time all occurencies without recursive calls when recursion throwble is detected.

Recursion throwble occur when searched is contained in replace_to. Example: [searched = "123" and replace_to = "a x 123"]

     or [searched = "123" and replace_to = " 123 "]
     or [searched = "123" and replace_to = "123"].

The parameter unsafe can be set to true when you have total sure that all parameters are in binary format and not null/empty. This will improve some performance.

Examples

iex > Krug.StringUtil.replace("aa   bb    cc","  "," ")
"aa bb cc"
iex > Krug.StringUtil.replace("aa       bb               cc","  "," ")
"aa bb cc"
iex > phrase = "replace all e letters by C letter"
iex > Krug.StringUtil.replace(phrase,"e","c")
"rcplacc all c lcttcrs by C lcttcr"
iex > phrase = "replace non recursive because recursion throwble place"
iex > Krug.StringUtil.replace(phrase,"ce","[Ace Ventures]")
"repla[Ace Ventures] non recursive because recursion throwble pla[Ace Ventures]"
Link to this function

replace_all(target, searched_array, replace_to)

View Source

Replaces all occurrences of each one element in searched_array into target, by replace_to string value.

Uses recursively the replace(target,searched,replace_to) function, and because of this the rules for replacement are the same.

If target is nil return nil.

If target is empty string, or searched_array is nil/empty array return empty string.

Examples

iex > Krug.StringUtil.replace_all("afbfc   bfb    cfdc",["  ","f","c"],"0")
"a0b000 b0b0000d0"
iex > Krug.StringUtil.replace_all("aa       bb               cc",["  ","f","c"],"0")
"aa000 bb0000000 00"
iex > phrase = "replace all e letters by C letter"
iex > Krug.StringUtil.replace_all(phrase1,["e","a","p","t"],"c")
"rcclccc cll c lccccrs by C lccccr"
iex > phrase = "replace non recursive because recursion throwble place"
iex > Krug.StringUtil.replace_all(phrase,["ce","ur"],"[Ace Ventures]")
"repla[Ace Vent[Ace Ventures]es] non rec[Ace Ventures]sive because rec[Ace Ventures]sion 
throwble pla[Ace Vent[Ace Ventures]es]"
Link to this function

right_spaces(string, size, unsafe \\ false)

View Source

Receives a value, force to string and completes the value with right spaces until the value size == size parameter value received.

Useful for visual formatting and bank services for example.

If size is nil or <= 0, return the value received.

If size is < that value received size, return the value received truncated to size.

The parameter unsafe can be set to true when you have total sure that all parameters are in binary format and not null/empty. This will improve some performance.

Examples

iex > Krug.StringUtil.right_spaces(nil,5)
"     "
iex > Krug.StringUtil.right_zeros("",5)
"     "
iex > Krug.StringUtil.right_spaces(" ",5)
"     "
iex > Krug.StringUtil.right_spaces("A",5)
"A    "
iex > Krug.StringUtil.right_spaces("AB",5)
"AB   "
iex > Krug.StringUtil.right_spaces(33,5)
"33   "
iex > Krug.StringUtil.right_spaces(33.4,5)
"33.4 "
iex > Krug.StringUtil.right_spaces(33.45,5)
"33.45"
iex > Krug.StringUtil.right_spaces(33.456,5)
"33.45"
iex > Krug.StringUtil.right_spaces(33.4567,5)
"33.45"
iex > Krug.StringUtil.right_spaces(33.45678,5)
"33.45"
Link to this function

right_zeros(string, size, unsafe \\ false)

View Source

Receives a value, force to string and completes the value with right zeros until the value size == size parameter value received.

Useful for visual formatting and bank services for example.

If size is nil or <= 0, return the value received.

If size is < that value received size, return the value received truncated to size.

The parameter unsafe can be set to true when you have total sure that all parameters are in binary format and not null/empty. This will improve some performance.

Examples

iex > Krug.StringUtil.right_zeros(nil,5)
"00000"
iex > Krug.StringUtil.right_zeros("",5)
"00000"
iex > Krug.StringUtil.right_zeros(" ",5)
" 0000"
iex > Krug.StringUtil.right_zeros("A",5)
"A0000"
iex > Krug.StringUtil.right_zeros("AB",5)
"AB000"
iex > Krug.StringUtil.right_zeros(33,5)
"33000"
iex > Krug.StringUtil.right_zeros(33.4,5)
"33.40"
iex > Krug.StringUtil.right_zeros(33.45,5)
"33.45"
iex > Krug.StringUtil.right_zeros(33.456,5)
"33.45"
iex > Krug.StringUtil.right_zeros(33.4567,5)
"33.45"
iex > Krug.StringUtil.right_zeros(33.45678,5)
"33.45"
Link to this function

slice(string, start_position, end_position)

View Source (since 1.1.0)

Obtain a substring of a string begining the cut at start_position position and finishing cut at end_position. Same expected parameters and results as String.slice/3 function, however more performatic than.

No safety verifications implemented due to preserve the performance. Take care on use (only pass valid strings and start/end interval between string length).

Examples

iex > Krug.StringUtil.slice("ABCDEFGH",1,5)
"BCDEF"
Link to this function

split(target, searched, unsafe \\ false)

View Source

Receive a string target and split it to an array of strings.

If target is nil return empty array.

If target is empty string return an array with empty string.

If searched is nil/empty string or target don't contains searched, return an array with target string.

The parameter unsafe can be set to true when you have total sure that all parameters are in binary format and not null/empty. This will improve some performance.

Examples

iex > Krug.StringUtil.split(nil,nil)
[]
iex > Krug.StringUtil.split(nil,"")
[]
iex > Krug.StringUtil.split("",nil)
[""]
iex > Krug.StringUtil.split("","")
[""]
iex > Krug.StringUtil.split("ABC",nil)
["ABC"]
iex > Krug.StringUtil.split("ABC","")
["ABC"]
iex > Krug.StringUtil.split("ABC","-")
["ABC"]
iex > Krug.StringUtil.split("A-B-C","-")
["A","B","C"]
iex > Krug.StringUtil.split(" A-B-C","-")
[" A","B","C"]
iex > Krug.StringUtil.split(" A-B-C ","-")
[" A","B","C "]
iex > Krug.StringUtil.split("-A-B-C-","-")
["","A","B","C",""]
Link to this function

to_char(char_code_string)

View Source

Convert a numeric value char_code_string received to the correspondent character alfanumeric of any alphabet of any language.

Useful in various functionalities that encode/decode chars.

Examples

iex > Krug.StringUtil.StringUtil.to_char("")
""
iex > Krug.StringUtil.StringUtil.to_char(" ")
""
iex > Krug.StringUtil.StringUtil.to_char("A")
""
iex > Krug.StringUtil.StringUtil.to_char("AB")
""
iex > Krug.StringUtil.StringUtil.to_char(5)
""
iex > Krug.StringUtil.StringUtil.to_char(65)
"A"
iex > Krug.StringUtil.StringUtil.to_char(225)
"á"
iex > Krug.StringUtil.StringUtil.to_char(16000)
"㺀"
Link to this function

to_char_code(array, position)

View Source

Convert a string character value alfanumeric, of any alphabet of any language, contained in array received to the correspondent char code.

Useful in various functionalities that encode/decode chars.

If array is nil/empty or position > size of array return nil.

If element at position is empty/nil return nil.

Examples

iex > Krug.StringUtil.to_char_code(nil,0) 
nil
iex > Krug.StringUtil.to_char_code([],0) 
nil
iex > Krug.StringUtil.to_char_code([nil],0) 
nil
iex > Krug.StringUtil.to_char_code([""],0) 
nil
iex > Krug.StringUtil.to_char_code([""],3) 
nil
iex > Krug.StringUtil.to_char_code([" "],0)
32
iex > Krug.StringUtil.to_char_code([""],0)
5
iex > Krug.StringUtil.to_char_code(["A"],0)
65
iex > Krug.StringUtil.to_char_code(["á"],0)
225
iex > Krug.StringUtil.to_char_code(["㺀"],0)
16000
iex > Krug.StringUtil.to_char_code([nil,"",3,[],%{},"A"],5)
65
Link to this function

trim(string, unsafe \\ false)

View Source

Convert a value to string, returning the value without left and right spaces.

The parameter unsafe can be set to true when you have total sure that all parameters are in binary format and not null/empty. This will improve some performance.

Examples

iex > Krug.StringUtil.trim(nil)
""
iex > Krug.StringUtil.trim("")
""
iex > Krug.StringUtil.trim(" ")
""
iex > Krug.StringUtil.trim(10.5)
"10.5"
iex > Krug.StringUtil.trim(" 10")
"10"
iex > Krug.StringUtil.trim(" 10.5 ")
"10.5"