Krug.StringUtil (Krug v0.1.0) 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 whit all words capitalized.

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

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

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 whit left spaces until the value size == size parameter value received.

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

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

Replaces all occurrences of each one element in searchedArray into target, by replaceTo string value.

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

Receives a value, force to string and completes the value whit 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 charCodeString 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 whitout left and right spaces.

Link to this section Functions

Convert a value to string whit 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, valueIfEmptyOrNull)

View Source

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

If valueIfEmptyOrNull 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(stringA, stringB, joinString)

View Source

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

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

containsOneElementOfArray(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.containsOneElementOfArray(nil,nil)
false
iex > Krug.StringUtil.containsOneElementOfArray("",nil)
false
iex > Krug.StringUtil.containsOneElementOfArray(" ",nil)
false
iex > Krug.StringUtil.containsOneElementOfArray("abcdef[]",[0,1,2,[]])
false
iex > Krug.StringUtil.containsOneElementOfArray("abcdef5",[0,1,2,[5,7]])
false
iex > Krug.StringUtil.containsOneElementOfArray("abcdef5,7",[0,1,2,[5,7]])
false
iex > Krug.StringUtil.containsOneElementOfArray("abcdef[5,7]",[0,1,2,[5,7]])
false
iex > Krug.StringUtil.containsOneElementOfArray("abcdef[]",[0,1,2,[],"]"])
true
iex > Krug.StringUtil.containsOneElementOfArray("abcdef[]",[0,1,2,[],"bc"])
true
iex > Krug.StringUtil.containsOneElementOfArray("abcdef[]",[0,1,2,[],"def["])
true
iex > Krug.StringUtil.containsOneElementOfArray("abcdef8[]",[0,1,2,[],8])
true

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

Example

iex > Krug.StringUtil.decodeUri("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.emptyIfNil(nil)
""
iex > Krug.StringUtil.emptyIfNil("")
""
iex > Krug.StringUtil.emptyIfNil(" ")
" "
iex > Krug.StringUtil.emptyIfNil("A")
"A"
iex > Krug.StringUtil.emptyIfNil(10)
"10"
iex > Krug.StringUtil.emptyIfNil(10.05)
"10.05"
iex > Krug.StringUtil.emptyIfNil(-10.05)
"-10.05"
Link to this function

getDecodedValueParam(arrayParams, 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 > arrayParams = ["name=Johann Backend","age=54","address=404 street"]
iex > Krug.StringUtil.getDecodedValueParam(arrayParams,"name","=")
"Johann Backend"
iex > arrayParams = ["name=Johann Backend","age=54","address=404 street"]
iex > Krug.StringUtil.getDecodedValueParam(arrayParams,"address","=")
"404 street"
Link to this function

leftSpaces(string, size)

View Source

Receives a value, force to string and completes the value whit 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.leftSpaces(nil,5)
"     "
iex > Krug.StringUtil.leftSpaces("",5)
"     "
iex > Krug.StringUtil.leftSpaces(" ",5)
"     "
iex > Krug.StringUtil.leftSpaces("A",5)
"    A"
iex > Krug.StringUtil.leftSpaces("AB",5)
"   AB"
iex > Krug.StringUtil.leftSpaces(33,5)
"   33"
iex > Krug.StringUtil.leftSpaces(33.4,5)
" 33.4"
iex > Krug.StringUtil.leftSpaces(33.45,5)
"33.45"
iex > Krug.StringUtil.leftSpaces(33.456,5)
"33.45"
iex > Krug.StringUtil.leftSpaces(33.4567,5)
"33.45"
iex > Krug.StringUtil.leftSpaces(33.45678,5)
"33.45"

Receives a value, force to string and completes the value whit 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.leftZeros(nil,5)
"00000"
iex > Krug.StringUtil.leftZeros("",5)
"00000"
iex > Krug.StringUtil.leftZeros(" ",5)
"0000 "
iex > Krug.StringUtil.leftZeros("A",5)
"0000A"
iex > Krug.StringUtil.leftZeros("AB",5)
"000AB"
iex > Krug.StringUtil.leftZeros(33,5)
"00033"
iex > Krug.StringUtil.leftZeros(33.4,5)
"033.4"
iex > Krug.StringUtil.leftZeros(33.45,5)
"33.45"
iex > Krug.StringUtil.leftZeros(33.456,5)
"33.45"
iex > Krug.StringUtil.leftZeros(33.4567,5)
"33.45"
iex > Krug.StringUtil.leftZeros(33.45678,5)
"33.45"
Link to this function

replace(target, searched, replaceTo)

View Source

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

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

     or [searched = "123" and replaceTo = " 123 "]
     or [searched = "123" and replaceTo = "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

replaceAll(target, searchedArray, replaceTo)

View Source

Replaces all occurrences of each one element in searchedArray into target, by replaceTo string value.

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

If target is nil return nil.

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

Examples

iex > Krug.StringUtil.replaceAll("afbfc   bfb    cfdc",["  ","f","c"],"0")
"a0b000 b0b0000d0"
iex > Krug.StringUtil.replaceAll("aa       bb               cc",["  ","f","c"],"0")
"aa000 bb0000000 00"
iex > phrase = "replace all e letters by C letter"
iex > Krug.StringUtil.replaceAll(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.replaceAll(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

rightSpaces(string, size)

View Source

Receives a value, force to string and completes the value whit 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.rightSpaces(nil,5)
"     "
iex > Krug.StringUtil.rightZeros("",5)
"     "
iex > Krug.StringUtil.rightSpaces(" ",5)
"     "
iex > Krug.StringUtil.rightSpaces("A",5)
"A    "
iex > Krug.StringUtil.rightSpaces("AB",5)
"AB   "
iex > Krug.StringUtil.rightSpaces(33,5)
"33   "
iex > Krug.StringUtil.rightSpaces(33.4,5)
"33.4 "
iex > Krug.StringUtil.rightSpaces(33.45,5)
"33.45"
iex > Krug.StringUtil.rightSpaces(33.456,5)
"33.45"
iex > Krug.StringUtil.rightSpaces(33.4567,5)
"33.45"
iex > Krug.StringUtil.rightSpaces(33.45678,5)
"33.45"
Link to this function

rightZeros(string, size)

View Source

Receives a value, force to string and completes the value whit 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.rightZeros(nil,5)
"00000"
iex > Krug.StringUtil.rightZeros("",5)
"00000"
iex > Krug.StringUtil.rightZeros(" ",5)
" 0000"
iex > Krug.StringUtil.rightZeros("A",5)
"A0000"
iex > Krug.StringUtil.rightZeros("AB",5)
"AB000"
iex > Krug.StringUtil.rightZeros(33,5)
"33000"
iex > Krug.StringUtil.rightZeros(33.4,5)
"33.40"
iex > Krug.StringUtil.rightZeros(33.45,5)
"33.45"
iex > Krug.StringUtil.rightZeros(33.456,5)
"33.45"
iex > Krug.StringUtil.rightZeros(33.4567,5)
"33.45"
iex > Krug.StringUtil.rightZeros(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 whit empty string.

If searched is nil/empty string or target don't contains searched, return an array whit 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",""]

Convert a numeric value charCodeString 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.toChar("")
""
iex > Krug.StringUtil.StringUtil.toChar(" ")
""
iex > Krug.StringUtil.StringUtil.toChar("A")
""
iex > Krug.StringUtil.StringUtil.toChar("AB")
""
iex > Krug.StringUtil.StringUtil.toChar(5)
""
iex > Krug.StringUtil.StringUtil.toChar(65)
"A"
iex > Krug.StringUtil.StringUtil.toChar(225)
"á"
iex > Krug.StringUtil.StringUtil.toChar(16000)
"㺀"
Link to this function

toCharCode(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.toCharCode(nil,0) 
nil
iex > Krug.StringUtil.toCharCode([],0) 
nil
iex > Krug.StringUtil.toCharCode([nil],0) 
nil
iex > Krug.StringUtil.toCharCode([""],0) 
nil
iex > Krug.StringUtil.toCharCode([""],3) 
nil
iex > Krug.StringUtil.toCharCode([" "],0)
32
iex > Krug.StringUtil.toCharCode([""],0)
5
iex > Krug.StringUtil.toCharCode(["A"],0)
65
iex > Krug.StringUtil.toCharCode(["á"],0)
225
iex > Krug.StringUtil.toCharCode(["㺀"],0)
16000
iex > Krug.StringUtil.toCharCode([nil,"",3,[],%{},"A"],5)
65

Convert a value to string, returning the value whitout 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"