馃摑 Stringi

KitsuneScript oferuje bogaty zestaw mo偶liwo艣ci pracy z tekstem, w艂膮czaj膮c template strings, raw strings i metody OOP.

Tworzenie string贸w

// Podw贸jne cudzys艂owy let str1 = "Hello, World!"; // Pojedyncze cudzys艂owy let str2 = 'Single quotes'; // Template strings (backticks) let name = "Kitsune"; let msg = `Cze艣膰, ${name}!`;

Template Strings (interpolacja)

let name = "Kitsune"; let age = 25; // Podstawowa interpolacja let msg = `Jestem ${name} i mam ${age} lat!`; // Wyra偶enia w interpolacji let a = 10, b = 20; print(`${a} + ${b} = ${a + b}`); // "10 + 20 = 30" // Wywo艂ania funkcji print(`Random: ${randomInt(1, 100)}`); // Wieloliniowe template strings let html = ` <div> <h1>Witaj, ${name}!</h1> <p>Wiek: ${age}</p> </div> `;

Raw Strings

Raw strings nie interpretuj膮 sekwencji escape:

// Normalny string - \n to nowa linia let normal = "Line1\nLine2"; print(normal); // Line1 // Line2 // Raw string - \n pozostaje jako tekst let raw = r"Line1\nLine2"; print(raw); // Line1\nLine2 // Idealne do 艣cie偶ek Windows let path = r"C:\Users\Name\Documents"; // Idealne do regex let pattern = r"\d+\.\d+";

Multi-line Strings

let poem = """ Roses are red, Violets are blue, KitsuneScript is great, And so are you! """; // Przydatne do JSON, HTML, SQL let json = """ { "name": "Kitsune", "version": "1.0.0" } """;

Escape sequences

Sekwencja Znaczenie
\nNowa linia
\tTabulacja
\rCarriage return
\\Backslash
\"Cudzys艂贸w
\'Apostrof

Metody string贸w (OOP style)

Mo偶esz wywo艂ywa膰 metody bezpo艣rednio na stringach:

W艂a艣ciwo艣ci

let text = " Hello World "; text.length; // 15 text.first; // " " text.last; // " " text.isEmpty; // false

Transformacje

let text = " Hello World "; text.trim(); // "Hello World" text.toUpperCase(); // " HELLO WORLD " text.toLowerCase(); // " hello world " text.reverse(); // " dlroW olleH " // Chainowanie " test ".trim().toUpperCase().reverse(); // "TSET"

Wyszukiwanie

"hello".contains("ell"); // true "hello".startsWith("he"); // true "hello".endsWith("lo"); // true "hello".indexOf("l"); // 2 "hello".lastIndexOf("l"); // 3

Manipulacja

"a,b,c".split(","); // ["a", "b", "c"] "hello".replace("l", "L"); // "heLLo" "hello".substring(1, 4); // "ell" "hello".charAt(0); // "h" "ha".repeat(3); // "hahaha" "42".padStart(5, "0"); // "00042" "42".padEnd(5, "0"); // "42000"

String multiplication

// Mno偶enie string贸w - powt贸rzenie let stars = "*" * 5; // "*****" let border = "=" * 20; // "====================" let laugh = 3 * "ha"; // "hahaha" (dzia艂a w obie strony) let empty = "ab" * 0; // "" // Praktyczne u偶ycie fn padLeft(text, width, char) { let padding = width - size(text); return (char * padding) + text; } print(padLeft("42", 5, "0")); // "00042"

Dost臋p do znak贸w

let text = "Hello"; // Indeksowanie (od 0) print(text[0]); // "H" print(text[4]); // "o" // Negatywne indeksy (od ko艅ca) print(text[-1]); // "o" (ostatni) print(text[-2]); // "l" // Slicing print(text[0:3]); // "Hel" print(text[1:-1]); // "ell" print(text[-3:]); // "llo"

Funkcje z modu艂u string

Po use "string" masz dost臋p do dodatkowych funkcji:

use "string"; split("a,b,c", ","); // ["a", "b", "c"] join(["a", "b", "c"], "-"); // "a-b-c" trim(" hello "); // "hello" replace("hello", "l", "L"); // "heLLo" replaceAll("aaa", "a", "b"); // "bbb" toLowerCase("HELLO"); // "hello" toUpperCase("hello"); // "HELLO" capitalize("hello"); // "Hello" camelCase("hello world"); // "helloWorld" snakeCase("helloWorld"); // "hello_world" kebabCase("helloWorld"); // "hello-world" reverse("hello"); // "olleh" repeat("ab", 3); // "ababab" padStart("42", 5, "0"); // "00042" padEnd("42", 5, "0"); // "42000" contains("hello", "ell"); // true startsWith("hello", "he"); // true endsWith("hello", "lo"); // true charAt("hello", 0); // "h" charCodeAt("A", 0); // 65 fromCharCode(65); // "A"

Konkatenacja

// Operator + let full = "Hello" + " " + "World"; // "Hello World" // Automatyczna konwersja let msg = "Value: " + 42; // "Value: 42" // Template strings (preferowane) let name = "Kit"; let greeting = `Hello, ${name}!`;
馃挕 Wskaz贸wka

Template strings (`...${...}`) s膮 preferowanym sposobem budowania string贸w - s膮 czytelniejsze i bezpieczniejsze od konkatenacji.