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 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 << — 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 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 << — 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 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_______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 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 << — pushes a string object into the stream. operator >> — extracts a word from the stream.1 Output: Geeks for Geeks8 Output: Geeks for Geeks21 Output: Geeks for Geeks8 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 geeks0 geeks for geeks1 geeks for geeks2
geeks for geeks4 Output: Geeks for Geeks37_______8_______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_______8_______48 Output : How do you do!2______8_______50 Output : How do you do!2____8_______52 Output: Geeks for Geeks8 Output: Hi do you do !4_______0_______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 << — pushes a string object into the stream. operator >> — extracts a word from the stream.1 Output: Geeks for Geeks8 Output: Geeks for Geeks21
Output: Geeks for Geeks8 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 |