Python tách chuỗi được phân tách bằng dấu phẩy thành mảng

Đố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_______12_______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_______12_______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_______8_______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
Output: Hi 
    do 
    you
    do
    !
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_______8_______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_______8_______48

Output : How 
     do 
     you
     do!
2______8_______50

Output : How 
     do 
     you
     do!
2____8_______52

Output: Geeks
    for
    Geeks
8
Output: Hi 
    do 
    you
    do
    !
4_______0_______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
Output: Hi 
    do 
    you
    do
    !
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