![]() |
Position eines Chars in einem Array of Chars finden
Hallo,
gibt es eine Möglichkeit, ein Char in einem Array of Chars zu finden und so die Position des Chars rauszufinden, ohne dies in einer Schleife zu erledigen: Meine Schleife schaut so aus:
Delphi-Quellcode:
const
Chars: array[1..10] of Char = ('ä', 'ö', 'ü', 'Ä', 'Ö', 'Ü', 'ß', '²', '³', 'µ'); var j: Integer; C_Char: Char; begin C_Char := 'Ä'; for j := Low(Chars) to High(Chars) do If Chars[j] = C_Char then begin ShowMessage(IntToStr(j)); //<--- Diese Zeile ist nur für diesen Thread drin break; end; Ich hätt gern so was in der Art:
Delphi-Quellcode:
Gibts so was?
ShowMessage(IntToStr(Array_Pos(C_Char, Chars)));
|
Re: Position eines Chars in einem Array of Chars finden
Zitat:
![]() Aber warum schreibst du dir die Finger bei der Defintion wund?
Delphi-Quellcode:
Gruß Hawkeye
const
Chars = 'äöüÄÖÜß²³µ'; |
Re: Position eines Chars in einem Array of Chars finden
Zitat:
Zitat:
Zitat:
|
Re: Position eines Chars in einem Array of Chars finden
Zitat:
Zitat:
![]() Gruß Hawkeye |
Re: Position eines Chars in einem Array of Chars finden
Zitat:
Ich hatte das mit Pos natürlich vor dem Schreiben dieses Threads probiert. da erhielt ich eine Fehlermeldung. Auf deine Frage jetzt hin, habs ich nochmals probiert - und es funktioniert:
Delphi-Quellcode:
Danke Hawkeye - man lernt nie aus
Pos(C_Char, Chars)
|
Re: Position eines Chars in einem Array of Chars finden
Zitat:
Was hingegen effizient wäre, wäre ein Bitvektor (32 Bytes), in dem du das dann überprüfst:
Delphi-Quellcode:
Zuweisung mehrerer Chars geht ganz bequem mit "myCharBitVector.SetCharacters('asdf');".
type
TCharBitVector=class private FBytes: array[0..31] of Byte; public function GetInVector(Character: Char): Boolean; procedure SetInVector(Character: Char; InVector: Boolean); procedure SetCharactersInVector(str: String); property Characters[Character: Char]: Boolean read GetInVector write SetInVector; default; end; implementation procedure CalcByteAndBitIndex(const Character: Char; var ByteIndex, BitIndex: Byte); asm movzx eax, al; push BitIndex; mov cl, 8; div cl; pop BitIndex; mov [ByteIndex], al; mov [BitIndex], ah; end; function TCharBitVector.GetInVector(Character: Char): Boolean; var ByteIndex, BitIndex: Byte; begin CalcByteAndBitIndex(Character, ByteIndex, BitIndex); Result:=Boolean((FBytes[ByteIndex] shr BitIndex) and 1); end; procedure TCharBitVector.SetInVector(Character: Char; InVector: Boolean); var ByteIndex, BitIndex: Byte; begin CalcByteAndBitIndex(Character, ByteIndex, BitIndex); if InVector then FBytes[ByteIndex]:=FBytes[ByteIndex] or (1 shl BitIndex) else FBytes[ByteIndex]:=FBytes[ByteIndex] and not (1 shl BitIndex); end; procedure TCharBitVector.SetCharactersInVector(str: String); var I: Integer; begin for I:=0 to 7 do FBytes[I]:=0; for I:=1 to length(str) do SetInVector(str[I], True); end; Nachsehen, ob ein Char drin ist mit "myCharBitVector['A]'" Setzen eines einzigen Chars mit "myCharBitVector['A']:=True/False;" Wenn du das nicht willst, kannst du die Chars wenigstens sortieren und dann eine binäre Suche durchführen. |
Alle Zeitangaben in WEZ +1. Es ist jetzt 22:27 Uhr. |
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024-2025 by Thomas Breitkreuz