Krug.StringUtil (Krug v0.4.11) 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.

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.

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.

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.

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)

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.

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

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

left_spaces(string, size)

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.

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)

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.

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

replace(target, searched, replace_to)

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"].

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)

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.

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)

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.

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"

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.

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

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

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"