String Methods
Path:
/src/mod/string.coffeecompiled to/lib/mod/string.js
starts()
This is a handy method to check that a certain string starts with the given phrase.
Example:
CoffeeScript Code util = require 'alinex-util'
test = 'abcdefg'
result = util.string.starts test, 'ab'
This results to:
result = true
Usage:
starts(string, literal, start)
- Parameter
-
string-Stringtext to be checkedliteral-Stringphrase to matchstart-Integeroptional offset character position to start looking for
- Return
Booleantrueif string starts with literal
ends()
Peek at the end of a given string to see if it matches a sequence.
Example:
CoffeeScript Code util = require 'alinex-util'
test = 'abcdefg'
result = util.string.ends test, 'fg'
This results to:
result = true
Usage:
ends(string, literal, back)
- Parameter
-
string-Stringtext to be checkedliteral-Stringphrase to matchback-Integeroptional offset character position from the end
- Return
Booleantrueif string ends with literal
repeat()
Repeat a given string multiple times.
Example:
CoffeeScript Code util = require 'alinex-util'
test = 'ab'
result = util.string.repeat test, 3
This results to:
result = 'ababab'
Usage:
repeat(string, num)
- Parameter
-
string-Stringphrase to repeatnum-Integernumber of repeats to do
- Return
Stringthe repeated text
lpad()
Left pad string to specified length.
Example:
CoffeeScript Code util = require 'alinex-util'
test = '5'
result = util.string.lpad test, 3, '0'
This results to:
result = '005'
Usage:
lpad(string, length, [char=')
- Parameter
-
string-Stringtext to be paddedlength-Integerfinal length of text[char='-Character'] character used for padding
- Return
Stringthe padded text
rpad()
Right pad string to specified length.
Example:
CoffeeScript Code util = require 'alinex-util'
test = 'abc'
result = util.string.rpad test, 5, ' '
This results to:
result = 'abc '
Usage:
rpad(string, length, [char=')
- Parameter
-
string-Stringtext to be paddedlength-Integerfinal length of text[char='-Character'] character used for padding
- Return
Stringthe padded text
cpad()
Center pad string to specified length. This will add the padding on both sides. For udd number of padding the padding on the right will be one character more as on the left.
Example:
CoffeeScript Code util = require 'alinex-util'
test = 'abc'
result = util.string.cpad test, 5, ' '
This results to:
result = 'abc '
Usage:
cpad(string, length, [char=')
- Parameter
-
string-Stringtext to be paddedlength-Integerfinal length of text[char='-Character'] character used for padding
- Return
Stringthe padded text
trim()
Trim given characters.
Example:
CoffeeScript Code util = require 'alinex-util'
test = '/var/local/'
result = util.string.trim test, '/'
This results to:
result = 'var/local'
Usage:
trim(string, [chars=')
- Parameter
-
string-Stringtext to be trimmed[chars='-String\n\t’] list of characters to trim off
- Return
Stringthe trimmed text
ltrim()
Left trim given characters.
Example:
CoffeeScript Code util = require 'alinex-util'
test = '/var/local/'
result = util.string.ltrim test, '/'
This results to:
result = 'var/local/'
Usage:
ltrim(string, [chars=')
- Parameter
-
string-Stringtext to be trimmed[chars='-String\n\t’] list of characters to trim off
- Return
Stringthe trimmed text
rtrim()
Right trim given characters.
Example:
CoffeeScript Code util = require 'alinex-util'
test = '/var/local/'
result = util.string.rtrim test, '/'
This results to:
result = '/var/local'
Usage:
rtrim(string, [chars=')
- Parameter
-
string-Stringtext to be trimmed[chars='-String\n\t’] list of characters to trim off
- Return
Stringthe trimmed text
ucFirst()
Make first letter upper case.
Example:
CoffeeScript Code util = require 'alinex-util'
test = 'abcdefg'
result = util.string.ucfirst test
This results to:
result = 'Abcdefg'
Usage:
ucFirst(string)
- Parameter
-
string-Stringtext to be changed
- Return
Stringthe text with first letter upper case
lcFirst()
Make first letter lower case.
Example:
CoffeeScript Code util = require 'alinex-util'
test = 'ABCDEFG'
result = util.string.lcFirst test
This results to:
result = 'aBCDEFG'
Usage:
lcFirst(string)
- Parameter
-
string-Stringtext to be changed
- Return
Stringthe text with first letter lower case
contains()
Check if string contains substring.
Example:
CoffeeScript Code util = require 'alinex-util'
test = 'abcdefg'
result1 = util.string.contains test, 'bc'
result2 = util.string.contains test, 'gh'
This results to:
result1 = true
result2 = false
Usage:
contains(string, phrase)
- Parameter
-
string-Stringtext to be checkedphrase-Stringtext to be searched for
- Return
Booleantrueif phrase is contained in string
wordwrap()
Word wraps a given text as needed.
Example:
CoffeeScript Code util = require 'alinex-util'
test = "All necessary parts are on the same machine, so that you only have to bring this
machine to work. Backups of the data are made on vs10152.
\n\n
Keep in mind that the machine is in the test net and you have to use a valid VPN
connection for accessing.
"""
result = util.string.wordwrap test, 78
This results to (variable result):
All necessary parts are on the same machine, so that you only have to bring
this machine to work. Backups of the data are made on vs10152.
Keep in mind that the machine is in the test net and you have to use a valid
VPN connection for accessing.
Usage:
wordwrap(str, width, brk)
- Parameter
-
str-Stringwidth-Integeroptional maximum amount of characters per line (default: 80)brk-Stringoptional string that will be added whenever it’s needed to break the line (default: \n)
- Return
Stringthe multiline text
shorten()
This will shorten the given text and add ellipsis if it is too long. This is done word aware.
Example:
CoffeeScript Code util = require 'alinex-util'
test = """
A high CPU usage means that the server may not start
another task immediately.
If the load is also very high the system is overloaded, check if any application
goes evil.
"""
result = util.string.shorten test, 68
This results to (variable result):
A high CPU usage means that the server may not start...
Usage:
shorten(str, limit)
- Parameter
-
str-Stringtext to shortenlimit-Integermaximum number of characters
- Return
Stringshortend text
toList()
Convert text into list or list of array (recordset).
Example:
CoffeeScript Code util = require 'alinex-util'
test = 'one,two,three\n1,2,3\n4,5,6'
list = util.string.toList test, /\n/
table = util.string.toList test, /\n/, /,/
This results to:
CoffeeScript Code list = [
'one,teo,three'
'1,2,3'
'4,5,6'
]
table = [
['one', 'two', 'three']
['1', '2', '3']
['4', '5', '6']
]
Usage:
toList(text, rowDelimiter, {String|RegExp\)
- Parameter
-
text-Stringdata to splitrowDelimiter-String|RegExpoptional text or regular expression to divide into lines (default: \n){String|RegExp\- [colDelimiter] text or regular expression to divide into columns
- Return
Arraythe new list or list of arrays
toRegExp()
Convert text into regular expression object if possible.
Example:
CoffeeScript Code util = require 'alinex-util'
test = '/\n/'
test = util.string.toRegExp test
list = util.string.split test # use the RegExp
Usage:
toRegExp(text)
- Parameter
-
text-Stringto be converted
- Return
RegExp|Stringregular expression or ioriginal text
Debugging
Debugging is possible using environment setting:
DEBUG=util:string -> each method call