C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

String ersetzen

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ (German)
View previous topic :: View next topic  
Author Message
Dennis Dahn
Guest





PostPosted: Thu Feb 02, 2006 11:02 am    Post subject: String ersetzen Reply with quote



Hallo,

ich möchte eine Zeichenkette durch einen String ersetzen:

Ich habe es so versucht:

while (c=*unix++){
if (c=='/'){
if (first==1){
*temp++ = 'C';
*temp++ = ':';
*temp++ = '\\';
first = 0;
}
else
*temp++ = '\\';
}
else
*temp++ = c;
}
*temp = '\0';



Das geht so auch, nur ist das Ersetzen speziell bei first==1 ziemlich
schlecht. Hat jemande eine bessere Idee wie ich den String kopieren und den
Zeiger weitersetzen kann?

Vielen Dank!!

Gruß
Dennis

--
de.comp.lang.iso-c++ - Moderation: mailto:voyager+mod (AT) bud (DOT) prima.de
FAQ: http://www.voyager.prima.de/cpp/ mailto:voyager+send-faq (AT) bud (DOT) prima.de
Back to top
Ernst Murnleitner
Guest





PostPosted: Sat Feb 04, 2006 8:30 pm    Post subject: Re: String ersetzen Reply with quote



Dennis Dahn schrieb:
Quote:
Hallo,

ich möchte eine Zeichenkette durch einen String ersetzen:


Wenn Du einen C-String verwendest, dann musst Du Dir aber im klaren
sein, dass der String länger wird und die evt. einen Überlauf bekommst.
Daher verwende ich prinzipiell nur mehr std::string.

Ich weiss zwar nicht, was Du mit Zeichenkette und String meinst. Aber
offensichtlich verwendest Du einen C-String. Spricht was gegen std::string?

Quote:
Ich habe es so versucht:

while (c=*unix++){
if (c=='/'){
if (first==1){
*temp++ = 'C';
*temp++ = ':';
*temp++ = '\\';
first = 0;
}
else
*temp++ = '\\';
}
else
*temp++ = c;
}
*temp = '\0';



Das geht so auch, nur ist das Ersetzen speziell bei first==1 ziemlich
schlecht. Hat jemande eine bessere Idee wie ich den String kopieren und den
Zeiger weitersetzen kann?


Du willst vermutlich den slash '/' nur ersetzen, wenn er an der 1.
Stelle steht? In Deinem Beispiel würdest Du ja aus

"directory/file" => "directoryC:\file"

machen.


Ich würde es so lösen:

#include <string>

std::string s = unix;
int iPos;
while (1){
iPos = s.find("/");
if (iPos == std::string::npos)
break;
else if (iPos == 0)
s.replace(0,1,"C:\\");
else
s.replace(iPos,1,"\\");
}
temp = s.c_str();


Ernst

--
de.comp.lang.iso-c++ - Moderation: mailto:voyager+mod (AT) bud (DOT) prima.de
FAQ: http://www.voyager.prima.de/cpp/ mailto:voyager+send-faq (AT) bud (DOT) prima.de
Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ (German) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.