Autor Beitrag
Ebil
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 131



BeitragVerfasst: Sa 07.06.08 17:47 
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
        
for I2 := 1 to length(name) do
        begin
          case ord(char(name[I])) of
          48..57: inc(right); //zahlen
          65..90: inc(right); //kleinbuchstaben
          97..122: inc(right); //großbuchstaben
          10: inc(right); //leerzeichen
          end;
        end;
        if right = length(name) then loot.add(name);


Ich will einzelne stellen eines strings prüfen, ob sie wirklich zahlen, oder buchstaben sind.
Alle anderen zeichen kann ich nicht gebrauchen.
Mir ist nichts besseres eingefallen wie man das lösen könnte ( denke da gibt es was...), aber so wie ich es hab funktioniert es nicht.
Die strings sehen zb so aus 'Blabla²' , also html codierungen drin , das würde 'Blabla²' ergeben, so einen string will ich nicht in meine liste aufnehmen, wäre es nur dieses eine sonderzeichen währe es ja kein problem, aber es gibt ja unmengen...
Also wie soll ich es machen? :/

Gruß
Ebil

Moderiert von user profile iconChristian S.: Quote- durch Delphi-Tags ersetzt
mkinzler
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 4106
Erhaltene Danke: 13


Delphi 2010 Pro; Delphi.Prism 2011 pro
BeitragVerfasst: Sa 07.06.08 20:07 
ausblenden Delphi-Quelltext
1:
  case ord(name[I]) of					

_________________
Markus Kinzler.
Ebil Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 131



BeitragVerfasst: Sa 07.06.08 20:23 
Ändert aber nichts am ergebnis :/
Sitzt schon ganze zeit dran.
Gibts keine andere lösung dafür das ein string nur buchstaben und zahlen haben darf?
Gausi
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 8554
Erhaltene Danke: 481

Windows 7, Windows 10
D7 PE, Delphi XE3 Prof, Delphi 10.3 CE
BeitragVerfasst: Sa 07.06.08 20:24 
Hast du right mit 0 initialisiert?

Edit: Und ist
ausblenden Delphi-Quelltext
1:
2:
3:
for I2 := 1 to length(name) do 
        begin 
          case ord(char(name[I])) of
nur ein Copy&Paste-Fehler? ;-)

_________________
We are, we were and will not be.
Yogu
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 2598
Erhaltene Danke: 156

Ubuntu 13.04, Win 7
C# (VS 2013)
BeitragVerfasst: Sa 07.06.08 20:29 
Hallo,

user profile iconEbil hat folgendes geschrieben:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
        
for I2 := 1 to length(name) do
        begin
          case ord(char(name[I])) of
          48..57: inc(right); //zahlen
          65..90: inc(right); //kleinbuchstaben
          97..122: inc(right); //großbuchstaben
          10: inc(right); //leerzeichen
          end;
        end;
        if right = length(name) then loot.add(name);

Vielleicht solltest du lieber gleiche Variablennamen nehmen... ;)

Edit: user profile iconGausi, wer war schneller? :mrgreen:

Warum schreibst du nicht einfach die Zeichen aus? Dann hast du kein Gefunzel mit ASCII-Codes. Und außerdem kann einzelne Einträge durch Kommas abtrennen, wenn die Aktion sowieso die gleiche ist.

ausblenden Delphi-Quelltext
1:
2:
3:
case Name[i] of
  '0'..'9', 'a'..'z', 'A'..'Z', ' ': inc(right);
end;

PS: Der ASCII-Code vom Leerzeichen ist nicht 10, sondern 32. 10 ist der Tab :)

Grüße,
Yogu
GTA-Place
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
EE-Regisseur
Beiträge: 5248
Erhaltene Danke: 2

WIN XP, IE 7, FF 2.0
Delphi 7, Lazarus
BeitragVerfasst: Sa 07.06.08 21:22 
Und warum dann überhaupt noch case of? :mrgreen:

ausblenden Delphi-Quelltext
1:
if Name[I] in ['A'..'Z', 'a'..'z', '0'..'9'] then					

Eventuell noch ein Name := AnsiLowerCase(Name); vorher, dann fällt die Überprüfung auf 'A'..'Z' weg.

_________________
"Wer Ego-Shooter Killerspiele nennt, muss konsequenterweise jeden Horrorstreifen als Killerfilm bezeichnen." (Zeit.de)
Ebil Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 131



BeitragVerfasst: Sa 07.06.08 22:00 
Oha danke... das "in" kannt ich noch garnich,und das I2 war richtig, und ja ich habs bei dem einen vergessen und mit I verwechselt...

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
  
right := 0      
for I2 := 1 to length(name) do
        begin
        if name[I2] in ['A'..'Z', 'a'..'z', '0'..'9'] then inc(right);
        end;
        if right = length(name) then loot.add(name);
        right := 0;


ne bessere lösung um nicht 10 ma ein eintrag zu machen als inc(right) gibt es nicht?
Bzw gibt es überhaupt noch bessere lösungen?
Man will ja immer dazu lernen...


//Edit: Und lowercase ist doof, brauch groß/kleinschreibung ^^.
Gruß
Ebil
Hidden
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 2242
Erhaltene Danke: 55

Win10
VS Code, Delphi 2010 Prof.
BeitragVerfasst: Sa 07.06.08 22:23 
Hi,

hmm. du kannst den Text ja einmal lowercase casten und ansonsten an der ursprünglichen Version arbeiten. Kommt aber auf den Zusammenhang an, was günstiger ist.

mfG,

_________________
Centaur spears can block many spells, but no one tries to block if they see that the spell is a certain shade of green. For this purpose it is useful to know some green stunning hexes. (HPMoR)
bflegel
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 193
Erhaltene Danke: 1

Win XP, Win 7, BS2000
D5
BeitragVerfasst: Sa 07.06.08 23:18 
Hallo,

wäre dies nicht effizienter:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
var
ok : boolean;

...

ok := true;
for I2 := 1 to length(name) do
  if not name[I2] in ['A'..'Z', 'a'..'z', '0'..'9'] then ok := false;
if ok then loot.add(name);


So würde die ok-Variable nur dann auf false gesetzt wenn ein nicht gültiges Zeichen im String wäre.

bye
bflegel

_________________
I know all the jokes about my name
Hidden
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 2242
Erhaltene Danke: 55

Win10
VS Code, Delphi 2010 Prof.
BeitragVerfasst: So 08.06.08 00:06 
Hi,

Dann wird aber unter umständen ein unnötig langer String vollständig durchsucht..

ausblenden Also eher:
1:
2:
3:
4:
5:
Fin := Length(S);
I2 := 1;
while (I2 < Fin) and OK do begin
  ...
end;


Indem man "OK := true" um "Exit;" ergänzt, könnte man, denke ich, nochmal Laufzeit spaaren.

mfG,

_________________
Centaur spears can block many spells, but no one tries to block if they see that the spell is a certain shade of green. For this purpose it is useful to know some green stunning hexes. (HPMoR)
bflegel
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 193
Erhaltene Danke: 1

Win XP, Win 7, BS2000
D5
BeitragVerfasst: So 08.06.08 00:16 
user profile iconHidden hat folgendes geschrieben:
Indem man "OK := true" um "Exit;" ergänzt, könnte man, denke ich, nochmal Laufzeit spaaren.


Hallo hidden,

wenn dann doch wohl eher "break". Mit "Exit" verlässt Du die ganze Prozedur, mit "break" nur die For-Schleife. Evtl möchte er aber in dieser Prozedur unabhängig vom Ergebnis dieser Überprüfung noch etwas anderes ausführen.

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
var
ok : boolean;

begin
...
ok := true;
for I2 := 1 to length(name) do
  if not name[I2] in ['A'..'Z', 'a'..'z', '0'..'9'] then 
    begin
      ok := false;
      break;
    end;
if ok then loot.add(name);
...
end;


bye
bflegel

_________________
I know all the jokes about my name
Hidden
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 2242
Erhaltene Danke: 55

Win10
VS Code, Delphi 2010 Prof.
BeitragVerfasst: So 08.06.08 00:33 
user profile iconbflegel hat folgendes geschrieben:
wenn dann doch wohl eher "break". Mit "Exit" verlässt Du die ganze Prozedur, mit "break" nur die For-Schleife. Evtl möchte er aber in dieser Prozedur unabhängig vom Ergebnis dieser Überprüfung noch etwas anderes ausführen.
:oops: Freud'scher ;)

_________________
Centaur spears can block many spells, but no one tries to block if they see that the spell is a certain shade of green. For this purpose it is useful to know some green stunning hexes. (HPMoR)
Ebil Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 131



BeitragVerfasst: So 08.06.08 00:42 
Der komplette string wird gecheckt bevor er eingetragen wird, bei deiner lösung würde der string auch eingetragen werden wenn nur das letzte zeichen des strings ein gülstiges wäre ( da die var. ok nach jedem buchstaben ihren wert setzt, und nicht nachdem alles durchgelaufen ist).

Gruß
Ebil
bflegel
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 193
Erhaltene Danke: 1

Win XP, Win 7, BS2000
D5
BeitragVerfasst: So 08.06.08 01:08 
Hallo Ebil,

ich habe den Code zwar nicht getestet, aber er müsste schon stimmen:

- Zuerst wird die ok-Variable auf true gesetzt.
- Dann wird ein Zeichen nach dem anderen überprüft.
- Sollte das überprüfte Zeichen ein ungültiges sein, wird die ok-Variable auf false gesetzt und die weitere Überprüfung durch das break abgebrochen.
- Als letztes wird überprüft ob die ok-Variable noch auf true ist; wenn ja -> Eintrag des Strings.

bye
bflegel

//Edit:

Habe den Code jetzt getestet. Es muss nur eine Korrektur vorgenommen werden:

ausblenden Delphi-Quelltext
1:
if not (name[I2] in ['A'..'Z', 'a'..'z', '0'..'9']) then					

_________________
I know all the jokes about my name
Ebil Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 131



BeitragVerfasst: So 08.06.08 02:52 
nice, nun funktionierts!