Share substr c++

lyancheers

New member
## Subr C ++

# C ++ Chuỗi

# Thao tác chuỗi

# Lập trình

# Hướng dẫn

** Subrt trong C ++ **

Hàm Subr () trong C ++ được sử dụng để trích xuất một chuỗi con từ một chuỗi.Chất nền là một chuỗi các ký tự tiếp giáp từ chuỗi gốc.Hàm con () có ba đối số:

* Đối số đầu tiên là chuỗi mà từ đó phần phụ sẽ được trích xuất.
* Đối số thứ hai là chỉ số bắt đầu của chuỗi con.
* Đối số thứ ba là số lượng ký tự được bao gồm trong chuỗi con.

Chỉ số bắt đầu của chuỗi con có thể là bất kỳ số nguyên nào từ 0 đến độ dài của chuỗi trừ 1. Số lượng ký tự được đưa vào trong chuỗi con có thể là bất kỳ số nguyên nào từ 0 đến độ dài của chuỗi trừ chỉ mục bắt đầu.

Nếu chỉ mục bắt đầu lớn hơn độ dài của chuỗi, hàm con () sẽ trả về một chuỗi trống.Nếu số lượng ký tự được bao gồm trong chuỗi con lớn hơn độ dài của chuỗi từ chỉ mục bắt đầu, hàm chuỗi con () sẽ trả về chuỗi con từ chỉ mục bắt đầu đến cuối chuỗi.

Mã sau đây cho thấy cách sử dụng hàm Subr () để trích xuất chuỗi con từ chuỗi:

`` `C ++
#include <Istream>
#include <String>

sử dụng không gian tên STD;

int main () {
// Xác định một chuỗi.
Chuỗi str = "Đây là một chuỗi";

// Trích xuất một chuỗi con từ chuỗi.
chuỗi chuỗi con = str.substr (0, 5);

// In hình nền.
cout << "Chất phụ là:" << Subring << endl;

trả lại 0;
}
`` `

Đầu ra:

`` `
Chất nền là: cái này
`` `

## Bài viết tham khảo

* [Chức năng Subr () trong C ++ - Hướng dẫn] (https://www.tutorialspoint.com/cplusplus/cpp_substr_function.htm)
* [Hàm con () trong c ++-geeksforgeek] (geeksforgeek.org - geeksforgeek Resources and Information.)
* [C ++ Subr () hàm - cppreference.com] (std::basic_string<CharT,Traits,Allocator>::substr - cppreference.com)
=======================================
## Substr C++

# C++ String

# String Manipulation

# Programming

# Tutorial

**Substr in C++**

The substr() function in C++ is used to extract a substring from a string. The substring is a contiguous sequence of characters from the original string. The substr() function takes three arguments:

* The first argument is the string from which the substring is to be extracted.
* The second argument is the starting index of the substring.
* The third argument is the number of characters to be included in the substring.

The starting index of the substring can be any integer from 0 to the length of the string minus 1. The number of characters to be included in the substring can be any integer from 0 to the length of the string minus the starting index.

If the starting index is greater than the length of the string, the substr() function returns an empty string. If the number of characters to be included in the substring is greater than the length of the string from the starting index, the substr() function returns the substring from the starting index to the end of the string.

The following code shows how to use the substr() function to extract a substring from a string:

```c++
#include <iostream>
#include <string>

using namespace std;

int main() {
// Define a string.
string str = "This is a string";

// Extract a substring from the string.
string substring = str.substr(0, 5);

// Print the substring.
cout << "The substring is: " << substring << endl;

return 0;
}
```

Output:

```
The substring is: This
```

## Reference Articles

* [Substr() Function in C++ - TutorialsPoint](https://www.tutorialspoint.com/cplusplus/cpp_substr_function.htm)
* [The substr() Function in C++ - GeeksforGeeks](https://www.geeksforgeeks.org/substr-function-in-c/)
* [C++ substr() Function - cppreference.com](https://en.cppreference.com/w/cpp/string/basic_string/substr)
 
Join Telegram ToolsKiemTrieuDoGroup
Back
Top