AnsiString: Zahl oder Buchstabe
-
Hi !
wie kann man in einem String beim parsen festellen ob Derstring[i] eine Zahl oder einen Buchstabe/Sonderzeichen enthält ??
MfG
Z01
-
Hallo
Indem Du nach dem ASCII-Wert schaust
char temp = derstring[i]; int x = int(temp); if(x <= weisichgradnicht && x >= weissichauchgradnicht) { // Zahl } else { // keine Zahl }Oder ob StrToInt einen Fehler verursacht. Deshalb in Try schalteln. Das ist aber die weitaus unschönste Lösung.
-
Genau wegen dem StrToInt frag ich. Vor dem anwenden wollte ich schon gern wissen obs auch wirklich nen integer ist weil sonst...ihr wisst ja.
Da is mir dein castint x = int(temp);und ein anschliesender Vergleich doch lieber.Ich ratter einfach mal ein paar Strings durch und schau was raus kommt.
Danke !
-
Die Zahlen erstrecken sich von 48 bis 57
Du kannst es auch mit StrToIntDef versuchen
extern PACKAGE int __fastcall StrToIntDef(const AnsiString S; const int Default);
-
Rostfrei[] schrieb:
extern PACKAGE int __fastcall StrToIntDef(const AnsiString S; const int Default);Sehr schön
vielen Dank !
-
if((DerString[i]>='0') && (DerString[i]<='9')) { // das zeichen ist eine Zahl } else { // keine zahl }natürlich werden hier Punkte/Komas nicht berücksichtigt..
-
Falsches Fenster. Sorry.
-
So sollte das doch Funktionieren:
for(int i = 1; i < outpack.Length();i++) { if(outpack[i] == '^' && i + 1 <= outpack.Length()) { if(StrToIntDef(outpack[i + 1],1111) != 1111) { // Zahl } else { // keine Zahl } } }
-
Hab die Methode jetzt fertig. So sollte man aus Strings Farbcodes entfernen können und spezifische ersetzen:
String firstrange = "",lastrange = ""; int index; for(int i = 1; i < outpack.Length();i++) { if(outpack[i] == '^' && i + 1 <= outpack.Length()) { if(StrToIntDef(outpack[i + 1],1111) != 1111) { // Zahl, also Farbcodes ausschneiden index = outpack.Pos("^"); firstrange = outpack.SubString(1,index - 1); lastrange = outpack.SubString(index + 2,outpack.Length() - (index + 1));//die 2 byte ausschneiden outpack = firstrange + lastrange; } else { // keine Zahl ,string teilen und neuen string in der mitte einfügen index = outpack.Pos("^"); if(index + 2 <= outpack.Length()) { if(outpack[index + 1] == 'v' && index + 2 <= outpack.Length()) { firstrange = outpack.SubString(1,index - 1) + "|"; lastrange = outpack.SubString(index + 2,outpack.Length() - (index + 1)); outpack = firstrange + lastrange; } if(outpack[index + 1] == 'a' && index + 2 <= outpack.Length()) { firstrange = outpack.SubString(1,index - 1) + "*"; lastrange = outpack.SubString(index + 2,outpack.Length() - (index + 1)); outpack = firstrange + lastrange; } if(outpack[index + 1] == 'c' && index + 2 <= outpack.Length()) { firstrange = outpack.SubString(1,index - 1) + ":"; lastrange = outpack.SubString(index + 2,outpack.Length() - (index + 1)); outpack = firstrange + lastrange; } if(outpack[index + 1] == 'd' && index + 2 <= outpack.Length()) { firstrange = outpack.SubString(1,index - 1) + "\\"; lastrange = outpack.SubString(index + 2,outpack.Length() - (index + 1)); outpack = firstrange + lastrange; } if(outpack[index + 1] == 's' && index + 2 <= outpack.Length()) { firstrange = outpack.SubString(1,index - 1) + "/"; lastrange = outpack.SubString(index + 2,outpack.Length() - (index + 1)); outpack = firstrange + lastrange; } if(outpack[index + 1] == 'q' && index + 2 <= outpack.Length()) { firstrange = outpack.SubString(1,index - 1) + "?"; lastrange = outpack.SubString(index + 2,outpack.Length() - (index + 1)); outpack = firstrange + lastrange; } if(outpack[index + 1] == 't' && index + 2 <= outpack.Length()) { firstrange = outpack.SubString(1,index - 1) + "\""; lastrange = outpack.SubString(index + 2,outpack.Length() - (index + 1)); outpack = firstrange + lastrange; } if(outpack[index + 1] == 'l' && index + 2 <= outpack.Length()) { firstrange = outpack.SubString(1,index - 1) + "<"; lastrange = outpack.SubString(index + 2,outpack.Length() - (index + 1)); outpack = firstrange + lastrange; } if(outpack[index + 1] == 'r' && index + 2 <= outpack.Length()) { firstrange = outpack.SubString(1,index - 1) + ">"; lastrange = outpack.SubString(index + 2,outpack.Length() - (index + 1)); outpack = firstrange + lastrange; } if(outpack[index + 1] == 'L' && index + 2 <= outpack.Length()) { firstrange = outpack.SubString(1,index - 1) + " "; lastrange = outpack.SubString(index + 2,outpack.Length() - (index + 1)); outpack = firstrange + lastrange; } } } } } return outpack;
-
Das schreit nach switch/case, zumindest aber nach massenhaft else if.
Und Sachen, die in jedem Fall gleich ausgeführt werden, gehören nur einmal in die Schleife, nicht in jede Verzweigung.Wenn du mal einen Original-String und das dazu passende gewünschte Ergebnis zeigst lässt sich die Stringbearbeitung selbst vielleicht auch noch optimieren.
-
Hallo
den Teil im else-Zweig kannst du aber sehr elegant zusammenfassen... die einzelnen Teile unterscheidet sich doch nur minimal voneinander
AnsiString Chars1 = "vacdsqt1rL"; AnsiString Chars2 = "|*:\\/?\"<> "; ... int pos = Chars1.Pos(outpack[index + 1]); if(pos > 0 && index + 2 <= outpack.Length()) { firstrange = outpack.SubString(1,index - 1) + Chars2[pos]; lastrange = outpack.SubString(index + 2,outpack.Length() - (index + 1)); outpack = firstrange + lastrange; } ...bis bald
akari
-
else if hört sich fürs erste gut an. Switch case funzt doch nur mit integer !? oder hab ich da was falsch verstanden?
aus MeinvNickaAUT soll Mein|Nick*AUT gemacht werden und ggf. Farbcodes ausgeschnitten werden um halt alles im Klartext zu erhalten.
Edit: Boah Akari, wie geil. Vielen Dank den ganzen Mitschreibern ! Ich bin echt erstaunt wie die leute hier mitdenken
-
switch/case funktioniert mit allen integralen Datentypen, also auch mit char (oder bool :D)
-
CStoll schrieb:
switch/case funktioniert mit allen integralen Datentypen, also auch mit char (oder bool :D)
Stimmt, hab noch mal nachgeschaut und is mir jetzt klar geworden. Danke. Wie kam ich nur auf so nen mist
...tonnen von code umschreib meine cpu atmet gleich auf
-
Öhm, jetzt weiß ich auch wie ich auf integer kam. Diese sind nur im case erlaubt
-
würde hier so was nicht schon reichen?
try { // Versuchen den Wert in einen Int zu wandeln variable = StrToInt(string); } catch(...) { Application->MessageBoxA("Fehler...","Fehler",0); }
-
Das wäre zwar auch ne Möglichkeit aber die Methode läuft nun bestens:
String __fastcall TForm1::GetClearString(String outpack) { String firstrange = "",lastrange = ""; String Chars1 = "vacdsqtlr"; String Chars2 = "|*:\\/?\"<>"; String Chars3 = "LGCJETB"; int index; for(int i = 1; i < outpack.Length();i++) { if(outpack[i] == '^' && i + 1 <= outpack.Length()) { if(StrToIntDef(outpack[i + 1],11112) != 11112) { // Zahl, also Farbcodes ausschneiden index = outpack.Pos("^"); if(index + 2 <= outpack.Length()) { firstrange = outpack.SubString(1,index - 1); lastrange = outpack.SubString(index + 2,outpack.Length() - (index + 1));//die 2 byte ausschneiden outpack = firstrange + lastrange; } } else { // keine Zahl ,string teilen und neuen string in der mitte einfügen index = outpack.Pos("^"); int pos = Chars1.Pos(outpack[index + 1]); if(pos > 0 && index + 2 <= outpack.Length()) { firstrange = outpack.SubString(1,index - 1) + Chars2[pos]; lastrange = outpack.SubString(index + 2,outpack.Length() - (index + 1)); outpack = firstrange + lastrange; } //Ländercode entfernen index = outpack.Pos("^"); pos = Chars3.Pos(outpack[index + 1]); if(pos > 0 && index + 2 <= outpack.Length()) { firstrange = outpack.SubString(1,index - 1); lastrange = outpack.SubString(index + 2,outpack.Length() - (index + 1));//die 2 byte ausschneiden outpack = firstrange + lastrange; } } } } return outpack; }