strings
Методы строк
Часто используемые операции со строками
Показать языки:
JSJavaScript
const str = " Hello, World! ";
str.trim() // "Hello, World!"
str.toLowerCase() // " hello, world! "
str.toUpperCase() // " HELLO, WORLD! "
str.includes("World") // true
str.replace("World", "JS") // " Hello, JS! "
str.split(", ") // [" Hello", "World! "]
str.slice(2, 7) // "Hello"
str.length // 18PYPython
s = " Hello, World! "
s.strip() # "Hello, World!"
s.lower() # " hello, world! "
s.upper() # " HELLO, WORLD! "
"World" in s # True
s.replace("World", "Python") # " Hello, Python! "
s.split(", ") # [' Hello', 'World! ']
s[2:7] # "Hello"
len(s) # 18C#C#
string str = " Hello, World! ";
str.Trim() // "Hello, World!"
str.ToLower() // " hello, world! "
str.ToUpper() // " HELLO, WORLD! "
str.Contains("World") // true
str.Replace("World", "C#") // " Hello, C#! "
str.Split(", ") // [" Hello", "World! "]
str.Substring(2, 5) // "Hello"
str.Length // 18C++C++
#include <string>
#include <algorithm>
string str = "Hello, World!";
// trim — нет встроенного, используют boost или вручную
transform(str.begin(), str.end(),
str.begin(), ::tolower); // lower
str.find("World") != string::npos; // contains
str.replace(str.find("World"), 5, "C++");
str.substr(0, 5); // "Hello"
str.length(); // 13Пояснение
Python использует len() как функцию, остальные — свойство .length/.Length. В C++ нет встроенного trim и contains (до C++23) — нужно писать вручную или использовать Boost.