Napisy string: najważniejsze metody
Klasy biblioteczne: Math, Random, string, StringBuilder - Podstawy C# (csharp)
Typ
string ma właściwość Length i wiele metod. Napis jest NIEZMIENNY: metody takie jak ToUpper, Replace czy Trim zwracają NOWY napis, oryginał zostaje. Znak na pozycji odczytujemy jak w tablicy: text[0] (typ char). W C# napisy porównujemy zwykłym == (w Javie trzeba equals). Substring(start, długość) przyjmuje DŁUGOŚĆ fragmentu, nie indeks końca. Split dzieli napis na tablicę.using System;
class Program
{
static void Main(string[] args)
{
string text = " Programowanie w C# jest proste ";
string trimmed = text.Trim();
Console.WriteLine($"[{trimmed}]");
Console.WriteLine("Długość: " + trimmed.Length);
Console.WriteLine("Pierwszy znak: " + trimmed[0]);
Console.WriteLine("Wielkie: " + trimmed.ToUpper());
Console.WriteLine("Małe: " + trimmed.ToLower());
Console.WriteLine("Substring(0, 13): " + trimmed.Substring(0, 13));
Console.WriteLine("IndexOf(\"C#\"): " + trimmed.IndexOf("C#"));
Console.WriteLine("Contains(\"jest\"): " + trimmed.Contains("jest"));
Console.WriteLine("StartsWith(\"Prog\"): " + trimmed.StartsWith("Prog"));
Console.WriteLine("Replace: " + trimmed.Replace("proste", "ciekawe"));
string[] words = trimmed.Split(' ');
Console.WriteLine("Słów: " + words.Length);
Console.WriteLine("Złączone: " + string.Join("-", words));
string a = "kot";
string b = "KOT".ToLower();
Console.WriteLine("a == b: " + (a == b));
}
}Ładowanie interaktywnego edytora... Jeśli się nie pojawi, włącz JavaScript.
Wszystkie lekcje kursu Podstawy C#·Zaloguj się, aby zapisać postęp na koncie.