2013-04-29 3 views
11

मैं string.split(delimiter) के लिए C में समान कार्य ++ रहा हूँ यह निर्दिष्ट सीमांकक की कटौती की तार की एक सरणी वापसी करता है करने के लिए इसी तरह के कार्य करते हैं। ।जावा के string.split ("") C++

http://docs.oracle.com/javase/6/docs/api/java/lang/String.html#split(java.lang.String)

एक अच्छा जवाब था लेकिन लेखक हटा दिया था।

vector<string> split(string str, string sep){ 
    char* cstr=const_cast<char*>(str.c_str()); 
    char* current; 
    vector<std::string> arr; 
    current=strtok(cstr,sep.c_str()); 
    while(current != NULL){ 
     arr.push_back(current); 
     current=strtok(NULL, sep.c_str()); 
    } 
    return arr; 
} 
+0

'[java] निकाला जा रहा है' जवाब के रूप में टैग जावा के साथ कुछ भी होने की संभावना नहीं है। –

+0

देखो इस http://stackoverflow.com/questions/7583090/split-function-for-c – jpfonsek

उत्तर

3

आप strtok उपयोग कर सकते हैं। http://www.cplusplus.com/reference/cstring/strtok/

#include <string> 
#include <vector> 
#include <string.h> 
#include <stdio.h> 
std::vector<std::string> split(std::string str,std::string sep){ 
    char* cstr=const_cast<char*>(str.c_str()); 
    char* current; 
    std::vector<std::string> arr; 
    current=strtok(cstr,sep.c_str()); 
    while(current!=NULL){ 
     arr.push_back(current); 
     current=strtok(NULL,sep.c_str()); 
    } 
    return arr; 
} 
int main(){ 
    std::vector<std::string> arr; 
    arr=split("This--is--split","--"); 
    for(size_t i=0;i<arr.size();i++) 
     printf("%s\n",arr[i].c_str()); 
    return 0; 
} 
+0

मैं इसे नष्ट कर दिया, क्योंकि मैं नहीं Strok शीर्षक में (जो शायद strtok हो गया है) को नोटिस नहीं किया था: डी – jakubinf

1

मुझे लगता है कि यह अन्य ढेर अतिप्रवाह सवाल इस सवाल का जवाब हो सकता है:

Split a string in C++?

सारांश में, वहाँ जावा के साथ की तरह कोई अंतर्निहित विधि है, लेकिन कोई भी उपयोगकर्ता इसी बहुत समान लिखा था विधि:

https://stackoverflow.com/a/236803/1739039

संबंधित मुद्दे