[SOLVED] How to insert spaces into CamelCase string to separate the words? lazarus / 21-General ---------------------------------------------------------------------- ToolGIS · 2023-04-18 16:08 I need to convert the string "AddPointCoordinatesToTable" to "Add Point Coordinates To Table". AnyHelpAppreciated. ---------------------------------------------------------------------- Eugene Loza · 2023-04-18 16:17 I can't quickly find a procedure to determine if the character is upper or lower case, but that should kinda work for ASCII-only symbols: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i var  S: String;  SLower: String;  SOut: String;  I: Integer;begin  S := 'AddPointCoordinatesToTable';  SLower := LowerCase(S);  SOut := '';  for I := 0 to Length(S)-1 do    if S[I] = SLower[I] then      SOut += S[I]   else      SOut += ' ' + S[I];  WriteLn(SOut);end; (Pseudocode, it might need some fixes) ---------------------------------------------------------------------- KodeZwerg · 2023-04-18 16:21 My try (works only for ASCII characters) Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function AddSpaces(const AInput: string): string;var  i: Integer;begin  Result := AInput;  if (Length(AInput) > 0) then    begin      Result := '';    for i := 1 to Length(AInput) do        if ((Ord(AInput[i]) in [41..90]) and (i > 1)) then          Result := Result + ' ' + AInput[i]          else          Result := Result + AInput[i];    end;end; ---------------------------------------------------------------------- alpine · 2023-04-18 16:25 Is it containing only ASCII characters? Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function Fun(S: String): String;var  I: Integer;begin  Result := Copy(S, 1, 1);  for I := 2 to Length(S) do  begin    if S[I] in ['A'..'Z'] then    Result := Result + ' ';    Result := Result + S[I];  end;end; ---------------------------------------------------------------------- Josh · 2023-04-18 16:46 Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function Ad dCharToCamelCaseString(AString,ACharsToAdd:string;APrecede,AIgnoreFirs t:Boolean):string;var i:integer;    ok:boolean;begin  Result:='';  If AString='' then exit;  ok:=Not AIgnoreFirst;  For I:=1 to length(AString) Do  Begin    If AString[i]=UpCase(AString[I]) then    begin      if ok then      begin        if APrecede then Result:=result+ACharsToAdd+AString[i] else result:=result+AString[i]+ACharsToAdd      End      Else Result:=Result+AString[i];      ok:=true;    end    Else Result:=Result+AString[i];  End;End;           Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i memo1.Lines .Add(AddCharToCamelCaseString('ThisIsATest','_',true,true));  memo1.Li nes.Add(AddCharToCamelCaseString('ThisIsATest','_',true,false));  memo 1.Lines.Add(AddCharToCamelCaseString('ThisIsATest','_',false,true));  memo1.Lines.Add(AddCharToCamelCaseString('ThisIsATest','_',false,false )); QuoteMemo1 This_Is_A_Test _This_Is_A_Test ThisI_sA_T_est T_hisI_sA_T_est ---------------------------------------------------------------------- ToolGIS · 2023-04-18 19:16 Thanks for the replies folks. I was hoping there was a string utility that makes it simple, to avoid having to write a custom script. Looks like there's no such luck. Will post a final function once I'm happy with it. ---------------------------------------------------------------------- MarkMLl · 2023-04-18 19:49 Scripts don't come into it, this is /programming/ :-) Seriously though: there really are limits as to what one can fairly add into libraries or class libraries: I can't speak for everybody around here, but I prefer the idea of having to work out a five-line algorithm to having to remember thousands of pre-written procedures and functions. Two things I would add to what everybody else has said. First, operations like this can often be handled more efficiently by iterating right-to-left than left-to-right. Second, indexing through a string can be problematic if non-ASCII characters are involved, since the default representation is UTF-8 which might have more than one byte per codepoint. MarkMLl ---------------------------------------------------------------------- alpine · 2023-04-18 20:37 Quote from: ToolGIS on April 18, 2023, 09:16:28 pmThanks for the replies folks. I was hoping there was a string utility that makes it simple, to avoid having to write a custom script. Looks like there's no such luck. Will post a final function once I'm happy with it. Can't imagine something simpler than that and also I don't see, into the four answers given to you, what can make you unhappy with ... with a small indexing error of Eugene Loza, but still showing the general idea ... you'd better try to study the small differences between them and gain some benefit from the answers of the pretty simple question you asked. ---------------------------------------------------------------------- Zvoni · 2023-04-18 20:58 Since lowercase has an unicode-overload: Wouldn’t it be enough to do a check like If lowercase(mystring[ i])<>mystring[ i] to catch capital letters? And then continue with one of the algorithms above? EDIT: just saw that Josh basically had the same idea. Sorry ---------------------------------------------------------------------- ToolGIS · 2023-04-19 09:40 Quote from: alpine on April 18, 2023, 10:37:54 pmI don't see, into the four answers given to you, what can make you unhappy with . Where did I indicate I was unhappy? There were a few solutions posted and I need review them and come up with a good fit for my needs. Which is kinda exactly what you just said. Quote from: alpine on April 18, 2023, 10:37:54 pmCan't imagine something simpler than that Clearly not simple enough to have an out of the box string utility. And if it's that simple, then please share your genius solution with us. ---------------------------------------------------------------------- ToolGIS · 2023-04-19 09:48 Quote from: MarkMLl on April 18, 2023, 09:49:53 pmI can't speak for everybody around here, but I prefer the idea of having to work out a five-line algorithm to having to remember thousands of pre-written procedures and functions. I honestly couldn't think of anything worse than having to write a split function every time I need it.  Even if I did have to, I'd write it once and use it again forever. But that's also exactly what a built in function is anyway, only someone else wrote it and it's always there in the main library. ---------------------------------------------------------------------- ToolGIS · 2023-04-19 09:55 Quote from: alpine on April 18, 2023, 06:25:57 pmIs it containing only ASCII characters? At this point yes, but UTF translations may be required in future for i18n. However that can be done on the corrected phrase, so it's not a problem. ---------------------------------------------------------------------- Zvoni · 2023-04-19 09:59 Quote from: ToolGIS on April 19, 2023, 11:48:09 amI honestly couldn't think of anything worse than having to write a split function every time I need it.  Even if I did have to, I'd write it once and use it again forever. But that's also exactly what a built in function is anyway, only someone else wrote it and it's always there in the main library. I think you're confusing "Use-cases that are needed often" vs. "edge- case use-cases" Honestly? Before your post i couldn't even imagine a need for such a function like you described Quote from: MarkMLl on April 18, 2023, 09:49:53 pmoperations like this can often be handled more efficiently by iterating right-to-left than left-to-right. MarkMLl The age old "i don't have to keep an eye on the count, since i've already passed the position i modified"? Agreed! ---------------------------------------------------------------------- wp · 2023-04-19 10:05 The solutions presented so far always extract characters in the text to be converted by means of an index. This will not work with UTF8 where a "character" may extend over up to 4 bytes. The following code works with UTF8-encoded text (not heavily tested, though): Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i uses  LazUTF8, LazUnicode; function CamelCaseToSpaces(s: String): String;var  P: PChar;  j: Integer;  ch: String;  upperCh: String;begin  SetLength(Result, Length(s) * 2 + 1);  FillChar(Result[1], Length(s)*2, 0);  P := @Result[1];  for ch in s do  begin    upperCh := UTF8Uppercase(ch);    if ch = upperCh then    begin      P^ := ' ';      inc(P);    end;    Move(ch[1], P^, Length(ch));    inc(P, Length(ch));  end;  SetLength(Result, StrLen(PChar(Result)));end; ---------------------------------------------------------------------- MarkMLl · 2023-04-19 10:05 Quote from: Zvoni on April 19, 2023, 11:59:39 amI think you're confusing "Use-cases that are needed often" vs. "edge-case use-cases" Honestly? Before your post i couldn't even imagine a need for such a function like you described I'm pretty sure I've had to do this, or at least something close to it, in the past... but I really can't remember the context. However as a topical example: break a camelcase function name into words, and ask one of the "AI" engines what it thinks of the resulting phrase. Or alternatively, break it into words and then use less imaginative pattern matching to process "how do I..." type questions. Or for that matter build a permuted index. MarkMLl ---------------------------------------------------------------------- Josh · 2023-04-19 11:27 would following work Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function Ad dCharToCamelCaseString(AString,ACharsToAdd:string;APrecede,AIgnoreFirs t:Boolean):string;var i:integer;    ok:boolean;    ch:String;begin  Result:='';  If AString='' then exit;  ok:=Not AIgnoreFirst;  For I:=1 to utf8length(AString) Do  Begin    ch:=UTF8Copy(AString,i,1);    If ch=UTF8UpperCase(ch) then    begin      if ok then      begin        if APrecede then Result:=result+ACharsToAdd+ch        else result:=result+ch+ACharsToAdd      End      Else Result:=Result+ch;    ok:=true;    end    Else Result:=Result+ch;  End;End;                     Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i memo1.Lines .Add(AddCharToCamelCaseString('Ⲙ𝙉০Ρ𝗤ThisIsATest','_',t rue,true));// note the utf8 chars in the string are being converted by the forum......   memo1.Lines.Add(AddCharToCamelCaseString('ThisIsATes t','_',true,false));  memo1.Lines.Add(AddCharToCamelCaseString('ThisIs ATest','_',false,true));  memo1.Lines.Add(AddCharToCamelCaseString('Th isIsATest','_',false,false));   QuoteⲘ_𝙉_০_Ρ_𝗤_This_Is_A_Test _This_Is_A_Test ThisI_sA_T_est T_hisI_sA_T_est ---------------------------------------------------------------------- wp · 2023-04-19 12:19 Quote from: Josh on April 19, 2023, 01:27:22 pmwould following work Yes. But it is ineffective since with every UTF8Copy the code has to iterate over the entire string from its beginning to find the codepoint at index i. Juha's iterator "for ch in s" in unit LazUnicode, on the other hand, continues extraction of the next codepoint at the position where the previous one had ended. ---------------------------------------------------------------------- KodeZwerg · 2023-04-19 20:27 Quote from: wp on April 19, 2023, 12:05:03 pm(not heavily tested, though)Thanks for your Unicode example (you've forgot to exclude adding a leading space), corrected in a hurry to Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function CamelCaseToSpaces(const s: String): String;var  P: PChar;  j: Integer;  ch: String;  upperCh: String;begin  Result := '';  SetLength(Result, Length(s) * 2 + 1);  FillChar(Result[1], Length(s)*2, 0);  P := @Result[1];  j := 1;  for ch in s do  begin    upperCh := UTF8Uppercase(ch);    if ((j <> 1) and (ch = upperCh)) then    begin      P^ := ' ';      inc(P);    end;    Move(ch[1], P^, Length(ch));    inc(P, Length(ch));    Inc(j);  end;  SetLength(Result, StrLen(PChar(Result)));end; ---------------------------------------------------------------------- alpine · 2023-04-20 07:17 My UTF8 version: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i uses  LazUTF8, LazUnicode, StrUtils; function CamelCaseToSpaces(const S: String): String;begin  with TCodePointEnumerator.Create(S) do    try    MoveNext;      Result := Current;      while MoveNext do        Result := Result + IfThen(Current = UTF8UpperCase(Current), ' ') + Current;    finally      Free;    end;end; ---------------------------------------------------------------------- ToolGIS · 2023-05-01 10:08 Quote from: alpine on April 20, 2023, 09:17:17 amMy UTF8 version: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i uses  LazUTF8, LazUnicode, StrUtils; function CamelCaseToSpaces(const S: String): String;begin  with TCodePointEnumerator.Create(S) do    try    MoveNext;      Result := Current;      while MoveNext do        Result := Result + IfThen(Current = UTF8UpperCase(Current), ' ') + Current;    finally      Free;    end;end; This is basically what I went with, but an improvement would need to handle consecutive uppercase letters. Eg "IWorkForTheCIA" gets expanded out to "I Work For The C I A". But these can be arbitrary as the "I_Work" space is necessary but the "C_I_A" spaces are not. One could make a basic rule that any 3 or more consecutive uppercase letters (adjoining the next word to the right) are a word, and the algorithm would need to work backwards.