Convert Text to Header-Case (Train-Case)
Learn how to convert text to Header-Case (also called Train-Case), where each word is capitalized and separated by hyphens. Understand its use in HTTP headers and technical naming.
Detailed Explanation
Converting Text to Header-Case
Header-Case (also known as Train-Case) capitalizes the first letter of every word and separates words with hyphens. It combines the capitalization of PascalCase with the delimiter of kebab-case.
Basic Conversion
Input: content type
Output: Content-Type
Input: x forwarded for
Output: X-Forwarded-For
Input: accept_encoding
Output: Accept-Encoding
Where Header-Case Is Used
HTTP Headers
The most prominent use of Header-Case is in HTTP headers:
Content-Type: application/json
Accept-Encoding: gzip, deflate
X-Forwarded-For: 192.168.1.1
Cache-Control: no-cache
Authorization: Bearer eyJhbGci...
Access-Control-Allow-Origin: *
While HTTP/2 lowercases all headers, the traditional HTTP/1.1 convention uses Header-Case.
Configuration Keys
Some configuration formats use Header-Case:
# YAML configuration
Content-Security-Policy: "default-src 'self'"
X-Frame-Options: DENY
Strict-Transport-Security: "max-age=31536000"
Title Formatting (Hyphenated)
In some technical documentation and naming systems, Header-Case is used for multi-word identifiers that need to be both human-readable and machine-parseable.
Conversion Algorithm
- Split the input into words (by spaces, underscores, hyphens, or case transitions).
- Capitalize the first letter of each word.
- Lowercase the remaining letters of each word.
- Join with hyphens (
-).
Header-Case vs. Other Hyphenated Cases
Header-Case: Content-Type
kebab-case: content-type
The difference is capitalization: Header-Case capitalizes each word, kebab-case lowercases everything.
HTTP/2 Considerations
HTTP/2 mandates lowercase header names (content-type), making Header-Case technically a legacy format for HTTP headers. However, Header-Case remains the conventional way to refer to headers in documentation, and many HTTP libraries still display headers in Header-Case for readability.
Edge Cases
- Single word:
"accept"→"Accept". - Acronyms:
"DNS"→"Dns"(unless an exception list preserves known acronyms). - Numbers:
"x-custom-header-2"→"X-Custom-Header-2". - Already Header-Case: passes through unchanged.
Use Case
Header-Case is used when constructing or documenting HTTP headers, configuring reverse proxy rules, writing API documentation that references header names, and generating configuration files for web servers (Nginx, Apache). It is also useful for creating human-readable hyphenated identifiers in technical systems.