Camel case and snake case: Difference between revisions

Added Easylang
(Added 11l)
(Added Easylang)
 
(25 intermediate revisions by 17 users not shown)
Line 1:
{{draft task|Camel Case and Snake Case}}
 
Two common conventions for naming of computer program variables are '''Snake Case''' and
Line 14:
:*Show the results on changing to both snake case and camel case for each of the following strings:
 
<langsyntaxhighlight lang="java">
"snakeCase", "snake_case", "variable_10_case", "variable10Case", "ɛrgo rE tHis",
"hurry-up-joe!", "c://my-docs/happy_Flag-Day/12.doc", " spaces "
</syntaxhighlight>
</lang>
 
;Related tasks:
 
* &nbsp; [[Naming_conventions]]
 
 
 
=={{header|11l}}==
{{trans|Python}}
 
<langsyntaxhighlight lang="11l">F snakeToCamelCase(nam, sep = ‘[_]+’, lcmiddle = 0B)
‘ convert snake '_' separator case to camel case ’
I nam == ‘’
Line 80 ⟶ 77:
‘ spaces ’]
print(teststring.rjust(36)‘ => ’f(teststring))
print()</langsyntaxhighlight>
 
{{out}}
Line 267 ⟶ 264:
 
</pre>
 
=={{header|ALGOL 68}}==
Treating space, - and _ as equivalent "break" characters (as in most of the other samples) and adding kebab case (as in the Raku sample) and resisting the urge to add "space case" for languages like Algol 68 where (insignificant) spaces can appear in identifiers...
<langsyntaxhighlight lang="algol68">BEGIN # convert camel case to and from snake case #
# returns c converted to uppercase if it is lowercase, c otherwise #
OP TOUPPER = ( CHAR c )STRING:
Line 383 ⟶ 379:
print( ( 40 PAD identifier[ i ], " -> ", CAMELTOKEBAB identifier[ i ], newline ) )
OD
END</langsyntaxhighlight>
{{out}}
<pre>
Line 413 ⟶ 409:
c://my-docs/happy_Flag-Day/12.doc -> c://my-docs/happy-flag-day/12.doc
spaces -> spaces
</pre>
=={{header|Arturo}}==
 
<syntaxhighlight lang="rebol">camelToSnake: function [str][
i: new 0
result: new ""
while [i < size str][
ch: str\[i]
if? upper? ch [
'result ++ `_` ++ lower str\[i]
]
else [
'result ++ str\[i]
]
 
inc 'i
]
return result
]
 
snakeToCamel: function [str][
i: new 0
result: new ""
while [i < size str][
ch: str\[i]
if? and? [ch=`_`][(i+1) < size str] [
'result ++ upper str\[i+1]
inc 'i
]
else [
'result ++ str\[i]
]
 
inc 'i
]
return result
]
 
tests: ["snakeCase", "snake_case", "variable_10_case", "variable10Case", "ɛrgo rE tHis",
"hurry-up-joe!", "c://my-docs/happy_Flag-Day/12.doc", " spaces "]
 
loop tests 'test [
print [pad test 35 "=> camel:" snakeToCamel test "snake:" camelToSnake test]
]</syntaxhighlight>
 
{{out}}
 
<pre> snakeCase => camel: snakeCase snake: snake_case
snake_case => camel: snakeCase snake: snake_case
variable_10_case => camel: variable10Case snake: variable_10_case
variable10Case => camel: variable10Case snake: variable10_case
ɛrgo rE tHis => camel: ɛrgo rE tHis snake: ɛrgo r_e t_his
hurry-up-joe! => camel: hurry-up-joe! snake: hurry-up-joe!
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>
 
=={{header|C++}}==
The output of this example reflects the author's interpretation of the task.
<syntaxhighlight lang="c++">
 
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <string>
#include <vector>
 
const char HYPHEN = '-';
const char SPACE = ' ';
const char UNDERSCORE = '_';
const std::string WHITESPACE = " \n\r\t\f\v";
 
std::string left_trim(const std::string& text) {
size_t start = text.find_first_not_of(WHITESPACE);
return ( start == std::string::npos ) ? "" : text.substr(start);
}
 
std::string right_trim(const std::string& text) {
size_t end = text.find_last_not_of(WHITESPACE);
return ( end == std::string::npos ) ? "" : text.substr(0, end + 1);
}
 
std::string trim(const std::string& text) {
return left_trim(right_trim(text));
}
 
void prepare_for_conversion(std::string& text) {
text = trim(text);
std::replace(text.begin(), text.end(), SPACE, UNDERSCORE);
std::replace(text.begin(), text.end(), HYPHEN, UNDERSCORE);
}
 
std::string to_snake_case(std::string& camel) {
prepare_for_conversion(camel);
std::string snake = "";
bool first = true;
for ( const char& ch : camel ) {
if ( first ) {
snake += ch;
first = false;
} else if ( ! first && ch >= 'A' && ch <= 'Z' ) {
if ( snake[snake.length() - 1] == UNDERSCORE ) {
snake += tolower(ch);
} else {
snake += UNDERSCORE;
snake += tolower(ch);
}
} else {
snake += ch;
}
}
return snake;
}
 
std::string to_camel_case(std::string& snake) {
prepare_for_conversion(snake);
std::string camel = "";
bool underscore = false;
for ( const char& ch : snake ) {
if ( ch == UNDERSCORE ) {
underscore = true;
} else if ( underscore ) {
camel += toupper(ch);
underscore = false;
} else {
camel += ch;
}
}
return camel;
}
 
int main() {
const std::vector<std::string> variable_names = { "snakeCase", "snake_case", "variable_10_case",
"variable10Case", "ergo rE tHis", "hurry-up-joe!", "c://my-docs/happy_Flag-Day/12.doc", " spaces " };
 
std::cout << std::setw(48) << "=== To snake_case ===" << std::endl;
for ( std::string text : variable_names ) {
std::cout << std::setw(34) << text << " --> " << to_snake_case(text) << std::endl;
}
 
std::cout << std::endl;
std::cout << std::setw(48) << "=== To camelCase ===" << std::endl;
for ( std::string text : variable_names ) {
std::cout << std::setw(34) << text << " --> " << to_camel_case(text) << std::endl;
}
}
</syntaxhighlight>
{{ out }}
<pre>
=== To snake_case ===
snakeCase --> snake_case
snake_case --> snake_case
variable_10_case --> variable_10_case
variable10Case --> variable10_case
ergo rE tHis --> ergo_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
 
=== To camelCase ===
snakeCase --> snakeCase
snake_case --> snakeCase
variable_10_case --> variable10Case
variable10Case --> variable10Case
ergo rE tHis --> ergoRETHis
hurry-up-joe! --> hurryUpJoe!
c://my-docs/happy_Flag-Day/12.doc --> c://myDocs/happyFlagDay/12.doc
spaces --> 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|EasyLang}}==
{{trans|Nim}}
<syntaxhighlight>
func$ strip s$ .
a = 1
while substr s$ a 1 = " "
a += 1
.
b = len s$
while substr s$ b 1 = " "
b -= 1
.
return substr s$ a (b - a + 1)
.
func$ toupper c$ .
c = strcode c$
if c >= 97 and c <= 122
c$ = strchar (c - 32)
.
return c$
.
func$ tolower c$ .
c = strcode c$
if c >= 65 and c <= 90
c$ = strchar (c + 32)
.
return c$
.
func isupper c$ .
c = strcode c$
if c >= 65 and c <= 90
return 1
.
.
delim$ = "_- "
func$ snakecase s$ .
s$ = strip s$
for c$ in strchars s$
if isupper c$ = 1 and prev$ <> ""
if strpos delim$ prev$ = 0
r$ &= "_"
.
r$ &= tolower c$
else
r$ &= c$
.
prev$ = c$
.
return r$
.
func$ camelcase s$ .
s$ = strip s$
prev$ = "x"
for c$ in strchars s$
if strpos delim$ prev$ <> 0
r$ &= toupper c$
elif strpos delim$ c$ = 0
r$ &= c$
.
prev$ = c$
.
return r$
.
test$[] = [ "snakeCase" "snake_case" "variable_10_case" "variable10Case" "ɛrgo rE tHis" "hurry-up-joe!" "c://my-docs/happy_Flag-Day/12.doc" " spaces " ]
print "=== To snake_case ==="
for s$ in test$[]
print s$ & " -> " & snakecase s$
.
print "\n=== To camelCase ==="
for s$ in test$[]
print s$ & " -> " & camelcase s$
.
</syntaxhighlight>
{{out}}
<pre>
=== To snake_case ===
snakeCase -> snake_case
snake_case -> snake_case
variable_10_case -> variable_10_case
variable10Case -> variable10_case
ɛrgo rE tHis -> ɛrgo 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
 
=== To camelCase ===
snakeCase -> snakeCase
snake_case -> snakeCase
variable_10_case -> variable10Case
variable10Case -> variable10Case
ɛrgo rE tHis -> ɛrgoRETHis
hurry-up-joe! -> hurryUpJoe!
c://my-docs/happy_Flag-Day/12.doc -> c://myDocs/happyFlagDay/12.doc
spaces -> spaces
</pre>
 
Line 418 ⟶ 780:
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.
{{works with|Factor|0.99 2021-06-02}}
<langsyntaxhighlight lang="factor">USING: formatting kernel math regexp sequences splitting
splitting.extras unicode ;
 
Line 450 ⟶ 812:
"c://my-docs/happy_Flag-Day/12.doc" " spaces "
" internal space "
} [ test ] each</langsyntaxhighlight>
{{out}}
<pre>
Line 471 ⟶ 833:
" internal space " >snake " internal_space "
" internal space " >camel " internalSpace "
</pre>
 
=={{header|FreeBASIC}}==
<syntaxhighlight lang="vb">Function isDigit(ch As String) As Boolean
Return ch >= "0" And ch <= "9"
End Function
 
Function to_snake_case(s As String) As String
Dim As Integer l = Len(s)
Dim As String snake = Trim(s), tmp = snake
For i As Integer = 1 To Len(snake)
If isDigit(Mid(snake, i, 1)) Then
Continue For
Elseif Instr(Mid(snake, i, 1), " ") Then
Mid(snake, i) = "_"
Continue For
Elseif Instr(Mid(snake, i, 1), Any "\:/-_!.") Then
Continue For
Elseif Mid(snake, i, 1) = Ucase(Mid(snake, i, 1)) Then
tmp = Lcase(Mid(snake, i,1))
Mid(snake, i) = "_"
snake = Left(snake, i) & tmp & Mid(snake, i+1)
End If
Next i
Return snake
End Function
 
Function toCamelCase(s As String) As String
Dim As Integer l = Len(s)
Dim As String camel = Trim(s), tmp = camel
For i As Integer = 1 To Len(camel)
If Instr(Mid(camel, i, 1), Any ":/!.") Then
Continue For
Elseif Instr(Mid(camel, i, 1), Any " _-") Then
camel = Left(camel, i-1) & Ucase(Mid(camel, i+1,1)) & Mid(camel, i+2)
End If
Next i
Return camel
End Function
 
Dim Shared tests(1 To ...) As String*33 => {_
"snakeCase", "snake_case", "variable_10_case", "variable10Case", _
"\u025brgo rE tHis", "ergo rE tHis", "hurry-up-joe!", _
"c://my-docs/happy_Flag-Day/12.doc", " spaces "}
 
Sub test0(title As String, fn As String)
Print title
For i As Integer = 1 To Ubound(tests)
Dim As String texto = tests(i) & " ===> " & to_snake_case(tests(i))
Locate i+1, 41 - Len(texto) / 2 : Print texto
Next i
End Sub
 
Sub test1(title As String, fn As String)
Print title
For i As Integer = 1 To Ubound(tests)
Dim As String texto = tests(i) & " ===> " & toCamelCase(tests(i))
Locate i+12, 41 - Len(texto) / 2 : Print texto
Next i
End Sub
 
test0 "to_snake_case:", "to_snake_case"
Print
test1 "toCamelCase:", "toCamelCase"
 
Sleep</syntaxhighlight>
 
=={{header|FutureBasic}}==
<syntaxhighlight lang="futurebasic">
 
include "NSLog.incl"
local fn snake_toCamel( s as CFStringRef ) as CFStringRef
long r,f
s = fn StringByTrimmingCharactersInSet( s, fn CFCharacterSetGetPredefined(_kCFCharacterSetWhitespace ))
for r = 1 to len(s)-2
f = instr(0, @"_- ", mid(s, r, 1))
if f <> NSNotFound
s = fn stringwithformat(@"%@%@%@", left( s, r ), ucase(mid(s,r+1,1)), mid(s,r+2))
end if
next
end fn = s
 
local fn CamelTo_snake( s as CFStringRef ) as CFStringRef
long r,f
s = fn StringByTrimmingCharactersInSet( s, fn CFCharacterSetGetPredefined(_kCFCharacterSetWhitespace ))
s = fn StringByReplacingOccurrencesOfString( s, @" ", @"_")
s = fn StringByReplacingOccurrencesOfString( s, @"-", @"_")
for r = 1 to len(s)-2
f = instr(0, @"ABCDEFGHIJKLMNOPQRSTUVWXYZ", mid(s, r, 1))
if f <> NSNotFound
if fn StringIsEqual(@"_", mid(s, r-1, 1)) then continue
s = fn stringwithformat(@"%@%@%@", left( s, r ), @"_", mid(s,r))
end if
next
s = fn stringwithformat(@"%@%@",left( s, 1), lcase(mid(s, 1)))
end fn = s
 
local fn show( s1 as CFStringRef )
CFStringRef s = @" "
CFStringRef s2 = fn snake_toCamel( s1 )
CFStringRef s3 = fn CamelTo_snake( s1 )
nslog(@" \"%@\"%@\"%@\"%@\"%@\"", s1, mid(s, len(s1)), s2, mid(s, len(s2)), s3)
end fn
 
nslog( @"%@",@"String ¬
fn snake_toCamel ¬
fn CamelTo_snake")
fn show(@"snakeCase")
fn show(@"snake_case")
fn show(@"variable_10_case")
fn show(@"variable10Case")
fn show(@"ɛrgo rE tHis")
fn show(@"hurry-up-joe!")
fn show(@"c://my-docs/happy_Flag-Day/12.doc")
fn show(@" spaces ")
 
handleevents
</syntaxhighlight>
{{out}}
<syntaxhighlight lang="futurebasic">
String fn snake_toCamel fn CamelTo_snake
"snakeCase" "snakeCase" "snake_case"
"snake_case" "snakeCase" "snake_case"
"variable_10_case" "variable10Case" "variable_10_case"
"variable10Case" "variable10Case" "variable10_case"
"ɛrgo rE tHis" "ɛrgoRETHis" "ɛrgo_r_e_t_his"
"hurry-up-joe!" "hurryUpJoe!" "hurry_up_joe!"
" spaces " "spaces" "spaces"
"c://my-docs/happy_Flag-Day/12.doc" "c://myDocs/happyFlagDay/12.doc" "c://my_docs/happy_flag_day/12.doc"
</syntaxhighlight>
=={{header|Java}}==
The output of this example reflects the authors interpretation of the task.
<syntaxhighlight lang="java">
 
import java.util.List;
 
public final class CamelCaseAndSnakeCase {
 
public static void main(String[] aArgs) {
List<String> variableNames = List.of( "snakeCase", "snake_case", "variable_10_case", "variable10Case",
"ergo rE tHis", "hurry-up-joe!", "c://my-docs/happy_Flag-Day/12.doc", " spaces ");
System.out.println(String.format("%48s", "=== To snake_case ==="));
for ( String text : variableNames ) {
System.out.println(String.format("%34s%s%s", text, " --> ", toSnakeCase(text)));
}
 
System.out.println();
System.out.println(String.format("%48s", "=== To camelCase ==="));
for ( String text : variableNames ) {
System.out.println(String.format("%34s%s%s", text, " --> ", toCamelCase(text)));
}
}
private static String toSnakeCase(String aCamel) {
aCamel = aCamel.trim().replace(SPACE, UNDERSCORE).replace(HYPHEN, UNDERSCORE);
StringBuilder snake = new StringBuilder();
boolean first = true;
for ( char ch : aCamel.toCharArray() ) {
if ( first ) {
snake.append(ch);
first = false;
} else if ( ! first && Character.isUpperCase(ch) ) {
if ( snake.toString().endsWith(UNDERSCORE) ) {
snake.append(Character.toLowerCase(ch));
} else {
snake.append(UNDERSCORE + Character.toLowerCase(ch));
}
} else {
snake.append(ch);
}
}
return snake.toString();
}
private static String toCamelCase(String aSnake) {
aSnake = aSnake.trim().replace(SPACE, UNDERSCORE).replace(HYPHEN, UNDERSCORE);
StringBuilder camel = new StringBuilder();
boolean underscore = false;
for ( char ch : aSnake.toCharArray() ) {
if ( Character.toString(ch).equals(UNDERSCORE) ) {
underscore = true;
} else if ( underscore ) {
camel.append(Character.toUpperCase(ch));
underscore = false;
} else {
camel.append(ch);
}
}
return camel.toString();
}
private static final String SPACE = " ";
private static final String UNDERSCORE = "_";
private static final String HYPHEN = "-";
}
</syntaxhighlight>
{{ out }}
<pre>
=== To snake_case ===
snakeCase --> snake_case
snake_case --> snake_case
variable_10_case --> variable_10_case
variable10Case --> variable10_case
ergo rE tHis --> ergo_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
 
=== To camelCase ===
snakeCase --> snakeCase
snake_case --> snakeCase
variable_10_case --> variable10Case
variable10Case --> variable10Case
ergo rE tHis --> ergoRETHis
hurry-up-joe! --> hurryUpJoe!
c://my-docs/happy_Flag-Day/12.doc --> c://myDocs/happyFlagDay/12.doc
spaces --> spaces
</pre>
 
Line 479 ⟶ 1,063:
 
'''Preliminaries'''
<langsyntaxhighlight lang="jq">def isUpper: explode[0] | 65 <= . and . <= 90;
 
def lpad($len): tostring | ($len - length) as $l | (" " * $l)[:$l] + .;
 
# "White space is not permitted as part of camel case or snake case variable names."
def trim: sub("^\\s+";"") | sub("\\s+$";"");</langsyntaxhighlight>
'''Camel and Snake'''
<langsyntaxhighlight lang="jq">def toCamel:
trim as $snake
| { camel: "", underscore : false}
Line 521 ⟶ 1,105:
"",
" === toCamelCase ===",
(tests[] | "\(lpad(33)) -> \(toCamel)")</langsyntaxhighlight>
{{out}}
<pre>
Line 544 ⟶ 1,128:
spaces -> spaces
</pre>
 
 
=={{header|Julia}}==
<langsyntaxhighlight lang="julia">#=
Regex based variable name convention change string functions.
`sep` is the separator targeted for change from (to camel case) or to (to snake case)
Line 614 ⟶ 1,196:
println()
end
</langsyntaxhighlight>{{out}}
<pre>
Testing function snakeToCamelCase:
Line 797 ⟶ 1,379:
c://my-docs/happy_Flag-Day/12.doc => c://my/docs/happy//flag//day/12/doc
spaces => spaces
</pre>
 
=={{header|Kotlin}}==
{{trans|Scala}}
<syntaxhighlight lang="Kotlin">
fun main() {
val variableNames = listOf("snakeCase", "snake_case", "variable_10_case", "variable10Case",
"ergo rE tHis", "hurry-up-joe!", "c://my-docs/happy_Flag-Day/12.doc", " spaces ")
 
println(" ".repeat(26) + "=== To snake_case ===")
variableNames.forEach { text ->
println("${text.padStart(34)} --> ${toSnakeCase(text)}")
}
 
println("\n" + " ".repeat(26) + "=== To camelCase ===")
variableNames.forEach { text ->
println("${text.padStart(34)} --> ${toCamelCase(text)}")
}
}
 
fun toSnakeCase(camel: String): String {
val snake = StringBuilder()
camel.trim().replace(" ", "_").replace("-", "_").forEach { ch ->
if (snake.isEmpty() || snake.last() != '_' || ch != '_') {
if (ch.isUpperCase() && snake.isNotEmpty() && snake.last() != '_') snake.append('_')
snake.append(ch.toLowerCase())
}
}
return snake.toString()
}
 
fun toCamelCase(snake: String): String {
val camel = StringBuilder()
var underscore = false
snake.trim().replace(" ", "_").replace("-", "_").forEach { ch ->
if (ch == '_') {
underscore = true
} else if (underscore) {
camel.append(ch.toUpperCase())
underscore = false
} else {
camel.append(ch)
}
}
return camel.toString()
}
</syntaxhighlight>
{{out}}
<pre>
=== To snake_case ===
snakeCase --> snake_case
snake_case --> snake_case
variable_10_case --> variable_10_case
variable10Case --> variable10_case
ergo rE tHis --> ergo_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
 
=== To camelCase ===
snakeCase --> snakeCase
snake_case --> snakeCase
variable_10_case --> variable10Case
variable10Case --> variable10Case
ergo rE tHis --> ergoRETHis
hurry-up-joe! --> hurryUpJoe!
c://my-docs/happy_Flag-Day/12.doc --> c://myDocs/happyFlagDay/12.doc
spaces --> spaces
 
</pre>
 
 
=={{header|Lambdatalk}}==
<syntaxhighlight lang="scheme">
{def snake2camel
{lambda {:w}
{S.replace (?:_|-)(\w)
by {span {@ style="text-transform:uppercase;"}$1}
in :w}}}
-> snake2camel
 
{def camel2snake
{lambda {:w}
{S.replace ([A-Z])
by _{span {@ style="text-transform:lowercase;"}$1}
in :w}}}
-> camel2snake
 
{S.map snake2camel
snake_case
snake-case
my_brave_new_world
my_brave-new_world
... and_so_on
}
-> snakeCase snakeCase myBraveNewWorld myBraveNewWorld ... andSoOn
 
{S.map camel2snake
snakeCase
myBraveNewWorld
... andSoOn
}
-> snake_case my_brave_new_world ... and_so_on
</syntaxhighlight>
=={{header|Lua}}==
 
{{works with|Lua|5.1 and later}}
 
Since the task is a bit vague this solution also has its own interpretation of several details.
 
We have the two functions toCamelCase() and fromCamelCase() that perform the conversion back and forth between camel case and non-camel case, including snake case. The test strings are assumed to potentially contain multiple names. Any character that isn't part of the matched names (including leading and trailing whitespace) is ignored/left alone. Numbers aren't mentioned in the task but the "variable_10_case"/"variable10Case" strings imply that numbers should be treated as their own words.
 
<syntaxhighlight lang="lua">local function escapeForPattern(str)
return (str:gsub("[-+*^?$.%%()[%]]", "%%%0"))
end
 
local function toCamelCase(str, separator)
local escapedSeparator = escapeForPattern(separator)
local namePattern = "%l%w*"..escapedSeparator.."[%w"..escapedSeparator.."]*%w" -- Starting with a lower case character and containing the separator character.
local separatorPattern = escapedSeparator.."(%w)" -- Discard the separator and capture the alphanumeric character.
 
return (str:gsub(namePattern, function(name)
return (name:gsub(separatorPattern, string.upper)) -- The captured character will be the one argument for string.upper().
end))
end
 
local function fromCamelCase(str, separator)
local namePattern = "%l+[%u%d]%w*" -- Starting with a lower case character and containing an upper case character or digit.
local separatorPattern1 = "(%l)([%u%d])" -- Lower case character followed by upper case character or digit.
local separatorPattern2 = "(%d)(%a)" -- Digit followed by alphanumeric character.
 
return (str:gsub(namePattern, function(name)
return (name
:gsub(separatorPattern1, function(char1,char2) return char1..separator..char2:lower() end)
:gsub(separatorPattern2, function(char1,char2) return char1..separator..char2:lower() end)
)
end))
end</syntaxhighlight>
 
'''Tests:'''
 
<syntaxhighlight lang="lua">local function utf8Length(str)
local len = 0
for char in str:gmatch"[\1-\127\194-\244][\128-\191]*" do
len = len + 1
end
return len
end
 
local tests = {
"snakeCase", "snake_case",
"variable_10_case", "variable10Case",
"ɛrgo rE tHis",
"hurry-up-joe!",
"c://my-docs/happy_Flag-Day/12.doc",
" spaces ",
" internal space ",
}
 
local function convert(label, converter)
print(label..":")
for _, str in ipairs(tests) do
print((" "):rep(35-utf8Length(str)) .. str .. " => " .. converter(str))
end
print()
end
 
convert("snake_case to camelCase", function(str) return toCamelCase (str, "_") end)
convert("space case to camelCase", function(str) return toCamelCase (str, " ") end)
convert("kebab-case to camelCase", function(str) return toCamelCase (str, "-") end)
convert("camelCase to snake_case", function(str) return fromCamelCase(str, "_") end)</syntaxhighlight>
 
{{out}}
 
<pre>
snake_case to camelCase:
snakeCase => snakeCase
snake_case => snakeCase
variable_10_case => variable10Case
variable10Case => variable10Case
ɛrgo rE tHis => ɛrgo rE tHis
hurry-up-joe! => hurry-up-joe!
c://my-docs/happy_Flag-Day/12.doc => c://my-docs/happyFlag-Day/12.doc
spaces => spaces
internal space => internal space
 
space case to camelCase:
snakeCase => snakeCase
snake_case => snake_case
variable_10_case => variable_10_case
variable10Case => variable10Case
ɛrgo rE tHis => ɛrgoRETHis
hurry-up-joe! => hurry-up-joe!
c://my-docs/happy_Flag-Day/12.doc => c://my-docs/happy_Flag-Day/12.doc
spaces => spaces
internal space => internalSpace
 
kebab-case to camelCase:
snakeCase => snakeCase
snake_case => snake_case
variable_10_case => variable_10_case
variable10Case => variable10Case
ɛrgo rE tHis => ɛrgo rE tHis
hurry-up-joe! => hurryUpJoe!
c://my-docs/happy_Flag-Day/12.doc => c://myDocs/happy_FlagDay/12.doc
spaces => spaces
internal space => internal space
 
camelCase to snake_case:
snakeCase => snake_case
snake_case => snake_case
variable_10_case => variable_10_case
variable10Case => variable_10_case
ɛrgo rE tHis => ɛrgo 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
internal space => internal space
</pre>
 
'''Notes:'''
 
* We don't check if names have preceding alphanumeric characters (e.g. "10kN" will have the name "kN").
* We don't handle consecutive upper case characters (e.g. "newUIBox").
* We don't handle non-ASCII characters because of Lua's limited support for them ("ɛ" just happen to work here).
 
=={{header|Nim}}==
We use the same rules as those used by Wren's solution.
<syntaxhighlight lang=Nim>import std/[strformat, strutils, unicode]
 
const Delimiters = [Rune('_'), Rune('-'), Rune(' ')]
 
func toCamelCase(s: string): string =
let s = s.strip(chars = Whitespace)
var prev = Rune(0)
for rune in s.runes:
if prev in Delimiters:
result.add rune.toUpper
elif rune notin Delimiters:
result.add rune
prev = rune
 
func toSnakeCase(s: string): string =
var s= s.strip(chars = Whitespace).replace(' ', '_')
var idx = 0
var prev = Rune(0)
for rune in s.runes:
if rune.isUpper and idx > 0:
if prev notin Delimiters:
result.add '_'
result.add rune.toLower
else:
result.add rune
prev = rune
inc idx
 
const Strings = ["snakeCase", "snake_case", "variable_10_case",
"variable10Case", "ɛrgo rE tHis", "hurry-up-joe!",
"c://my-docs/happy_Flag-Day/12.doc", " spaces "]
 
echo center("### To snake case ###", 69)
for s in Strings:
echo &"{s:>33} → {s.toSnakeCase}"
 
echo()
echo center("### To camel case ###", 69)
for s in Strings:
echo &"{s:>33} → {s.toCamelCase}"
</syntaxhighlight>
 
{{out}}
<pre> ### To snake case ###
snakeCase → snake_case
snake_case → snake_case
variable_10_case → variable_10_case
variable10Case → variable10_case
ɛrgo rE tHis → ɛrgo_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
 
### To camel case ###
snakeCase → snakeCase
snake_case → snakeCase
variable_10_case → variable10Case
variable10Case → variable10Case
ɛrgo rE tHis → ɛrgoRETHis
hurry-up-joe! → hurryUpJoe!
c://my-docs/happy_Flag-Day/12.doc → c://myDocs/happyFlagDay/12.doc
spaces → spaces
</pre>
 
=={{header|Perl}}==
<langsyntaxhighlight lang="perl">#!/usr/bin/perl
 
use strict; # https://rosettacode.org/wiki/Camel_case_and_snake_case
Line 830 ⟶ 1,702:
{
printf "%35s -> %s\n", $word, tocamel($word);
}</langsyntaxhighlight>
{{out}}
<pre>
Line 855 ⟶ 1,727:
spaces -> spaces
</pre>
 
=={{header|Phix}}==
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">to_snake_case</span><span style="color: #0000FF;">(</span><span style="color: #004080;">string</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">)</span>
Line 889 ⟶ 1,760:
<span style="color: #000000;">test</span><span style="color: #0000FF;">(</span><span style="color: #008000;">" === to_snake_case ===\n"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">to_snake_case</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">test</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"\n === toCamelCase ===\n"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">toCamelCase</span><span style="color: #0000FF;">)</span>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 912 ⟶ 1,783:
spaces ===> spaces
</pre>
 
=={{header|Python}}==
<langsyntaxhighlight lang="python">""" https://rosettacode.org/wiki/Camel_case_and_snake_case """
 
import re
Line 1,007 ⟶ 1,877:
print(teststring.rjust(36), " => ", f(teststring))
print()
</langsyntaxhighlight>{{out}}
<pre>
Testing function <function snakeToCamelCase at 0x000001F17C25AC10>:
Line 1,190 ⟶ 2,060:
c://my-docs/happy_Flag-Day/12.doc => c:/my/docs/happy//flag//day/12/doc
spaces => spaces
</pre>
=={{header|Quackery}}==
 
<syntaxhighlight lang="quackery"> [ $ "_ -" find 3 < ] is separator ( c --> b )
 
[ dup lower != ] is capital ( c --> b )
 
[ 2 times [ reverse trim ]
$ "" false rot
witheach
[ dup separator iff
[ 2drop true ]
else
[ over if upper
swap dip join
drop false ] ]
drop ] is camelise ( $ --> $ )
 
[ camelise
$ "" swap
witheach
[ dup capital if
[ dip
[ char _ join ] ]
lower join ] ] is snakify ( $ --> $ )</syntaxhighlight>
 
{{out}}
Testing in the shell.
<pre>/O> ' [ $ "snakeCase"
... $ "snake_case"
... $ "variable_10_case"
... $ "variable10Case"
... $ "ergo rE tHis"
... $ "hurry-up-joe!"
... $ "c://my-docs/happy_Flag-Day/12.doc"
... $ " spaces " ]
... dup
... say "To camelCase:" cr
... witheach [ do camelise say " " echo$ cr ]
... cr
... say "To snake_case:" cr
... witheach [ do snakify say " " echo$ cr ]
...
To camelCase:
snakeCase
snakeCase
variable10Case
variable10Case
ergoRETHis
hurryUpJoe!
c://myDocs/happyFlagDay/12.doc
spaces
 
To snake_case:
snake_case
snake_case
variable10_case
variable10_case
ergo_r_e_t_his
hurry_up_joe!
c://my_docs/happy_flag_day/12.doc
spaces
 
Stack empty.</pre>
 
=={{header|Racket}}==
<syntaxhighlight lang="racket">
#lang racket
 
(define input '("snakeCase" "snake_case" "variable_10_case" "variable10Case" "ɛrgo rE tHis" "hurry-up-joe!" "c://my-docs/happy_Flag-Day/12.doc" " spaces "))
 
;; make '-' the canonical separator by replacing '_' and ' ' with '-'
(define (dashify s)
(regexp-replace* #px"[_ ]" s "-"))
 
;; replace -X with -x for any upper-case X
(define (dash-upper->dash-lower s)
(regexp-replace* #px"-[[:upper:]]" s string-downcase))
 
;; replace X with -x for any upper-case X
(define (upper->dash-lower s)
(regexp-replace* #px"[[:upper:]]"
s
(λ (s) (string-append "-" (string-downcase s)))))
 
(define (string-kebabcase s)
(upper->dash-lower (dash-upper->dash-lower (dashify s))))
 
(define (string-snakecase s)
;; once we have kebabcase, snakecase is easy, just change '-' to '_'
(regexp-replace* #px"-" (string-kebabcase s) "_"))
 
(define (string-camelcase s)
;; camel is pretty easy, too - replace dash-anything with uppercase-anything
;; note: this will change non-letters as well, so -10 becomes just 10
(regexp-replace* #px"-." (string-kebabcase s) (λ (s) (string-upcase (substring s 1 2)))))
 
(define (convert-case to-case case-name namelist)
(printf "Conversions to ~a:~n" case-name)
(for ([name namelist])
(printf "'~a' --> '~a'~n" name (to-case (string-trim name))))
(printf "~n"))
 
(convert-case string-kebabcase "kebab-case" input)
(convert-case string-snakecase "snake_case" input)
(convert-case string-camelcase "camelCase" input)
</syntaxhighlight>
{{out}}
<pre>
Conversions to kebab-case:
'snakeCase' --> 'snake-case'
'snake_case' --> 'snake-case'
'variable_10_case' --> 'variable-10-case'
'variable10Case' --> 'variable10-case'
'ɛrgo rE tHis' --> 'ɛrgo-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'
 
Conversions to snake_case:
'snakeCase' --> 'snake_case'
'snake_case' --> 'snake_case'
'variable_10_case' --> 'variable_10_case'
'variable10Case' --> 'variable10_case'
'ɛrgo rE tHis' --> 'ɛrgo_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'
 
Conversions to camelCase:
'snakeCase' --> 'snakeCase'
'snake_case' --> 'snakeCase'
'variable_10_case' --> 'variable10Case'
'variable10Case' --> 'variable10Case'
'ɛrgo rE tHis' --> 'ɛrgoRETHis'
'hurry-up-joe!' --> 'hurryUpJoe!'
'c://my-docs/happy_Flag-Day/12.doc' --> 'c://myDocs/happyFlagDay/12.doc'
' spaces ' --> 'spaces'
</pre>
 
Line 1,195 ⟶ 2,203:
The specs are a little vague, but taking a wild stab at it... ''(May be completely wrong but without any examples of expected output it is hard to judge. This is what '''I''' would expect at least...) ''
 
<syntaxhighlight lang="raku" perl6line>my @tests = qww<
snakeCase snake_case variable_10_case variable10Case "ɛrgo rE tHis"
hurry-up-joe! c://my-docs/happy_Flag-Day/12.doc " spaces "
Line 1,227 ⟶ 2,235:
printf "%33s ==> %s\n", $_, .&toCamelCase for @tests;
say "\n{' ' x 30}to-kabab-case";
printf "%33s ==> %s\n", $_, .&to-kebab-case for @tests;</langsyntaxhighlight>
{{out}}
<pre> to_snake_case
Line 1,258 ⟶ 2,266:
c://my-docs/happy_Flag-Day/12.doc ==> c://my-docs/happy_Flag-Day/12.doc
spaces ==> spaces</pre>
 
 
=={{header|RPL}}==
« 1
'''WHILE''' DUP2 DUP SUB " " == '''REPEAT''' 1 + '''END'''
OVER DUP SIZE
'''WHILE''' DUP2 DUP SUB " " == '''REPEAT''' 1 - '''END'''
SWAP DROP SUB
» '<span style="color:blue">TRIM</span>' STO
« <span style="color:blue">TRIM</span> → s
« ""
1 s SIZE '''FOR''' j
s j DUP SUB
'''CASE'''
"- " OVER POS '''THEN'''
DROP "_" '''END'''
DUP "A" ≥ OVER "Z" ≤ AND '''THEN'''
NUM 32 + CHR
'''IF''' OVER DUP SIZE DUP SUB "_" ≠ '''THEN''' "_" SWAP + '''END'''
'''END'''
'''END'''
+
'''NEXT'''
» » '<span style="color:blue">→SNAKE</span>' STO
« <span style="color:blue">TRIM</span> → s
« "" 1 CF
1 s SIZE '''FOR''' j
s j DUP SUB
'''CASE'''
"-_ " OVER POS '''THEN'''
DROP "" 1 SF '''END'''
DUP "a" ≥ OVER "z" ≤ AND 1 FS?C AND '''THEN'''
NUM 32 - CHR '''END'''
'''END'''
+
'''NEXT'''
» » '<span style="color:blue">→CAMEL</span>' STO
« { "snakeCase" "snake_case" "variable_10_case" "variable10Case"
"εrgo rE tHis" "hurry-up-joe!" "c://my-docs/happy_Flag-Day/12.doc" " spaces " }
DUP 1 « <span style="color:blue">→SNAKE</span> » DOLIST
SWAP 1 « <span style="color:blue">→CAMEL</span> » DOLIST
» '<span style="color:blue">TASK</span>' STO
{{out}}
<pre>
2: { "snake_case" "snake_case" "variable_10_case" "variable10_case" "εrgo_r_e_t_his" "hurry_up_joe!" "c://my_docs/happy_flag_day/12.doc" "spaces" }
1: { "snakeCase" "snakeCase" "variable10Case" "variable10Case" "εrgoRETHis" "hurryUpJoe!" "c://myDocs/happyFlagDay/12.doc" "spaces" }
</pre>
 
=={{header|Scala}}==
{{trans|Java}}
<syntaxhighlight lang="Scala">
object CamelCaseAndSnakeCase extends App {
 
val variableNames = List("snakeCase", "snake_case", "variable_10_case", "variable10Case",
"ergo rE tHis", "hurry-up-joe!", "c://my-docs/happy_Flag-Day/12.doc", " spaces ")
 
println(" " * 26 + "=== To snake_case ===")
variableNames.foreach { text =>
println(f"$text%34s --> ${toSnakeCase(text)}")
}
 
println("\n" + " " * 26 + "=== To camelCase ===")
variableNames.foreach { text =>
println(f"$text%34s --> ${toCamelCase(text)}")
}
 
def toSnakeCase(camel: String): String = {
val snake = new StringBuilder
camel.trim.replace(" ", "_").replace("-", "_").foreach { ch =>
if (snake.isEmpty || snake.last != '_' || ch != '_') {
if (ch.isUpper && snake.nonEmpty && snake.last != '_') snake.append('_')
snake.append(ch.toLower)
}
}
snake.toString
}
 
def toCamelCase(snake: String): String = {
val camel = new StringBuilder
var underscore = false
snake.trim.replace(" ", "_").replace("-", "_").foreach { ch =>
if (ch == '_') underscore = true
else if (underscore) {
camel.append(ch.toUpper)
underscore = false
} else camel.append(ch)
}
camel.toString
}
}
</syntaxhighlight>
{{out}}
<pre>
=== To snake_case ===
snakeCase --> snake_case
snake_case --> snake_case
variable_10_case --> variable_10_case
variable10Case --> variable10_case
ergo rE tHis --> ergo_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
 
=== To camelCase ===
snakeCase --> snakeCase
snake_case --> snakeCase
variable_10_case --> variable10Case
variable10Case --> variable10Case
ergo rE tHis --> ergoRETHis
hurry-up-joe! --> hurryUpJoe!
c://my-docs/happy_Flag-Day/12.doc --> c://myDocs/happyFlagDay/12.doc
spaces --> spaces
 
</pre>
 
=={{header|V (Vlang)}}==
{{trans|go}}
<syntaxhighlight lang="v (vlang)">fn to_camel(snake string) string {
mut camel := ''
mut underscore := false
letters := snake.trim(' ').runes()
for c in letters {
if c.str() in [' ','-','_'] {
underscore = true
} else if underscore {
camel += c.str().to_upper()
underscore = false
} else {
camel += c.str()
}
}
return camel
}
fn to_snake(camel string) string {
mut snake := ''
mut first := true
letters := camel.trim(' ').replace(' ','_').runes()
for c in letters {
if first {
snake+=c.str()
first = false
} else if !first && (c.str().is_upper() && c.bytes().len ==1 && c.bytes()[0].is_letter()) {
if snake[snake.len-1..snake.len] == '_' || snake[snake.len-1..snake.len] == '-' {
snake += c.str().to_lower()
}else {
snake += '_'+c.str().to_lower()
}
}else{
snake+=c.str()
}
}
return snake
}
const tests = ["snakeCase", "snake_case", "variable_10_case", "variable10Case", "ɛrgo rE tHis",
"hurry-up-joe!", "c://my-docs/happy_Flag-Day/12.doc", " spaces "]
fn main() {
println(' === to_snake_case ===')
for word in tests {
println('${word:33} -> ${to_snake(word)}')
}
println(' === to_camel_case ===')
for word in tests {
println('${word:33} -> ${to_camel(word)}')
}
}</syntaxhighlight>
{{out}}
<pre>Same as Go entry</pre>
 
=={{header|Wren}}==
Line 1,267 ⟶ 2,445:
 
2. I've assumed that an underscore should not be added if the previous character was already a separator.
<langsyntaxhighlight ecmascriptlang="wren">import "./str" for Char
import "./fmt" for Fmt
 
var toCamel = Fn.new { |snake|
Line 1,321 ⟶ 2,499:
for (snake in tests) {
Fmt.print("$33s -> $s", snake, toCamel.call(snake))
}</langsyntaxhighlight>
 
{{out}}
Line 1,347 ⟶ 2,525:
 
=={{header|XPL0}}==
<langsyntaxhighlight XPL0lang="xpl0">string 0; \use zero-terminated strings
char Out(100); \output string (made global for safety)
 
Line 1,412 ⟶ 2,590:
CrLf(0);
];
]</langsyntaxhighlight>
 
{{out}}
1,978

edits