There is a line where there are no spaces, for example: ИвановПетрСидорович

It is necessary to find the position of the letters in upper case to separate the words.

    3 answers 3

    I most likely will not give the decision code, because I have not practiced Delphi for a long time ...

    But the general course of the algorithm is as follows:

    You sort through the string.

    Then at each iteration of the cycle, you analyze the letter on the register. I do not remember if there is a special function in Delphi for this. If you have, then you use this function, and if not, then you write your own.

    If you look at the ASCII table, you can see that the big letters are in one code range, and the flips are in another.

    UPD

    The comments suggest that there are specials. functions

    myStr [i], AnsiUpperCase (compare myStr [i] with AnsiUpperCase (myStr [i])), Copy, Delete (the latter - may not be used, depends on the implementation). In the latest versions of Delphi, you can use other functions, but these are in any version.

    • one
      Then add: myStr [i], AnsiUpperCase (compare myStr [i] with AnsiUpperCase (myStr [i])), Copy, Delete (the latter may not be used, depends on the implementation). In the latest versions of Delphi, you can use other functions, but these are in any version. - kami

    There are several ways to detect a capital letter in a line. But they strongly depend on which version of Delphi you are using and whether you need Unicode support.

    In modern versions of Delphi, simply use the TCharacter.IsUpper function from the Character unit:

     uses System.Character; ... function SplitByUpper(const AStr: string): string; var I: Integer; begin Result := ''; for I := 1 to Length(AStr) do begin if TCharacter.IsUpper(AStr[I]) and (Result <> '') then begin Result := Result + ' '; end; Result := Result + AStr[I]; end; end; 

    The code is illustrative and can be optimized!

    In old Delphi, you can use the IsCharUpper function (works with the current user locale) from WinApi or generate a list with information about each character for a particular locale yourself, using GetStringTypeEx . Generating a list will be justified if you need to use this function very intensively and you run into speed.

    If you need portable code between different versions of Delphi, then you can take the JCL, there is such a function:

     function CharIsUpper(const C: Char): Boolean; begin {$IFDEF UNICODE_RTL_DATABASE} Result := TCharacter.IsUpper(C); {$ELSE ~UNICODE_RTL_DATABASE} Result := (StrCharTypes[C] and C1_UPPER) <> 0; {$ENDIF ~UNICODE_RTL_DATABASE} end; 

    The same question in the English version of the SO:

      Try this:

       const UpperChars = 'АБВГДЕЁЖЗИЙКЛМНОПРСТУФХЦЧШЩЬЪЫЭЮЯ';' var Idx : integer; begin ResStr := ''; for i := 1 to length(InStr) do begin Idx := Pos(InStr[i]), UpperChars); // узнаем верхний ли регистр у символа if (Idx > 0) and (i > 1) then // значит нужно вставить пробел ResStr := ResStr + ' '; ResStr := ResStr + InStr[i] // добавляем символ end; 

      PS Well, you namudri ...