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 > — 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 geeks0
geeks for geeks1
geeks for geeks2
geeks for geeks3
geeks for geeks4
geeks for geeks5
Output: Geeks for Geeks7
Output: Geeks for Geeks8
geeks for geeks8
Output: Geeks for Geeks8
// 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 Geeks8
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 > — 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 Geeks8
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 Geeks0
geeks for geeks4
Geeks for-Geeks Geeks for Geeks2
Output: Geeks for Geeks9
Geeks for-Geeks Geeks for Geeks4
Output: Geeks for Geeks7
Output: Geeks for Geeks8
geeks for geeks8
Output: Geeks for Geeks8
// 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 Geeks8
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 > — 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 Geeks8
Output: Hi do you do !4
Output: Hi do you do !4
Output: Geeks for Geeks5
Output: Geeks for Geeks01
Output: Geeks for Geeks5
Output: Geeks for Geeks03
Output: Geeks for Geeks9
Output: Geeks for Geeks05
Output: Geeks for Geeks06
Output: Geeks for Geeks7
Output: Geeks for Geeks8
Output: Geeks for Geeks09
Output: Geeks for Geeks10
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 Geeks8
Output: Geeks for Geeks13
Output: Geeks for Geeks14
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 Geeks8
Output: Geeks for Geeks17
Output: Geeks for Geeks8
Output: Geeks for Geeks19
Output: Geeks for Geeks8
Output: Geeks for Geeks21
Output: Geeks for Geeks8
Output: Geeks for Geeks23
Output: Geeks for Geeks24
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 > — extracts a word from the stream.1
Output: Geeks for Geeks8
Output: Geeks for Geeks21
Output: Geeks for Geeks8____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 geeks0
geeks for geeks1
geeks for geeks2
geeks for geeks4
Output: Geeks for Geeks37_______10_______38
Output: Geeks for Geeks39
Output: Geeks for Geeks7
Output: Geeks for Geeks8
Output: Geeks for Geeks5
Output: Geeks for Geeks43
Output: Geeks for Geeks8
Output: Geeks for Geeks45
Output: Geeks for Geeks7
Output : How do you do!2
Output: Geeks for Geeks48
Output : How do you do!2
Output: Geeks for Geeks50
Output : How do you do!2
Output: Geeks for Geeks52
Output: Geeks for Geeks8
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 > — extracts a word from the stream.8
Output: Geeks for Geeks56
Output: Hi do you do !4
Output: Geeks for Geeks5
Output: Geeks for Geeks01
Output: Geeks for Geeks5
Output: Geeks for Geeks03
Output: Geeks for Geeks9
Output: Geeks for Geeks05
Output: Geeks for Geeks06
Output: Geeks for Geeks7
Output: Geeks for Geeks8
Output: Geeks for Geeks67
Output: Geeks for Geeks8
Output: Geeks for Geeks09
Output: Geeks for Geeks70
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 Geeks8
Output: Geeks for Geeks73
Output: Geeks for Geeks74
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 > — extracts a word from the stream.1
Output: Geeks for Geeks8
Output: Geeks for Geeks21
Output: Geeks for Geeks8____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