STRINGS
Strings in Vint
Strings are a sequence of characters used to represent text in the Vint programming language. Here’s a detailed explanation of how to work with strings, including syntax, manipulation, and useful built-in methods.
Basic Syntax
In Vint, strings can be enclosed in either single quotes ('') or double quotes (""):
print("Hello") // Output: Hello let name = 'Tachera' print("Hello", name) // Output: Hello Tachera
Concatenating Strings
Strings can be concatenated using the + operator:
let greeting = "Hello" + " " + "World" print(greeting) // Output: Hello World let message = "Hello" message += " World" // Output: Hello World
You can also repeat a string a specific number of times using the * operator:
print("Hello " * 3) // Output: Hello Hello Hello let repeated = "World" repeated *= 2 // Output: WorldWorld
Looping Over a String
You can loop through each character of a string using the for keyword:
let name = "Avicenna" for char in name { print(char) } // Output: // A // v // i // c // e // n // n // a
You can also loop through the string using its index and character:
for i, char in name { print(i, "=>", char) } // Output: // 0 => A // 1 => v // 2 => i // 3 => c // 4 => e // 5 => n // 6 => n // 7 => a
Comparing Strings
You can compare two strings using the == operator:
let a = "Vint" print(a == "Vint") // Output: true print(a == "vint") // Output: false
String Methods
Length of a String (length)
You can find the length of a string using the length method. It does not accept any parameters:
let message = "Vint" print(message.length()) // Output: 4
Convert to Uppercase (upper)
This method converts the string to uppercase:
let text = "vint" print(text.upper()) // Output: VINT
Convert to Lowercase (lower)
This method converts the string to lowercase:
let text = "VINT" print(text.lower()) // Output: vint
Split a String (split)
The split method splits a string into an array based on a specified delimiter. If no delimiter is provided, it splits by whitespace.
Example without a delimiter:
let sentence = "Vint programming language" let words = sentence.split() print(words) // Output: ["Vint", "programming", "language"]
Example with a delimiter:
let sentence = "Vint,programming,language" let words = sentence.split(",") print(words) // Output: ["Vint", "programming", "language"]
Replace Substrings (replace)
You can replace a substring with another string using the replace method:
let greeting = "Hello World" let newGreeting = greeting.replace("World", "Vint") print(newGreeting) // Output: Hello Vint
Trim Whitespace (trim)
You can remove whitespace from the start and end of a string using the trim method:
let text = " Hello World " print(text.trim()) // Output: "Hello World"
Check String Start (startsWith)
The startsWith method checks if a string starts with a specified prefix:
let text = "Hello World" print(text.startsWith("Hello")) // Output: true print(text.startsWith("World")) // Output: false
Check String End (endsWith)
The endsWith method checks if a string ends with a specified suffix:
let text = "Hello World" print(text.endsWith("World")) // Output: true print(text.endsWith("Hello")) // Output: false
Check String Contains (includes)
The includes method checks if a string contains a specified substring:
let text = "Hello World" print(text.includes("lo Wo")) // Output: true print(text.includes("xyz")) // Output: false
Repeat String (repeat)
The repeat method repeats a string a specified number of times:
let text = "Ha" print(text.repeat(3)) // Output: "HaHaHa" print("X".repeat(5)) // Output: "XXXXX"
Capitalize String (capitalize)
The capitalize method capitalizes the first letter of a string:
let text = "hello world" print(text.capitalize()) // Output: "Hello world"
Check if Numeric (isNumeric)
The isNumeric method checks if a string contains only numeric characters:
print("123".isNumeric()) // Output: true print("12.34".isNumeric()) // Output: true print("abc".isNumeric()) // Output: false print("12a".isNumeric()) // Output: false
Check if Alphabetic (isAlpha)
The isAlpha method checks if a string contains only alphabetic characters:
print("hello".isAlpha()) // Output: true print("Hello".isAlpha()) // Output: true print("hello123".isAlpha()) // Output: false print("hello world".isAlpha()) // Output: false (space is not alphabetic)
Case-Insensitive Compare (compareIgnoreCase)
The compareIgnoreCase method compares strings ignoring case differences:
let text = "Hello" print(text.compareIgnoreCase("hello")) // Output: 0 (equal) print(text.compareIgnoreCase("apple")) // Output: 1 (greater) print(text.compareIgnoreCase("zebra")) // Output: -1 (less)
Format String (format)
The format method applies simple formatting to strings using placeholders:
let template = "Hello {0}, you are {1} years old" print(template.format("John", 25)) // Output: "Hello John, you are 25 years old" let message = "The result is {0}" print(message.format(42)) // Output: "The result is 42"
Remove Accents (removeAccents)
The removeAccents method removes accent characters from a string:
print("café".removeAccents()) // Output: "cafe" print("naïve".removeAccents()) // Output: "naive" print("résumé".removeAccents()) // Output: "resume"
Convert to Integer (toInt)
The toInt method converts a string to an integer:
print("123".toInt()) // Output: 123 print("-456".toInt()) // Output: -456 // Note: Returns an error if the string is not a valid integer
let message = " Hello World " print(message.trim()) // Output: Hello World
Get a Substring (substring)
You can extract a substring from a string by specifying the starting and ending indices:
let sentence = "Vint programming" print(sentence.substring(0, 4)) // Output: Vint
Find the Index of a Substring (indexOf)
You can find the index of a substring within a string using the indexOf method:
let sentence = "Vint programming" print(sentence.indexOf("programming")) // Output: 5
Slugify a String (slug)
You can convert a string into a URL-friendly format (slug) using the slug method:
let title = "Creating a Slug String" print(title.slug()) // Output: creating-a-slug-string
Checking Substring Presence (contains)
Check if a string contains a specific substring:
let name = "Tachera Sasi" print(name.contains("Sasi")) // Output: true
Get Character at Index (charAt)
Get the character at a specific index:
let word = "Hello" print(word.charAt(1)) // Output: e print(word.charAt(10)) // Output: "" (empty string for out of bounds)
Repeat String (times)
Repeat a string a specified number of times:
let pattern = "Ha" print(pattern.times(3)) // Output: HaHaHa
Pad String Start (padStart)
Pad the string to a target length from the beginning:
let num = "5" print(num.padStart(3, "0")) // Output: 005 let word = "hi" print(word.padStart(5, "*")) // Output: ***hi
Pad String End (padEnd)
Pad the string to a target length from the end:
let num = "5" print(num.padEnd(3, "0")) // Output: 500 let word = "hi" print(word.padEnd(5, "*")) // Output: hi***
Check String Start (startsWith)
Check if a string starts with a specified prefix:
let message = "Hello World" print(message.startsWith("Hello")) // Output: true print(message.startsWith("World")) // Output: false
Check String End (endsWith)
Check if a string ends with a specified suffix:
let filename = "document.pdf" print(filename.endsWith(".pdf")) // Output: true print(filename.endsWith(".txt")) // Output: false
Extract Slice (slice)
Extract a section of the string:
let text = "Hello World" print(text.slice(0, 5)) // Output: Hello print(text.slice(6)) // Output: World print(text.slice(-5)) // Output: World
Example Usage
Here’s an example of how you might use these string operations in Vint:
import "string" // Example: Trim whitespace let trimmed = string.trim(" Hello, World! ") print(trimmed) // Output: "Hello, World!" // Example: Check if a string contains a substring let containsResult = string.contains("Hello, World!", "World") print(containsResult) // Output: true // Example: Convert to uppercase let upperResult = string.toUpper("hello") print(upperResult) // Output: "HELLO" // Example: Convert to lowercase let lowerResult = string.toLower("HELLO") print(lowerResult) // Output: "hello" // Example: Replace a substring let replaceResult = string.replace("Hello, World!", "World", "Vint") print(replaceResult) // Output: "Hello, Vint!" // Example: Split a string into parts let splitResult = string.split("a,b,c,d", ",") print(splitResult) // Output: ["a", "b", "c", "d"] // Example: Join string parts let joinResult = string.join(["a", "b", "c"], "-") print(joinResult) // Output: "a-b-c" // Example: Get the length of a string let lengthResult = string.length("Hello") print(lengthResult) // Output: 5
Example with Vint Data
Here's an example using Vint-specific strings:
let name = "Tachera Sasi" let reversed = name.reverse() print(reversed) // Output: "isaS arehcaT" let upperName = name.upper() print(upperName) // Output: "TACHERA SASI" let trimmedName = name.trim("T") print(trimmedName) // Output: "achera Sasi"
Understanding how to manipulate and work with strings in Vint allows you to efficiently handle text data in your programs.