Stringlar
String nima?
String — bu belgilar ketma-ketligi bo‘lib, odatda so‘z yoki gaplar kabi matnni ifodalash uchun ishlatiladi.
Stringlar dasturlashdagi eng keng tarqalgan ma’lumot tiplaridan biri bo‘lib, tilga qarab yakka tirnoq 'Hello' yoki qo‘shtirnoq "Hello" ichiga olinadi.
name = "Jenny"
print(name)
const name = "Jenny";
console.log(name);
String name = "Jenny";
System.out.println(name);
string name = "Jenny";
cout << name;
O‘zingiz sinab ko‘ring »
Stringlarni birlashtirish (konkatenatsiya)
+ operatori yordamida stringlarni birlashtirishingiz (konkatenatsiya qilishingiz) mumkin.
first = "Hello"
last = "World"
message = first + " " + last
print(message)
const first = "Hello";
const last = "World";
const message = first + " " + last;
console.log(message);
String first = "Hello";
String last = "World";
String message = first + " " + last;
System.out.println(message);
string first = "Hello";
string last = "World";
string message = first + " " + last;
cout << message;
String uzunligi
O‘rnatilgan funksiya yoki xususiyat yordamida stringda nechta belgi borligini aniqlashingiz mumkin.
txt = "Hello"
print(len(txt))
const txt = "Hello";
console.log(txt.length);
String txt = "Hello";
System.out.println(txt.length());
string txt = "Hello";
cout << txt.length();
Belgilarga murojaat qilish
Stringlar belgilar massiviga o‘xshaydi, shuning uchun indeks (0 dan boshlanadi) yordamida bitta belgiga murojaat qilishingiz mumkin.
txt = "Hello"
print(txt[1]) # prints 'e'
const txt = "Hello";
console.log(txt[1]); // prints 'e'
String txt = "Hello";
System.out.println(txt.charAt(1)); // prints 'e'
string txt = "Hello";
cout << txt[1]; // prints 'e'
Keng tarqalgan string amallari
Stringlar bilan eng ko‘p bajariladigan amallar:
- Katta yoki kichik harflarga o‘tkazish
- String boshqa stringni o‘z ichiga olganini tekshirish
- String ichidagi matnni almashtirish
txt = "Hello World"
print(txt.upper()) # HELLO WORLD
print(txt.lower()) # hello world
print("World" in txt) # True
print(txt.replace("World", "Python"))
const txt = "Hello World";
console.log(txt.toUpperCase()); // HELLO WORLD
console.log(txt.toLowerCase()); // hello world
console.log(txt.includes("World")); // true
console.log(txt.replace("World", "JavaScript"));
String txt = "Hello World";
System.out.println(txt.toUpperCase()); // HELLO WORLD
System.out.println(txt.toLowerCase()); // hello world
System.out.println(txt.contains("World")); // true
System.out.println(txt.replace("World", "Java")); // Hello Java
#include <iostream>
#include <string>
#include <algorithm> // for transform
#include <cctype> // for toupper, tolower
using namespace std;
int main() {
string txt = "Hello World";
// Convert to uppercase
string upper_txt = txt;
transform(upper_txt.begin(), upper_txt.end(), upper_txt.begin(), ::toupper);
cout << upper_txt << endl; // HELLO WORLD
// Convert to lowercase
string lower_txt = txt;
transform(lower_txt.begin(), lower_txt.end(), lower_txt.begin(), ::tolower);
cout << lower_txt << endl; // hello world
// Check if string contains "World"
if (txt.find("World") != string::npos) {
cout << "true" << endl; // true
} else {
cout << "false" << endl;
}
// Replace "World" with "C++"
string replaced_txt = txt;
size_t pos = replaced_txt.find("World");
if (pos != string::npos) {
replaced_txt.replace(pos, 5, "C++");
}
cout << replaced_txt << endl; // Hello C++
return 0;
}
Eslatma: Har bir dasturlash tilining o‘z string funksiyalari to‘plami bor. Uzunlikni aniqlash, belgilarga murojaat qilish va matnni almashtirish kabi eng keng tarqalgan amallar deyarli barcha tillarda mavjud.
Hayotiy misol
Stringlar ayniqsa foydalanuvchi kiritgan ma’lumotlar bilan ishlashda foydali. Masalan, deylik, foydalanuvchini ismi bilan qutlaydigan dastur yaratyapmiz:
name = "Jenny"
greeting = "Hello, " + name + "!"
print(greeting)
const name = "Jenny";
const greeting = "Hello, " + name + "!";
console.log(greeting);
String name = "Jenny";
String greeting = "Hello, " + name + "!";
System.out.println(greeting);
string name = "Jenny";
string greeting = "Hello, " + name + "!";
cout << greeting;
W3Schools Pathfinder
Yutuqlaringizni kuzating – bu bepul!
