@rbxts/jsnatives



startsWith

Determines whether a string begins with the characters of a specified string.

Signature

function startsWith(str: string, search: PatternAdmissible, position?: NumericAdmissible): boolean

Description

The startsWith function checks if a string begins with a specified substring. It returns true if the string starts with the specified characters, and false otherwise. The search is case-sensitive.

Parameters

Return value

Examples

Basic usage

// Check if a string starts with a substring
const startsWithHello = String.startsWith("Hello world", "Hello");
print(startsWithHello); // Outputs: true
const startsWithWorld = String.startsWith("Hello world", "world");
print(startsWithWorld); // Outputs: false

With starting position

// Start checking from position 6
const text = "Hello world";
const startsWithWorld = String.startsWith(text, "world", 6);
print(startsWithWorld); // Outputs: true
// Using a position beyond the substring
const startsWithHe = String.startsWith(text, "He", 1);
print(startsWithHe); // Outputs: false

Using non-string values

// Checking with a number (converted to string)
const startsWithNumber = String.startsWith("123456", 123);
print(startsWithNumber); // Outputs: true
// Checking with a boolean (converted to string)
const startsWithBoolean = String.startsWith("true story", true);
print(startsWithBoolean); // Outputs: true

Case sensitivity

// The check is case-sensitive
const text = "Hello world";
const startsWithhello = String.startsWith(text, "hello");
print(startsWithhello); // Outputs: false
const startsWithHello = String.startsWith(text, "Hello");
print(startsWithHello); // Outputs: true

Practical applications

// Check if a filename has a specific extension
function hasExtension(filename: string, extension: string): boolean {
// Make sure extension starts with a dot
if (!String.startsWith(extension, ".")) {
extension = `.${extension}`;
}
return String.endsWith(filename, extension);
}
print(hasExtension("document.pdf", "pdf")); // Outputs: true
print(hasExtension("image.jpg", ".jpg")); // Outputs: true
print(hasExtension("script.ts", "js")); // Outputs: false
// Validate URL protocol
function isHttpUrl(url: string): boolean {
return String.startsWith(url, "http://") || String.startsWith(url, "https://");
}
print(isHttpUrl("https://example.com")); // Outputs: true
print(isHttpUrl("ftp://example.com")); // Outputs: false