Β· By DevToolHub Team

Text Processing Tips for Developers: Count, Convert, Sort

Text manipulation is one of the most common tasks in development. Whether you’re counting words in user input, converting naming conventions, sorting log entries, or cleaning data files β€” having the right techniques at hand saves hours. This guide covers practical patterns for everyday text processing.

Counting: Words, Characters, Lines

Word Counting

The simplest word count splits on whitespace:

function countWords(text) {
  if (!text.trim()) return 0;
  return text.trim().split(/\s+/).length;
}

This works for most European languages where words are separated by spaces. For CJK text (Chinese, Japanese, Korean), word segmentation is a harder problem that typically requires a library like Intl.Segmenter.

In Python:

word_count = len(text.split())  # split() handles any whitespace

In the terminal:

wc -w < filename    # word count
wc -m < filename    # character count
wc -l < filename    # line count

Tip: wc is the Unix Swiss Army knife for text stats. Use wc -lwm to get lines, words, and characters in one call.

Try our Word Counter, Character Counter, and Line Counter for quick analysis without leaving the browser.

Character Counting Gotchas

Character counting seems trivial until Unicode enters the picture:

"Hello".length          // 5 β€” expected
"cafΓ©".length           // 4 β€” correct
"πŸ‘‹".length             // 2 β€” surprise! (surrogate pair)
"πŸ‘¨β€πŸ‘©β€πŸ‘§".length            // 8 β€” ZWJ sequence

JavaScript’s .length returns the number of UTF-16 code units, not visible characters. For a user-perceived character count (grapheme clusters), use:

function countGraphemes(text) {
  const segmenter = new Intl.Segmenter("en", { granularity: "grapheme" });
  return [...segmenter.segment(text)].length;
}

Rule of thumb: use .length for storage/byte calculations and Intl.Segmenter for display/UI character limits.

Case Conversion

Common Developer Cases

CaseExampleTypical use
camelCasemyVariableNameJavaScript variables, JSON keys
PascalCaseMyComponentNameClasses, React components, TypeScript types
snake_casemy_function_namePython, Ruby, Rust, database columns
kebab-casemy-css-classCSS, HTML attributes, URL slugs
CONSTANT_CASEMAX_RETRY_COUNTConstants, environment variables

Converting Between Cases in JavaScript

The key insight: first split into words, then reassemble.

function toWords(input) {
  return input
    .replace(/([a-z])([A-Z])/g, "$1 $2")  // split camelCase
    .replace(/[_\-]+/g, " ")               // split snake/kebab
    .split(/\s+/)
    .filter(Boolean);
}

const toSnakeCase = (s) => toWords(s).map(w => w.toLowerCase()).join("_");
const toCamelCase = (s) => toWords(s).map((w, i) =>
  i === 0 ? w.toLowerCase() : w[0].toUpperCase() + w.slice(1).toLowerCase()
).join("");
const toKebabCase = (s) => toWords(s).map(w => w.toLowerCase()).join("-");

Python: use the inflection library for robust conversions, or re.sub(r'(?<=[a-z])(?=[A-Z])', '_', s).lower() for a quick camelCase-to-snake_case.

Convert text between 9 different case styles with our Case Converter.

Sorting Lines

In the Terminal

sort filename              # alphabetical
sort -r filename           # reverse
sort -n filename           # numeric
sort -u filename           # unique (dedup + sort)
sort -t',' -k2 filename   # sort by 2nd CSV column
sort -V filename           # version/natural sort (file1, file2, file10)

In JavaScript

const lines = text.split("\n");

lines.sort();                              // alphabetical (ASCII order)
lines.sort((a, b) => a.localeCompare(b));  // locale-aware
lines.sort((a, b) =>                       // natural sort
  a.localeCompare(b, undefined, { numeric: true })
);

Watch out: JavaScript’s default .sort() uses lexicographic (ASCII) order where "10" < "9" because "1" < "9". Always use localeCompare with { numeric: true } for human-friendly sorting.

Removing Duplicates While Preserving Order

const unique = [...new Set(lines)];
unique = list(dict.fromkeys(lines))  # preserves insertion order (Python 3.7+)
awk '!seen[$0]++' filename   # preserves order, unlike sort -u

Sort, deduplicate, and shuffle lines with our Line Sorter.

Reversing Strings

Reversing text is a classic interview question, but the implementation matters:

// Naive β€” breaks emoji and surrogate pairs
"hello".split("").reverse().join("");

// Unicode-aware β€” handles most emoji correctly
[..."hello πŸ‘‹"].reverse().join("");

The spread operator iterates over Unicode code points (not UTF-16 code units), so it correctly handles single emoji. However, it still breaks ZWJ sequences (family emoji, skin tones) and combining characters (letters with accents). For production use, consider a grapheme-aware library.

Python handles this more gracefully:

text[::-1]  # slice reversal β€” works on code points

For quick string reversal, use our Text Reverser.

Building Text Pipelines

Real-world text processing often chains multiple operations. Here is a pipeline that cleans up a messy list of email addresses:

const cleaned = rawText
  .split("\n")                     // split into lines
  .map(line => line.trim())        // trim whitespace
  .filter(line => line.length > 0) // remove empty lines
  .map(line => line.toLowerCase()) // normalize case
  .filter(line => line.includes("@"))  // basic email filter
  .sort((a, b) => a.localeCompare(b))  // sort alphabetically
  .filter((v, i, a) => a[i - 1] !== v); // remove adjacent duplicates

Terminal equivalent:

cat emails.txt | tr -s ' ' | sed '/^$/d' | tr 'A-Z' 'a-z' | grep '@' | sort -u

The Unix philosophy of small composable tools applies to text processing in any language. Each step does one thing. Chain them together for complex transformations.

Performance Tips

  • Large files (>100 MB): Use streams instead of loading everything into memory. Node.js readline or Python open() with line-by-line iteration.
  • Repeated operations: Compile regex patterns once outside the loop (new RegExp() in JS, re.compile() in Python).
  • Sorting: JavaScript’s built-in .sort() uses Timsort (O(n log n)). For specialized sorts on large datasets, consider radix sort for integers or bucket sort for known distributions.
  • Deduplication of huge datasets: A Set in JS or set in Python works for millions of strings. For billions, consider probabilistic data structures like Bloom filters.

Summary

TaskJS one-linerTerminal one-liner
Count wordstext.trim().split(/\s+/).lengthwc -w < file
Count charstext.lengthwc -m < file
Count linestext.split('\n').lengthwc -l < file
Uppercasetext.toUpperCase()tr 'a-z' 'A-Z' < file
Sort lineslines.sort((a,b) => a.localeCompare(b))sort file
Unique lines[...new Set(lines)]sort -u file
Reverse text[...text].reverse().join('')rev < file

For more text manipulation techniques, check out our Regex Cheat Sheet with Examples and the URL Slug Best Practices guide.

Further Reading

FAQ

How do I count words in JavaScript?
Split on whitespace and filter empty strings: `text.trim().split(/\s+/).filter(Boolean).length`. This handles multiple spaces, tabs, and newlines correctly. For an empty string, check `text.trim()` first to avoid returning 1.
What is the fastest way to convert case in Python?
Python strings have built-in methods: `s.upper()`, `s.lower()`, `s.title()`, `s.capitalize()`, and `s.swapcase()`. For developer-specific conventions like camelCase or snake_case, use a library like `inflection` or write a helper that splits on word boundaries and reassembles.
How do I sort lines uniquely in the terminal?
Use `sort -u filename` which sorts alphabetically and removes duplicates in one pass. For case-insensitive unique sort: `sort -fu filename`. For natural sort (handling numbers correctly): `sort -Vu filename` on GNU sort.
Why does reversing a string break emoji?
Emoji like πŸ‘¨β€πŸ‘©β€πŸ‘§ are composed of multiple Unicode code points joined by zero-width joiners (ZWJ). Naive reversal using `split('').reverse().join('')` breaks them because it operates on individual UTF-16 code units. Use `[...str].reverse().join('')` which iterates over code points, or a library like `esrever` for full grapheme-cluster support.
How do I remove duplicate lines while preserving order?
In the terminal: `awk '!seen[$0]++' filename`. In JavaScript: `[...new Set(text.split('\n'))].join('\n')`. In Python: `list(dict.fromkeys(text.splitlines()))`. All three preserve the original order while removing duplicates.
What is the fastest way to count words in a large text file?
On the command line: `wc -w filename` is the fastest option for files up to a few GB. For multi-GB files, `awk '{count+=NF} END{print count}' filename` may be faster. In the browser, paste smaller text snippets into our [Word Counter](/tools/word-counter/) for instant counts plus reading time and other stats.
How do I sort lines but keep duplicates?
Use `sort` without the `-u` flag on the command line: `sort filename` keeps all lines including duplicates. In the browser, use the [Line Sorter](/tools/line-sorter/) and leave 'Remove duplicates' unchecked. To deduplicate without changing the order, use the [Duplicate Line Remover](/tools/duplicate-line-remover/) instead.
text programming productivity javascript python

Related Tools

Related Articles