C++ tách chuỗi có nhiều khoảng trắng

Nếu bạn muốn tách một chuỗi thành nhiều dấu cách, hãy chuyển một biểu thức chính quy, i. e. , /\s+/ đến phương thức split(). Biểu thức chính quy này sẽ phân tách chuỗi dựa trên một hoặc nhiều khoảng trắng

let str = "Chocolate   Pastry    Biscuit  Cake";

let arr = str.split(/\s+/);
console.log(arr); //["Chocolate", "Pastry", "Biscuit", "Cake"]

Nếu một chuỗi có khoảng trắng ở đầu hoặc cuối, hãy gọi phương thức trim() để loại bỏ chúng trước khi gọi phương thức split(). Nếu bạn không làm điều này, thì phương thức split() sẽ trả về một mảng có các phần tử trống

let str = "  Chocolate   Pastry    Biscuit  Cake    ";

let arr = str.split(/\s+/);
console.log(arr); //["", "Chocolate", "Pastry", "Biscuit", "Cake", ""]

arr = str.trim().split(/\s+/);
console.log(arr); //["Chocolate", "Pastry", "Biscuit", "Cake"]

Trong JavaScript, một biểu thức chính quy được viết bên trong một cặp ký tự gạch chéo (/)

\s có nghĩa là bất kỳ ký tự khoảng trắng nào. Ký tự khoảng trắng bao gồm dấu cách, tab và dòng mới

Trong một biểu thức chính quy, dấu cộng (+) có một ý nghĩa đặc biệt. + khớp với một hoặc nhiều lần xuất hiện của mục trước nó

Đối tượng Stringstream có thể được khởi tạo bằng cách sử dụng đối tượng chuỗi, nó sẽ tự động mã hóa các chuỗi trên dấu cách. Cũng giống như “cin” luồng stringstream cho phép bạn đọc một chuỗi dưới dạng một luồng từ. Ngoài ra, chúng ta cũng có thể sử dụng chức năng getline để mã hóa chuỗi trên bất kỳ dấu phân cách ký tự đơn nào

Some of the Most Common used functions of StringStream.
clear() — flushes the stream 
str() —  converts a stream of words into a C++ string object.
operator << — pushes a string object into the stream.
operator >> — extracts a word from the stream.

Đoạn mã dưới đây chứng minh điều đó.  

C++




Output: Hi 
    do 
    you
    do
    !
9

geeks for geeks
0
geeks for geeks
1
geeks for geeks
2

 

geeks for geeks
3

geeks for geeks
4
geeks for geeks
5

Output: Geeks
    for
    Geeks
7

Output: Geeks
    for
    Geeks
8
geeks for geeks
8

Output: Geeks
    for
    Geeks
8
// expregexp is the delimiting regular expression; 
// limit is the number of returned strings
public String[] split(String regexp, int limit);

// We can call split() without limit also
public String[] split(String regexp)
0

Output: Geeks
    for
    Geeks
8
Some of the Most Common used functions of StringStream.
clear() — flushes the stream 
str() —  converts a stream of words into a C++ string object.
operator << — pushes a string object into the stream.
operator >> — extracts a word from the stream.
8
// expregexp is the delimiting regular expression; 
// limit is the number of returned strings
public String[] split(String regexp, int limit);

// We can call split() without limit also
public String[] split(String regexp)
3

Output : How 
     do 
     you
     do!
2
// expregexp is the delimiting regular expression; 
// limit is the number of returned strings
public String[] split(String regexp, int limit);

// We can call split() without limit also
public String[] split(String regexp)
5

Output: Geeks
    for
    Geeks
8
Output: Hi 
    do 
    you
    do
    !
4

Output: Hi 
    do 
    you
    do
    !
4

 

// expregexp is the delimiting regular expression; 
// limit is the number of returned strings
public String[] split(String regexp, int limit);

// We can call split() without limit also
public String[] split(String regexp)
9

Geeks
for-Geeks

Geeks
for
Geeks
0

geeks for geeks
4
Geeks
for-Geeks

Geeks
for
Geeks
2
Output: Geeks
    for
    Geeks
9
Geeks
for-Geeks

Geeks
for
Geeks
4

Output: Geeks
    for
    Geeks
7

Output: Geeks
    for
    Geeks
8
geeks for geeks
8

Output: Geeks
    for
    Geeks
8
// expregexp is the delimiting regular expression; 
// limit is the number of returned strings
public String[] split(String regexp, int limit);

// We can call split() without limit also
public String[] split(String regexp)
0

Output: Geeks
    for
    Geeks
8
Some of the Most Common used functions of StringStream.
clear() — flushes the stream 
str() —  converts a stream of words into a C++ string object.
operator << — pushes a string object into the stream.
operator >> — extracts a word from the stream.
8
 
  // regexp is the delimiting regular expression; 
  // limit is limit the number of splits to be made 
  str.split(regexp = "", limit = string.count(str))  
2

Output : How 
     do 
     you
     do!
2
 
  // regexp is the delimiting regular expression; 
  // limit is limit the number of splits to be made 
  str.split(regexp = "", limit = string.count(str))  
4

Output : How 
     do 
     you
     do!
2
// expregexp is the delimiting regular expression; 
// limit is the number of returned strings
public String[] split(String regexp, int limit);

// We can call split() without limit also
public String[] split(String regexp)
5

Output: Geeks
    for
    Geeks
8
Output: Hi 
    do 
    you
    do
    !
4

Output: Hi 
    do 
    you
    do
    !
4

 

Output: Geeks
    for
    Geeks
5
Output: Geeks
    for
    Geeks
01
Output: Geeks
    for
    Geeks
5
Output: Geeks
    for
    Geeks
03
Output: Geeks
    for
    Geeks
9
Output: Geeks
    for
    Geeks
05
Output: Geeks
    for
    Geeks
06

Output: Geeks
    for
    Geeks
7

Output: Geeks
    for
    Geeks
8
Output: Geeks
    for
    Geeks
09
Output: Geeks
    for
    Geeks
10
Note:  The main disadvantage of strtok() is that it only works for C style strings.
       Therefore we need to explicitly convert C++ string into a char array.
       Many programmers are unaware that C++ has two additional APIs which are more elegant
       and works with C++ string. 
2

Output: Geeks
    for
    Geeks
8
Output: Geeks
    for
    Geeks
13
Output: Geeks
    for
    Geeks
14
Note:  The main disadvantage of strtok() is that it only works for C style strings.
       Therefore we need to explicitly convert C++ string into a char array.
       Many programmers are unaware that C++ has two additional APIs which are more elegant
       and works with C++ string. 
2

Output: Geeks
    for
    Geeks
8
Output: Geeks
    for
    Geeks
17

Output: Geeks
    for
    Geeks
8
Output: Geeks
    for
    Geeks
19

Output: Geeks
    for
    Geeks
8
Output: Geeks
    for
    Geeks
21

Output: Geeks
    for
    Geeks
8
Output: Geeks
    for
    Geeks
23
Output: Geeks
    for
    Geeks
24
Some of the Most Common used functions of StringStream.
clear() — flushes the stream 
str() —  converts a stream of words into a C++ string object.
operator << — pushes a string object into the stream.
operator >> — extracts a word from the stream.
1

Output: Geeks
    for
    Geeks
8
Output: Geeks
    for
    Geeks
21

Output: Geeks
    for
    Geeks
8____3_______6
Output: Hi 
    do 
    you
    do
    !
7

Output: Hi 
    do 
    you
    do
    !
4

Output : How 
     do 
     you
     do!

Phương pháp 2. Sử dụng API find() và substr() của C++

Điều kiện tiên quyết. tìm chức năng và substr()

Phương pháp này mạnh mẽ hơn và có thể phân tích một chuỗi bằng bất kỳ dấu phân cách nào, không chỉ dấu cách (mặc dù hành vi mặc định là phân tách trên dấu cách. ) Logic khá đơn giản để hiểu từ mã bên dưới

C++




Output: Hi 
    do 
    you
    do
    !
9

geeks for geeks
0
geeks for geeks
1
geeks for geeks
2

 

geeks for geeks
4
Output: Geeks
    for
    Geeks
37_______10_______38
Output: Geeks
    for
    Geeks
39

Output: Geeks
    for
    Geeks
7

Output: Geeks
    for
    Geeks
8
Output: Geeks
    for
    Geeks
5
Output: Geeks
    for
    Geeks
43

Output: Geeks
    for
    Geeks
8
Output: Geeks
    for
    Geeks
45
Output: Geeks
    for
    Geeks
7

Output : How 
     do 
     you
     do!
2
Output: Geeks
    for
    Geeks
48

Output : How 
     do 
     you
     do!
2
Output: Geeks
    for
    Geeks
50

Output : How 
     do 
     you
     do!
2
Output: Geeks
    for
    Geeks
52

Output: Geeks
    for
    Geeks
8
Output: Hi 
    do 
    you
    do
    !
4
Some of the Most Common used functions of StringStream.
clear() — flushes the stream 
str() —  converts a stream of words into a C++ string object.
operator << — pushes a string object into the stream.
operator >> — extracts a word from the stream.
8
Output: Geeks
    for
    Geeks
56

Output: Hi 
    do 
    you
    do
    !
4

Output: Geeks
    for
    Geeks
5
Output: Geeks
    for
    Geeks
01
Output: Geeks
    for
    Geeks
5
Output: Geeks
    for
    Geeks
03
Output: Geeks
    for
    Geeks
9
Output: Geeks
    for
    Geeks
05
Output: Geeks
    for
    Geeks
06

Output: Geeks
    for
    Geeks
7

Output: Geeks
    for
    Geeks
8
Output: Geeks
    for
    Geeks
67

Output: Geeks
    for
    Geeks
8
Output: Geeks
    for
    Geeks
09
Output: Geeks
    for
    Geeks
70
Note:  The main disadvantage of strtok() is that it only works for C style strings.
       Therefore we need to explicitly convert C++ string into a char array.
       Many programmers are unaware that C++ has two additional APIs which are more elegant
       and works with C++ string. 
2

Output: Geeks
    for
    Geeks
8
Output: Geeks
    for
    Geeks
73
Output: Geeks
    for
    Geeks
74
Some of the Most Common used functions of StringStream.
clear() — flushes the stream 
str() —  converts a stream of words into a C++ string object.
operator << — pushes a string object into the stream.
operator >> — extracts a word from the stream.
1

Output: Geeks
    for
    Geeks
8
Output: Geeks
    for
    Geeks
21

 

Output: Geeks
    for
    Geeks
8____3_______6
Output: Hi 
    do 
    you
    do
    !
7

Output: Hi 
    do 
    you
    do
    !
4

Output: Hi 
    do 
    you
    do
    !

Phương pháp 3. Sử dụng chuỗi tạm thời

Nếu bạn được cung cấp rằng độ dài của dấu phân cách là 1, thì bạn chỉ cần sử dụng chuỗi tạm thời để tách chuỗi. Điều này sẽ tiết kiệm thời gian hoạt động của chức năng trong trường hợp của phương pháp 2

Làm cách nào để tách một chuỗi có dấu cách trong C?

Để tách chuỗi chúng ta cần có dấu phân cách - dấu phân cách là các ký tự sẽ được sử dụng để tách chuỗi. Giả sử, chúng tôi có chuỗi sau và chúng tôi muốn trích xuất các từ riêng lẻ. char str[] = "strtok cần được gọi nhiều lần để tách chuỗi"; Các từ được phân tách bằng dấu cách.

Làm cách nào để tách các từ trong một chuỗi trong C?

Trong C, hàm strtok() được sử dụng để tách một chuỗi thành một loạt mã thông báo dựa trên một dấu phân cách cụ thể. Mã thông báo là một chuỗi con được trích xuất từ ​​chuỗi gốc.

Làm cách nào để tách một chuỗi trong C mà không cần sử dụng strtok?

5 câu trả lời .
bắt đầu bằng một con trỏ tới đầu chuỗi
lặp lại từng ký tự, tìm kiếm dấu phân cách của bạn
mỗi khi bạn tìm thấy một chuỗi, bạn có một chuỗi từ vị trí cuối cùng có độ dài khác nhau - hãy làm những gì bạn muốn với chuỗi đó
đặt vị trí bắt đầu mới thành dấu phân cách + 1 và chuyển sang bước 2

Làm cách nào để tách một chuỗi trong C bằng dấu phân cách?

Bạn có thể sử dụng hàm strtok() để tách một chuỗi (và chỉ định dấu phân cách sẽ sử dụng) . Lưu ý rằng strtok() sẽ sửa đổi chuỗi được truyền vào nó. Nếu chuỗi gốc được yêu cầu ở nơi khác, hãy tạo một bản sao của nó và chuyển bản sao đó tới strtok().