馃摑 Stringi
KitsuneScript oferuje bogaty zestaw mo偶liwo艣ci pracy z tekstem, w艂膮czaj膮c template strings, raw strings i metody OOP.
Tworzenie string贸w
let str1 = "Hello, World!";
let str2 = 'Single quotes';
let name = "Kitsune";
let msg = `Cze艣膰, ${name}!`;
Template Strings (interpolacja)
let name = "Kitsune";
let age = 25;
let msg = `Jestem ${name} i mam ${age} lat!`;
let a = 10, b = 20;
print(`${a} + ${b} = ${a + b}`);
print(`Random: ${randomInt(1, 100)}`);
let html = `
<div>
<h1>Witaj, ${name}!</h1>
<p>Wiek: ${age}</p>
</div>
`;
Raw Strings
Raw strings nie interpretuj膮 sekwencji escape:
let normal = "Line1\nLine2";
print(normal);
let raw = r"Line1\nLine2";
print(raw);
let path = r"C:\Users\Name\Documents";
let pattern = r"\d+\.\d+";
Multi-line Strings
let poem = """
Roses are red,
Violets are blue,
KitsuneScript is great,
And so are you!
""";
let json = """
{
"name": "Kitsune",
"version": "1.0.0"
}
""";
Escape sequences
| Sekwencja |
Znaczenie |
\n | Nowa linia |
\t | Tabulacja |
\r | Carriage 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;
text.first;
text.last;
text.isEmpty;
Transformacje
let text = " Hello World ";
text.trim();
text.toUpperCase();
text.toLowerCase();
text.reverse();
" test ".trim().toUpperCase().reverse();
Wyszukiwanie
"hello".contains("ell");
"hello".startsWith("he");
"hello".endsWith("lo");
"hello".indexOf("l");
"hello".lastIndexOf("l");
Manipulacja
"a,b,c".split(",");
"hello".replace("l", "L");
"hello".substring(1, 4);
"hello".charAt(0);
"ha".repeat(3);
"42".padStart(5, "0");
"42".padEnd(5, "0");
String multiplication
let stars = "*" * 5;
let border = "=" * 20;
let laugh = 3 * "ha";
let empty = "ab" * 0;
fn padLeft(text, width, char) {
let padding = width - size(text);
return (char * padding) + text;
}
print(padLeft("42", 5, "0"));
Dost臋p do znak贸w
let text = "Hello";
print(text[0]);
print(text[4]);
print(text[-1]);
print(text[-2]);
print(text[0:3]);
print(text[1:-1]);
print(text[-3:]);
Funkcje z modu艂u string
Po use "string" masz dost臋p do dodatkowych funkcji:
use "string";
split("a,b,c", ",");
join(["a", "b", "c"], "-");
trim(" hello ");
replace("hello", "l", "L");
replaceAll("aaa", "a", "b");
toLowerCase("HELLO");
toUpperCase("hello");
capitalize("hello");
camelCase("hello world");
snakeCase("helloWorld");
kebabCase("helloWorld");
reverse("hello");
repeat("ab", 3);
padStart("42", 5, "0");
padEnd("42", 5, "0");
contains("hello", "ell");
startsWith("hello", "he");
endsWith("hello", "lo");
charAt("hello", 0);
charCodeAt("A", 0);
fromCharCode(65);
Konkatenacja
let full = "Hello" + " " + "World";
let msg = "Value: " + 42;
let name = "Kit";
let greeting = `Hello, ${name}!`;
馃挕 Wskaz贸wka
Template strings (`...${...}`) s膮 preferowanym sposobem budowania string贸w - s膮 czytelniejsze i bezpieczniejsze od konkatenacji.