Camel case and snake case: Difference between revisions

Content added Content deleted
(Added Racket solution)
Line 464: Line 464:
c://my-docs/happy_Flag-Day/12.doc => camel: c://my-docs/happyFlag-Day/12.doc snake: c://my-docs/happy__flag-_day/12.doc
c://my-docs/happy_Flag-Day/12.doc => camel: c://my-docs/happyFlag-Day/12.doc snake: c://my-docs/happy__flag-_day/12.doc
spaces => camel: spaces snake: spaces</pre>
spaces => camel: spaces snake: spaces</pre>
=={{header|Delphi}}==
{{works with|Delphi|6.0}}
{{libheader|SysUtils, StdCtrls}}
Makes use of Delphi "sets" to help parse strings.


<syntaxhighlight lang="Delphi">
const TestStrings: array [0..7] of string = (
'snakeCase', 'snake_case', 'variable_10_case', 'variable10Case',
'\u025brgo rE tHis', 'hurry-up-joe!', 'c://my-docs/happy_Flag-Day/12.doc',
' spaces ');


function MakeCamelCase(S: string): string;
{Convert string to camel-case}
var I: integer;
var Toggle: boolean;
begin
S:=Trim(S);
Result:='';
for I:=1 to Length(S) do
if Toggle then
begin
Result:=Result+UpperCase(S[I]);
Toggle:=False;
end
else if S[I] in [' ','_','-'] then Toggle:=True
else Result:=Result+S[I];
end;


function MakeSnakeCase(S: string): string;
{Convert string to snake-case}
var I: integer;
var Toggle: boolean;
begin
S:=Trim(S);
Result:='';
for I:=1 to Length(S) do
if S[I] in [' ','-'] then Result:=Result+'_'
else if S[I] in ['A'..'Z'] then
begin
Result:=Result+'_';
Result:=Result+LowerCase(S[I]);
end
else Result:=Result+S[I];
end;

procedure ConvertCamelSnake(SA: array of string; Memo: TMemo);
var I: integer;
var S: string;

function FormatStrs(S1,S2: string): string;
begin
Result:=Format('%35s',[S1])+' '+Format('%-35s',[S2]);
end;

begin
Memo.Lines.Add('Snake Case: ');
for I:=0 to High(SA) do
begin
S:=FormatStrs(SA[I],MakeSnakeCase(SA[I]));
Memo.Lines.Add(S);
end;
Memo.Lines.Add('Camel Case: ');
for I:=0 to High(SA) do
begin
S:=FormatStrs(SA[I],MakeCamelCase(SA[I]));
Memo.Lines.Add(S);
end;
end;

procedure CamelSnakeTest(Memo: TMemo);
{Test camel/snake conversion routines}
begin
ConvertCamelSnake(TestStrings,Memo);
end;

</syntaxhighlight>
{{out}}
<pre>
Snake Case:
snakeCase snake_case
snake_case snake_case
variable_10_case variable_10_case
variable10Case variable10_case
\u025brgo rE tHis \u025brgo_r_e_t_his
hurry-up-joe! hurry_up_joe!
c://my-docs/happy_Flag-Day/12.doc c://my_docs/happy__flag__day/12.doc
spaces spaces
Camel Case:
snakeCase snakeCase
snake_case snakeCase
variable_10_case variable10Case
variable10Case variable10Case
\u025brgo rE tHis \u025brgoRETHis
hurry-up-joe! hurryUpJoe!
c://my-docs/happy_Flag-Day/12.doc c://myDocs/happyFlagDay/12.doc
spaces spaces
</pre>



=={{header|Factor}}==
=={{header|Factor}}==
In my interpretation of the task, leading/trailing whitespace should be ignored, not trimmed. And non-leading/trailing whitespace should be dealt with the same way as underscores and hyphens. Although the task says nothing about numbers, I chose to treat letter->number and number->letter transitions the same way as lower->upper for the sake of converting to snake case.
In my interpretation of the task, leading/trailing whitespace should be ignored, not trimmed. And non-leading/trailing whitespace should be dealt with the same way as underscores and hyphens. Although the task says nothing about numbers, I chose to treat letter->number and number->letter transitions the same way as lower->upper for the sake of converting to snake case.
Line 521: Line 624:
" internal space " >camel " internalSpace "
" internal space " >camel " internalSpace "
</pre>
</pre>

=={{header|jq}}==
=={{header|jq}}==
'''Adapted from [[#Wren|Wren]]'''
'''Adapted from [[#Wren|Wren]]'''