stringcase

package module
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 26, 2024 License: MIT Imports: 1 Imported by: 1

README

stringcase Go Reference CI Status MIT License

This library provides some functions that convert string cases between camelCase, COBOL-CASE, kebab-case, MACRO_CASE, PascalCase, snake_case and Train-Case.

Basically, these functions targets the upper and lower cases of only ASCII alphabets for capitalization, and all characters except ASCII alphabets and ASCII numbers are eliminated as word separators.

To limit characters using as separators, the functions named like *_with_sep are provided, and to keep specified characters, the functions named like *_with_keep are provided.

Install

Write the following import declaration, then run go mod tidy.

import "github.com/sttk/stringcase"

Usage

This functions contained in this library are excuted as follows:

func main() {
    input := "foo-bar-baz"
    camel := stringcase.CamelCase(input)
    fmt.Printf("%s\n", camel)
    // => "fooBarBaz"
}

Supporting Go versions

This library supports Go 1.18 or later.

Actual test results for each Go version:
% gvm-fav
Now using version go1.18.10
go version go1.18.10 darwin/amd64
ok  	github.com/sttk/stringcase	0.235s	coverage: 100.0% of statements

Now using version go1.19.13
go version go1.19.13 darwin/amd64
ok  	github.com/sttk/stringcase	0.237s	coverage: 100.0% of statements

Now using version go1.20.14
go version go1.20.14 darwin/amd64
ok  	github.com/sttk/stringcase	0.185s	coverage: 100.0% of statements

Now using version go1.21.7
go version go1.21.7 darwin/amd64
ok  	github.com/sttk/stringcase	0.189s	coverage: 100.0% of statements

Now using version go1.22
go version go1.22.0 darwin/amd64
ok  	github.com/sttk/stringcase	0.192s	coverage: 100.0% of statements

Back to go1.22
Now using version go1.22

License

Copyright (C) 2024 Takayuki Sato

This program is free software under MIT License.
See the file LICENSE in this distribution for more details.

Documentation

Overview

This library provides some functions that convert string cases between camelCase, COBOL-CASE, kebab-case, MACRO_CASE, PascalCase, snake_case and Train-Case.

Basically, these functions targets the upper and lower cases of only ASCII alphabets for capitalization, and all characters except ASCII alphabets and ASCII numbers are eliminated as word separators.

To limit characters using as separators, the functions named like *_with_sep are provided, and to keep specified characters, the functions named like *_with_keep are provided.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func CamelCase

func CamelCase(input string) string

Converts a string to camel case.

This function takes a string as its argument, then returns a string of which the case style is camel case.

This function targets only the upper and lower cases of ASCII alphabets for capitalization, and all characters except ASII alphabets and ASII numbers are eliminated as word separators.

Example
package main

import (
	"fmt"

	"github.com/sttk/stringcase"
)

func main() {
	camel := stringcase.CamelCase("foo_bar_baz")
	fmt.Printf("camel = %s\n", camel)
}
Output:
camel = fooBarBaz

func CamelCaseWithKeep

func CamelCaseWithKeep(input, keeped string) string

Converts a string to camel case using characters other than the specified characters as separators.

This function takes a string as its argument, then returns a string of which the case style is camel case.

This function targets only the upper and lower cases of ASCII alphabets for capitalization, and the characters other than the specified characters as the second argument of this function are regarded as word separators and are eliminated.

Example
package main

import (
	"fmt"

	"github.com/sttk/stringcase"
)

func main() {
	camel := stringcase.CamelCaseWithKeep("foo-bar100%baz", "%")
	fmt.Printf("camel = %s\n", camel)
}
Output:
camel = fooBar100%Baz

func CamelCaseWithSep

func CamelCaseWithSep(input, seps string) string

Converts a string to camel case using the specified characters as separators.

This function takes a string as its argument, then returns a string of which the case style is camel case.

This function targets only the upper and lower cases of ASCII alphabets for capitalization, and the characters specified as the second argument of this function are regarded as word separators and are eliminated.

Example
package main

import (
	"fmt"

	"github.com/sttk/stringcase"
)

func main() {
	camel := stringcase.CamelCaseWithSep("foo-bar100%baz", "- ")
	fmt.Printf("camel = %s\n", camel)
}
Output:
camel = fooBar100%Baz

func CobolCase

func CobolCase(input string) string

Converts a string to cobol case.

This function takes a string as its argument, then returns a string of which the case style is cobol case.

This function targets the upper and lower cases of ASCII alphabets for capitalization, and all characters except ASCII alphabets and ASCII numbers are replaced to hyphens as word separators.

Example
package main

import (
	"fmt"

	"github.com/sttk/stringcase"
)

func main() {
	cobol := stringcase.CobolCase("foo_bar_baz")
	fmt.Printf("cobol = %s\n", cobol)
}
Output:
cobol = FOO-BAR-BAZ

func CobolCaseWithKeep

func CobolCaseWithKeep(input, keeped string) string

Converts a string to cobol case using characters other than the specified characters as separators.

This function takes a string as its argument, then returns a string of which the case style is cobol case.

This function targets only the upper and lower cases of ASCII alphabets for capitalization, and the characters other than the specified characters as the second argument of this function are regarded as word separators and are replaced to hyphens.

Example
package main

import (
	"fmt"

	"github.com/sttk/stringcase"
)

func main() {
	cobol := stringcase.CobolCaseWithKeep("foo-bar100%baz", "%")
	fmt.Printf("cobol = %s\n", cobol)
}
Output:
cobol = FOO-BAR100%-BAZ

func CobolCaseWithSep

func CobolCaseWithSep(input, seps string) string

Converts a string to cobol case using the specified characters as separators.

This function takes a string as its argument, then returns a string of which the case style is cobol case.

This function targets only the upper and lower cases of ASCII alphabets for capitalization, and the characters specified as the second argument of this function are regarded as word separators and are replaced to hyphens.

Example
package main

import (
	"fmt"

	"github.com/sttk/stringcase"
)

func main() {
	cobol := stringcase.CobolCaseWithSep("foo-bar100%baz", "- ")
	fmt.Printf("cobol = %s\n", cobol)
}
Output:
cobol = FOO-BAR100%-BAZ

func KebabCase

func KebabCase(input string) string

Converts a string to kebab case.

This function takes a string as its argument, then returns a string of which the case style is kebab case.

This function targets the upper and lower cases of ASCII alphabets for capitalization, and all characters except ASCII alphabets and ASCII numbers are eliminated as word separators.

Example
package main

import (
	"fmt"

	"github.com/sttk/stringcase"
)

func main() {
	kebab := stringcase.KebabCase("fooBarBaz")
	fmt.Printf("kebab = %s\n", kebab)
}
Output:
kebab = foo-bar-baz

func KebabCaseWithKeep

func KebabCaseWithKeep(input, keeped string) string

Converts a string to kebab case using characters other than the specified characters as separators.

This function takes a string as its argument, then returns a string of which the case style is kebab case.

This function targets only the upper and lower cases of ASCII alphabets for capitalization, and the characters other than the specified characters as the second argument of this function are regarded as word separators and are replaced to hyphens.

Example
package main

import (
	"fmt"

	"github.com/sttk/stringcase"
)

func main() {
	kebab := stringcase.KebabCaseWithKeep("foo-bar100%baz", "%")
	fmt.Printf("kebab = %s\n", kebab)
}
Output:
kebab = foo-bar100%-baz

func KebabCaseWithSep

func KebabCaseWithSep(input, seps string) string

Converts a string to kebab case using the specified characters as separators.

This function takes a string as its argument, then returns a string of which the case style is kebab case.

This function targets only the upper and lower cases of ASCII alphabets for capitalization, and the characters specified as the second argument of this function are regarded as word separators and are replaced to hyphens.

Example
package main

import (
	"fmt"

	"github.com/sttk/stringcase"
)

func main() {
	kebab := stringcase.KebabCaseWithSep("foo-bar100%baz", "- ")
	fmt.Printf("kebab = %s\n", kebab)
}
Output:
kebab = foo-bar100%-baz

func MacroCase

func MacroCase(input string) string

Converts a string to macro case.

This function takes a string as its argument, then returns a string of which the case style is macro case.

This function targets the upper and lower cases of ASCII alphabets for capitalization, and all characters except ASCII alphabets and ASCII numbers are eliminated as word separators.

Example
package main

import (
	"fmt"

	"github.com/sttk/stringcase"
)

func main() {
	macro := stringcase.MacroCase("fooBarBaz")
	fmt.Printf("macro = %s\n", macro)
}
Output:
macro = FOO_BAR_BAZ

func MacroCaseWithKeep

func MacroCaseWithKeep(input, keeped string) string

Converts a string to macro case using characters other than the specified characters as separators.

This function takes a string as its argument, then returns a string of which the case style is macro case.

This function targets only the upper and lower cases of ASCII alphabets for capitalization, and the characters other than the specified characters as the second argument of this function are regarded as word separators and are replaced to underscores.

Example
package main

import (
	"fmt"

	"github.com/sttk/stringcase"
)

func main() {
	macro := stringcase.MacroCaseWithKeep("foo-Bar100%Baz", "%")
	fmt.Printf("macro = %s\n", macro)
}
Output:
macro = FOO_BAR100%_BAZ

func MacroCaseWithSep

func MacroCaseWithSep(input, seps string) string

Converts a string to macro case using the specified characters as separators.

This function takes a string as its argument, then returns a string of which the case style is macro case.

This function targets only the upper and lower cases of ASCII alphabets for capitalization, and the characters specified as the second argument of this function are regarded as word separators and are replaced to underscores.

Example
package main

import (
	"fmt"

	"github.com/sttk/stringcase"
)

func main() {
	macro := stringcase.MacroCaseWithSep("foo-Bar100%Baz", "- ")
	fmt.Printf("macro = %s\n", macro)
}
Output:
macro = FOO_BAR100%_BAZ

func PascalCase

func PascalCase(input string) string

Converts a string to pascal case.

This function takes a string as its argument, then returns a string of which the case style is pascal case.

This function targets the upper and lower cases of ASCII alphabets for capitalization, and all characters except ASCII alphabets and ASCII numbers are eliminated as word separators.

Example
package main

import (
	"fmt"

	"github.com/sttk/stringcase"
)

func main() {
	pascal := stringcase.PascalCase("foo_bar_baz")
	fmt.Printf("pascal = %s\n", pascal)
}
Output:
pascal = FooBarBaz

func PascalCaseWithKeep

func PascalCaseWithKeep(input, keeped string) string

Converts a string to pascal case using the specified characters as separators.

This function takes a string as its argument, then returns a string of which the case style is pascal case.

This function targets only the upper and lower cases of ASCII alphabets for capitalization, and the characters specified as the second argument of this function are regarded as word separators and are eliminated.

Example
package main

import (
	"fmt"

	"github.com/sttk/stringcase"
)

func main() {
	pascal := stringcase.PascalCaseWithKeep("foo-bar100%baz", "%")
	fmt.Printf("pascal = %s\n", pascal)
}
Output:
pascal = FooBar100%Baz

func PascalCaseWithSep

func PascalCaseWithSep(input, seps string) string

Converts a string to pascal case using the specified characters as separators.

This function takes a string as its argument, then returns a string of which the case style is pascal case.

This function targets only the upper and lower cases of ASCII alphabets for capitalization, and the characters specified as the second argument of this function are regarded as word separators and are eliminated.

Example
package main

import (
	"fmt"

	"github.com/sttk/stringcase"
)

func main() {
	pascal := stringcase.PascalCaseWithSep("foo-Bar100%Baz", "- ")
	fmt.Printf("pascal = %s\n", pascal)
}
Output:
pascal = FooBar100%Baz

func SnakeCase

func SnakeCase(input string) string

Converts a string to snake case.

This function takes a string as its argument, then returns a string of which the case style is snake case.

This function targets the upper and lower cases of ASCII alphabets for capitalization, and all characters except ASCII alphabets and ASCII numbers are eliminated as word separators.

Example
package main

import (
	"fmt"

	"github.com/sttk/stringcase"
)

func main() {
	snake := stringcase.SnakeCase("fooBarBaz")
	fmt.Printf("snake = %s\n", snake)
}
Output:
snake = foo_bar_baz

func SnakeCaseWithKeep

func SnakeCaseWithKeep(input, keeped string) string
Example
package main

import (
	"fmt"

	"github.com/sttk/stringcase"
)

func main() {
	snake := stringcase.SnakeCaseWithKeep("foo-bar100%baz", "%")
	fmt.Printf("snake = %s\n", snake)
}
Output:
snake = foo_bar100%_baz

func SnakeCaseWithSep

func SnakeCaseWithSep(input, seps string) string

Converts a string to snake case using the specified characters as separators.

This function takes a string as its argument, then returns a string of which the case style is snake case.

This function targets only the upper and lower cases of ASCII alphabets for capitalization, and the characters specified as the second argument of this function are regarded as word separators and are replaced to underscores.

Example
package main

import (
	"fmt"

	"github.com/sttk/stringcase"
)

func main() {
	snake := stringcase.SnakeCaseWithSep("foo-Bar100%Baz", "- ")
	fmt.Printf("snake = %s\n", snake)
}
Output:
snake = foo_bar100%_baz

func TrainCase

func TrainCase(input string) string

Converts a string to train case.

This function takes a string as its argument, then returns a string of which the case style is train case.

This function targets the upper and lower cases of ASCII alphabets for capitalization, and all characters except ASCII alphabets and ASCII numbers are eliminated as word separators.

Example
package main

import (
	"fmt"

	"github.com/sttk/stringcase"
)

func main() {
	train := stringcase.TrainCase("fooBarBaz")
	fmt.Printf("train = %s\n", train)
}
Output:
train = Foo-Bar-Baz

func TrainCaseWithKeep

func TrainCaseWithKeep(input, keeped string) string

Converts a string to train case using characters other than the specified characters as separators.

This function takes a string as its argument, then returns a string of which the case style is train case.

This function targets only the upper and lower cases of ASCII alphabets for capitalization, and the characters other than the specified characters as the second argument of this function are regarded as word separators and are replaced to hyphens.

Example
package main

import (
	"fmt"

	"github.com/sttk/stringcase"
)

func main() {
	train := stringcase.TrainCaseWithKeep("foo-bar100%baz", "%")
	fmt.Printf("train = %s\n", train)
}
Output:
train = Foo-Bar100%-Baz

func TrainCaseWithSep

func TrainCaseWithSep(input, seps string) string

Converts a string to train case using the specified characters as separators.

This function takes a string as its argument, then returns a string of which the case style is train case.

This function targets only the upper and lower cases of ASCII alphabets for capitalization, and the characters specified as the second argument of this function are regarded as word separators and are replaced to hyphens.

Example
package main

import (
	"fmt"

	"github.com/sttk/stringcase"
)

func main() {
	train := stringcase.TrainCaseWithSep("foo-bar100%baz", "- ")
	fmt.Printf("train = %s\n", train)
}
Output:
train = Foo-Bar100%-Baz

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL