Skip to content

Strings

.at

ts
// access by index
// out of range returns undefined
// negative indexes count back from end
const character = string.at(index)
swift
// No equivalent, use JustSugar* library
import JustSugar

// access by index
// out of range returns nil
// negative indexes count back from end
let character = string.at(index)

* see JustSugar String.at(_:)

.endsWith

ts
// check if string ends with characters
if (string.endsWith(characters)) { /* */ }
swift
// check if string ends with characters
if string.hasSuffix(characters) { /* */ }

.includes

ts
// check if string includes characters
if (string.includes(characters)) { /* */ }
swift
// check if string includes characters
if string.contains(characters) { /* */ }

.indexOf

ts
// find index of first occurrence of characters
const index = string.indexOf(characters)
swift
// find index of first occurrence of characters
let index = string.range(of: characters)?.lowerBound.utf16Offset(in: string)

// Shorthand with `import JustSugar` *
let index = string.indexOf(characters)

* see JustSugar String.indexOf(_:)

.lastIndexOf

ts
// find index of last occurrence of characters
const index = string.lastIndexOf(characters)
swift
// find index of last occurrence of characters
let index = string.range(of: characters, options: .backwards)?.lowerBound.utf16Offset(in: string)

// Shorthand with `import JustSugar` *
let index = string.lastIndexOf(characters)

* see JustSugar String.lastIndexOf(_:)

.matchAll

ts
// find all matches of a regular expression
const regex = /t(e)(st(\d?))/g

for (const match of "test1test2".matchAll(regex)) {
  console.log(`Found: ${match[0]}, groups: [${match[1]}, ${match[2]}, ${match[3]}] start: ${match.index}`)
}
// Found: test1, groups: [e, st1, 1] start: 0
// Found: test2, groups: [e, st2, 2] start: 5
swift
// find all matches of a regular expression
import RegexBuilder

// global by default
let regex = /t(e)(st(\d?))/
let string = "test1test2"

for match of string.matches(of: regex) {
		print("Found: \(match[0]), groups: [\(match[1]), \(match[2]), \(match[3])] start: \(match.range.lowerBound.utf16Offset(in: string))")
}
// Found: test1, groups: [e, st1, 1] start: 0
// Found: test2, groups: [e, st2, 2] start: 5

.padEnd

ts
// pad a string with characters to a target length
// eg. "title".padEnd(10, "-") -> "title-----"
const paddedString = string.padEnd(count, characters)
swift
// pad a string with characters to a target length
// eg. "title".padEnd(10, "-") -> "title-----"
let paddingCount = string.count - characters.count
let paddedString = string + String(repeating: characters, count: (paddingCount + characters.count - 1) / characters.count)

// Shorthand with `import JustSugar` *
let paddedString = string.padEnd(count, characters)

* see JustSugar String.padEnd(::)

.padStart

ts
// pad a string with characters to a target length
// eg. "1".padStart(3, "0") -> "001"
const paddedString = string.padStart(count, characters)
swift
// pad a string with characters to a target length
// eg. "1".padStart(3, "0") -> "001"
let paddingCount = string.count - characters.count
let paddedString = String(repeating: characters, count: (paddingCount + characters.count - 1) / characters.count) + string

// Shorthand with `import JustSugar` *
let paddedString = string.padStart(count, characters)

* see JustSugar String.padStart(::)

.repeat

ts
// repeat a string a number of times
// eg. "NY".repeat(3) -> "NYNYNY"
const newString = string.repeat(count)
swift
// repeat a string a number of times
// eg. "NY".repeat(3) -> "NYNYNY"
let newString = String(repeating: string, count: count)

.replace

ts
// replace first occurrence of characters with replacement
const newString = string.replace(characters, replacement)

// or some regex
const newString = string.replace(/regex/, replacement)
swift
// replace first occurrence of characters with replacement
let newString = string.replacing(characters, with: replacement, maxReplacements: 1)

// or some regex
let newString = string.replacing(/regex/, with: replacement, maxReplacements: 1)

.replaceAll

ts
// replace all occurrences of characters with replacement
const newString = string.replaceAll(characters, replacement)

// or some regex
const newString = string.replaceAll(/regex/g, replacement)
swift
// replace all occurrences of characters with replacement
let newString = string.replacing(characters, with: replacement)

// or some regex
let newString = string.replacing(/regex/, with: replacement)

.slice

ts
// extract characters from start index up to end index
// negative indexes count back from end
const characters = string.slice(start, end)
swift
// No equivalent, use JustSugar* library
import JustSugar

// extract characters from start index up to end index
// negative indexes count back from end
let characters = string.slice(start, end)

* see JustSugar String.slice(::)

.split

ts
// split a string into an array of substrings by a separator
const array = string.split(characters)
swift
// split a string into an array of substrings by a separator
let array = string.split(separator: characters, omittingEmptySubsequences: false).map(String.init)
// or
let array = string.components(separatedBy: characters)

// Shorthand with `import JustSugar` *
let array = string.split(characters)

* see JustSugar String.split(_:)

.startsWith

ts
// return `true` if string starts with characters
if (string.startsWith(characters)) { /* */ }
swift
// return `true` if string starts with characters
if string.starts(with: characters) { /* */ }
// or with regex
if string.starts(with: /regex/) { /* */ }

// also can use
if string.hasPrefix(characters) { /* */ }

.toLowerCase

ts
// convert a string to lowercase
const lower = string.toLowerCase()
swift
// convert a string to lowercase
let lower = string.lowercased()

.toUpperCase

ts
// convert a string to uppercase
const upper = string.toUpperCase()
swift
// convert a string to uppercase
let upper = string.uppercased()

.trim

ts
// remove whitespace from both ends of a string
const trimmed = string.trim()
swift
// remove whitespace from both ends of a string
let trimmed = string.trimmingCharacters(in: .whitespacesAndNewlines)

// Shorthand with `import JustSugar` *
let trimmed = string.trim()

* see JustSugar String.trim()

.trimEnd

ts
// remove whitespace from the end of a string
const trimmed = string.trimEnd()
swift
// remove whitespace from the end of a string
if let range = string.rangeOfCharacter(from: .whitespacesAndNewlines.inverted, options: .backwards) {
	let trimmed = String(string[..< range.upperBound])
}

// Shorthand with `import JustSugar` *
let trimmed = string.trimEnd()

* see JustSugar String.trimEnd()

.trimStart

ts
// remove whitespace from the start of a string
const trimmed = string.trimStart()
swift
// remove whitespace from the start of a string
if let range = string.rangeOfCharacter(from: .whitespacesAndNewlines.inverted) {
	let trimmed = String(string[range.lowerBound...])
}

// Shorthand with `import JustSugar` *
let trimmed = string.trimStart()

* see JustSugar String.trimStart()