Forum > General

SOLVED Capitalize word after each space not working

(1/2) > >>

DreamVB:
Hi, I been making a little text editor and I am adding in some features to deal with converting a selection of text, I got the uppercase,lowercase and toggle-case working however I am stuck on the capitalize case, what I done so far is made it so you can select a string with a space after each word and it will uppercase the first letter after the space.

I want to add support for TABS also the code below works with tabs but if you insert a space before or after the tab is does not uppercase the next none space char can someone please have a look what I have done wrong thanks.


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---function Capitalize(const S : String) : String;var  flag : Boolean;  S1 : String;  CH : String;  X : Integer;begin   flag := True;  S1 := '';    For X := 1 to Length(S) do begin       CH := Copy(S,X,1);        if flag then       begin         if (CH <> ' ') or (CH <> #9) then begin            S1 := S1 + UpperCase(CH);            flag := False;         end         else         begin             S1 := S1 + CH;         end;       end       else       begin           S1 := S1 + Lowercase(CH);           flag := (CH = ' ') OR (ch = #9);       end;   end;    Result := S1;end;

y.ivanov:

--- Quote from: DreamVB on July 04, 2022, 12:41:20 am ---Hi, I been making a little text editor and I am adding in some features to deal with converting a selection of text, I got the uppercase,lowercase and toggle-case working however I am stuck on the capitalize case, what I done so far is made it so you can select a string with a space after each word and it will uppercase the first letter after the space.

I want to add support for TABS also the code below works with tabs but if you insert a space before or after the tab is does not uppercase the next none space char can someone please have a look what I have done wrong thanks.


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---function Capitalize(const S : String) : String;var  flag : Boolean;  S1 : String;  CH : String;  X : Integer;begin   flag := True;  S1 := '';    For X := 1 to Length(S) do begin       CH := Copy(S,X,1);        if flag then       begin         if (CH <> ' ') or (CH <> #9) then begin            S1 := S1 + UpperCase(CH);            flag := False;         end         else         begin             S1 := S1 + CH;         end;       end       else       begin           S1 := S1 + Lowercase(CH);           flag := (CH = ' ') OR (ch = #9);       end;   end;    Result := S1;end;
--- End quote ---

At line 17: the opposite of (CH = ' ') OR (ch = #9) is (CH <> ' ') and (CH <> #9)
i.e. not(A or B) = (not A) and (not B)

It is called "DeMorgan Law":
https://en.wikipedia.org/wiki/De_Morgan%27s_laws

derek.john.evans:
Have you tried StrUtils.AnsiProperCase?

https://www.freepascal.org/docs-html/rtl/strutils/ansipropercase.html

jamie:
Hmm.

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---Function CamelCaseWords(S:String):String;Var  LookingForFirstChar:Boolean = True;  I:Integer;begin  Result := S;  For I := 1 to Length(Result) do    Begin      If (LookingForFirstChar) And (Result[I]in['A'..'z'])       Then Result[I] :=UpCase(Result[I]);      LookingForFirstChar := Result[I] in [' ',#8];    end;end;                    Study that one. That should avoid UTF8 mixup and only cap Ansi.

 But be aware, there does not expand the TAB.

y.ivanov:
@jamie
The name of the function is misleading. It is not doing a camelCase.
The expression (Result[ I ]in['A'..'z']) includes non-letters, which is not fatal, but not very educational.

Navigation

[0] Message Index

[#] Next page

Go to full version