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)

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