String Methods

Path: /src/mod/string.coffee compiled 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 - String text to be checked
  • literal - String phrase to match
  • start - Integer optional offset character position to start looking for
Return
Boolean true if 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 - String text to be checked
  • literal - String phrase to match
  • back - Integer optional offset character position from the end
Return
Boolean true if 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 - String phrase to repeat
  • num - Integer number of repeats to do
Return
String the 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 - String text to be padded
  • length - Integer final length of text
  • [char=' - Character '] character used for padding
Return
String the 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 - String text to be padded
  • length - Integer final length of text
  • [char=' - Character '] character used for padding
Return
String the 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 - String text to be padded
  • length - Integer final length of text
  • [char=' - Character '] character used for padding
Return
String the 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 - String text to be trimmed
  • [chars=' - String \n\t’] list of characters to trim off
Return
String the 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 - String text to be trimmed
  • [chars=' - String \n\t’] list of characters to trim off
Return
String the 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 - String text to be trimmed
  • [chars=' - String \n\t’] list of characters to trim off
Return
String the 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 - String text to be changed
Return
String the 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 - String text to be changed
Return
String the 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 - String text to be checked
  • phrase - String text to be searched for
Return
Boolean true if 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 - String
  • width - Integer optional maximum amount of characters per line (default: 80)
  • brk - String optional string that will be added whenever it’s needed to break the line (default: \n)
Return
String the 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 - String text to shorten
  • limit - Integer maximum number of characters
Return
String shortend 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 - String data to split
  • rowDelimiter - String|RegExp optional text or regular expression to divide into lines (default: \n)
  • {String|RegExp\ - [colDelimiter] text or regular expression to divide into columns
Return
Array the 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 - String to be converted
Return
RegExp|String regular expression or ioriginal text

Debugging

Debugging is possible using environment setting:

DEBUG=util:string    -> each method call