Schrift
Wiki:Tipp zum Debugging: use Data::Dumper; local $Data::Dumper::Useqq = 1; print Dumper \@var;
[thread]1156[/thread]

umwandeln string in array

Leser: 3


<< >> 10 Einträge, 1 Seite
Gast Gast
 2007-05-16 19:38
#11551 #11551
hallo @lle,
wie kann ich ein string in array konvertieren?
danke
PerlProfi
 2007-05-16 19:48
#11552 #11552
User since
2006-11-29
340 Artikel
BenutzerIn
[default_avatar]
Was genau meinst du jetzt?
Ein string ist ein array, suchst du eventuell c_str() oder data() ?

MfG\n\n

<!--EDIT|PerlProfi|1179330674-->
blaise4714
 2007-05-18 12:29
#11553 #11553
User since
2007-04-18
22 Artikel
BenutzerIn
[Homepage] [default_avatar]
hallo!
ich habe auch diesselbe frage. wie kann ich in C ein string in array umwandeln?
ZB:
collection = "ich;habe;hunger;na;und";
ich möchte dieses String in einer array umwandeln (anhand des trennzeichnens ";"), damit ich jedes elements lesen kann
array[0] = "ich"
array[1] = "habe"
usw...
danke
MisterL
 2007-05-18 12:39
#11554 #11554
User since
2006-07-05
334 Artikel
BenutzerIn
[default_avatar]
Darf man die Aufmerksamkeit der Leserschaft auf
Openbook über C lenken ?
“Perl is the only language that looks the same before and after RSA encryption.”
murphy
 2007-05-18 12:43
#11555 #11555
User since
2004-07-19
1776 Artikel
HausmeisterIn
[Homepage]
user image
Wenn nur ANSI C zur Verfügung steht, muss man sich hier mit einer oder mehreren der Funktionen size_t strcspn(const char *str, const char *set), char *strpbrk(const char *str, const char *set), char *strchr(const char *str, int chr) und char *strtok(char *str, const char *set) behelfen.

Wenn man eine vernünftige Laufzeitbibliothek zur Verfügung hat, gibt es eventuell noch etwas komfortablere und threadsicherere Funktionen -- GLib bietet beispielsweise gchar **g_strsplit(const gchar *string, const gchar *delimiter, gint max_tokens) an, was sich viel mehr wie die Splitfunktion in Perl verhält.
When C++ is your hammer, every problem looks like your thumb.
lichtkind
 2007-05-18 12:52
#11556 #11556
User since
2004-03-22
5679 Artikel
ModeratorIn + EditorIn
[Homepage]
user image
hab übersehen das es hier um c geht\n\n

<!--EDIT|lichtkind|1179478931-->
Wiki:Tutorien in der Wiki, mein zeug:
kephra, baumhaus, garten, gezwitscher

Es beginnt immer mit einer Entscheidung.
esskar
 2007-05-18 16:10
#11557 #11557
User since
2003-08-04
7321 Artikel
ModeratorIn

user image
strtok find ich dafür ganz nett!
shigetsu
 2007-06-06 02:14
#11558 #11558
User since
2007-04-22
16 Artikel
BenutzerIn
[Homepage] [default_avatar]
[quote=esskar,18.05.2007, 14:10]strtok find ich dafür ganz nett![/quote]
Ja, aber man bedenke hierzu:

Quote
Because strtok() modifies it's argument, the string is subsequently unsafe and cannot be used in its original form. If you need to preserve the original string, copy it into a buffer and pass the address of the buffer to strtok() instead of the original string.
Del Piero
 2007-06-11 14:01
#11559 #11559
User since
2007-03-21
28 Artikel
BenutzerIn
[default_avatar]
wie wärs wenn du eine funktion schreibst die bis zum ';' jedes einzelne zeichen in dein array[0] reinkopiert und dann wieder bis zum nächsten ';' in array[1] kopiert usw....???
esskar
 2007-06-11 14:26
#11560 #11560
User since
2003-08-04
7321 Artikel
ModeratorIn

user image
und wenn man eh schon die StdLib benutzt, z.b. auch so

Code: (dl )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
std::vector<std::string> StringSplit(const std::string & str, const std::string & pattern) {
std::vector<std::string> retval;
std::string work = str;

int pos;
while((pos = work.find(pattern)) != std::string::npos) {
std::string item = work.substr(0, pos);
retval.push_back(item);

work = work.substr(pos + pattern.length());
}

if(work != "")
retval.push_back(work);

return retval;
}
<< >> 10 Einträge, 1 Seite



View all threads created 2007-05-16 19:38.