Strings

Strings and Text

The String Object

Whilst the string is considered one of MaSH’s basic types, MaSH also includes the Symfony String Component which provides many advanced methods for strings such as search and replace, append and prepend, pad and trim and many more.

Natural

printline mash.str("HELLO FROM MASH").lower() #hello from mash
printline mash.str("hello from mash").upper() #HELLO FROM MASH

printline

printline mash.str("hello from mash").title() #Hello from mash
printline mash.str("hello from mash").title(true) #Hello From Mash

printline

printline mash.str("hello from mash").camel() #helloFromMash
printline mash.str("hello from mash").snake() #hello_from_mash

printline

# Check if the string starts/ends with the given string
printline mash.str("https://foldr.io").startsWith("https") #true
printline mash.str("mash-info.pdf").endsWith(".pdf") #true

printline
# Replaces all occurrences of the given string
printline mash.str("http://foldr.io").replace("http://", "https://") #https://foldr.io

Standard

printline(mash.str("HELLO FROM MASH").lower()) #hello from mash
printline(mash.str("hello from mash").upper()) #HELLO FROM MASH

printline()

printline(mash.str("hello from mash").title()) #Hello from mash
printline(mash.str("hello from mash").title(true)) #Hello From Mash

printline()

printline(mash.str("hello from mash").camel()) #helloFromMash
printline(mash.str("hello from mash").snake()) #hello_from_mash

printline()

# Check if the string starts/ends with the given string
printline(mash.str("https://foldr.io").startsWith("https")) #true
printline(mash.str("mash-info.pdf").endsWith(".pdf")) #true

printline
# Replaces all occurrences of the given string
printline(mash.str("http://foldr.io").replace("http://", "https://")) #https://foldr.io

Output

hello from mash
HELLO FROM MASH

Hello from mash
Hello From Mash

helloFromMash
hello_from_mash

true
true

https://foldr.io

Notes

strings and advanced strings can be used interchangeably within MaSH.

Natural

block sayHelloTo name
  printline "Hello {{ name }}"
  printline "Hello {{ mash.str(name).title() }} (in title case)"
end

sayHelloTo "grace hopper"
sayHelloTo mash.str("alan turing")

Standard

block sayHelloTo(name) {
  printline "Hello {{ name }}"
  printline "Hello {{ mash.str(name).title() }} (in title case)"
}

sayHelloTo("grace hopper")
sayHelloTo(mash.str("alan turing"))

Output

Hello grace hopper
Hello Grace hopper (in title case)
Hello alan turing
Hello Alan turing (in title case)

Measuring and slicing strings

The string object also lets you measure a string and pull pieces out of it. To get the number of characters in a string, wrap it with mash.str() and call .length(). A plain string has no .length or .size property of its own, so always reach for mash.str(...).length().

Natural

printline mash.str("hello world").length() #11

printline

# Take part of a string (start index, length). Indexes start at 0.
printline mash.str("hello world").slice(0, 5) #hello

# Shorten a string to a maximum number of characters
printline mash.str("hello world").truncate(8) #hello wo

# Remove surrounding whitespace
printline mash.str("  hello  ").trim() #hello

# Find where one string first appears inside another (-1 if it is absent)
printline mash.str("hello world").indexOf("world") #6

printline

# Split a string into an array on a separator
set parts to mash.str("apple,banana,coconut").split(",")
printline count(parts) #3

Standard

printline(mash.str("hello world").length()) #11

printline()

printline(mash.str("hello world").slice(0, 5)) #hello
printline(mash.str("hello world").truncate(8)) #hello wo
printline(mash.str("  hello  ").trim()) #hello
printline(mash.str("hello world").indexOf("world")) #6

printline()

parts = mash.str("apple,banana,coconut").split(",")
printline(count(parts)) #3

Output

11

hello
hello wo
hello
6

3

Regular expressions and Strings

We can use MaSH’s Regular Expressions to perform searches and replacements on String objects using the match and replaceMatches methods.

Natural

set mobile to mash.str("(+44) 7911 123456")

printline mobile.replaceMatches(/[^A-Za-z0-9]++/, "")

printline

set img to mash.str("photo-73647.png")

printline img.match(/photo-(\d+)\.png/)

Standard

mobile = mash.str("(+44) 7911 123456")

printline(mobile.replaceMatches(/[^A-Za-z0-9]++/, ""))

printline()

img = mash.str("photo-73647.png")

printline(img.match(/photo-(\d+)\.png/))

Output

447911123456

Array [
    "photo-73647.png",
    "73647"
]

← All articles