How to determine if all characters in a string are equal

后端 未结 6 961
甜味超标
甜味超标 2021-01-13 12:09

I need to know if all characters in a string are equal (formed by the same character). the function must return true or false depending if all the elements of the string ar

6条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-13 12:32

    You could consider creating an Integer value with the Element repeated 4 times (since this is AnsiChar in Delphi 7), shifted like Ord(Element) + Ord(Element) shl 8 + Ord(Element) shl 16 + Ord(Element) shl 24, then typecast the string to a PIntegerArray (^array[0..MaxInt div 4 - 1] of Integer) and loop over it Length(Str) div 4 times, comparing as integers instead of characters. You'll need to compare the last few Length(str) mod 4 characters manually though.

提交回复
热议问题