ForHosting KIT · Text & AI

Wrap text to a fixed line width

Set a width of 40 and a long sentence comes back as 3 lines, none wider than 40 characters and no word cut in half. Your own line breaks stay exactly where they are. Free in your browser.

  • Free
  • Runs in your browser
  • No sign-up
  • Your data stays on your device
Use it from WebAPIEmailTelegramApp soon
If you enter
Width
40
You get
Line count
3
Longest word
6

You send a string and an integer, you get back the wrapped text, the individual lines, and a few useful counts.

Why word-safe wrapping matters

Most display surfaces impose a hard column limit: a terminal at eighty columns, a plain-text email at seventy-two, a code comment block at one hundred, a printed invoice at forty. Reflowing text by hand is tedious and error-prone, and the naive approach — slicing the string every N characters — destroys readability because it cuts words in half.

The wrap text to line width capability does the job correctly with a greedy algorithm: it walks the words in order, adds each one to the current line while it fits, and starts a new line when the next word would overflow. Words are never broken, hyphenation is never invented, and the result is stable: the same input and the same width always produce byte-identical output, which makes it safe to use in tests, in generated documents, and in any pipeline that compares text.

Existing line breaks in your input are preserved, and each source line is wrapped independently, so pre-formatted paragraphs keep their structure.

How the algorithm behaves at the edges

The contract is strict where strictness protects you. The width must be a positive integer, and it must be at least as long as the longest word in the text. Runs of whitespace collapse to single spaces, blank source lines survive as blank lines, and the output reports the width used, the number of lines produced and the length of the longest word encountered, so you can log exactly what happened.

The algorithm measures length in characters as JavaScript counts them, which is the right unit for fixed-width terminal and code contexts.

Wrap commit messages and changelogs

Reflow prose bodies to the conventional 72-column limit before writing them to a repository or release file.

Format plain-text emails

Keep generated emails inside the classic 78-column envelope so they render cleanly in any mail client.

Lay out terminal and CLI output

Wrap help text and descriptions to the detected terminal width without ever splitting a flag or a URL mid-word.

What happens if a word is longer than the width?

The capability never breaks a word to make it fit.

Are existing line breaks preserved?

Yes. The input is split into lines first, and each line is wrapped independently, so paragraph structure survives.

Does it hyphenate long words?

No. Hyphenation is never invented; words are kept whole and the line simply breaks earlier.

Is the output deterministic?

Yes. The greedy algorithm has no randomness, no clock and no locale dependence, so identical input always yields identical output.

How much text can I send?

Split larger documents at paragraph boundaries before sending.

Everything on this page is available programmatically. This section is for teams who want to wire it into their own systems; everyone else can just use the tool above.

POSThttps://api.kit.forhosting.com/text/wrap-width

Prefer to automate it? One authenticated POST creates the task; the result comes back by webhook or a signed link. The same capability also runs here on the web, by email and from Telegram — and soon from our app too.

curl -X POST https://api.kit.forhosting.com/text/wrap-width \
  -H "Authorization: Bearer $KIT_KEY" \
  -H "Content-Type: application/json" \
  -d '{"text":"The quick brown fox jumps over the lazy dog while the sun rises slowly over the quiet hills.","width":40}'
{
  "text": "The quick brown fox jumps over the lazy dog while the sun rises slowly over the quiet hills.",
  "width": 40
}
{
  "task_id": "tsk_a1b2c3d4e5f6a1b2c3d4e5f6",
  "type": "text.wrap_width",
  "status": "queued",
  "_links": {
    "result": "/tasks/tsk_…/result"
  }
}

The API is asynchronous: the call returns a task_id immediately and the result arrives by webhook. Polling is capped at 1 req/s per task.

Per request$0.002

Published price — no tokens, no invented credits. A failed task is never charged.

max_mb1
max_chars500000
HTTPCodeMeaning
401unauthorizedMissing or invalid API key.
402insufficient_balanceYour balance doesn't cover the task price.
404unknown_typeThat task type doesn't exist.
429rate_limitedToo many requests. Use the webhook instead of polling.

The text wrapping API takes any plain text and a maximum line width, and returns the text reflowed into lines that never exceed that width and never split a word. It is the classic word-wrap algorithm done properly: greedy, deterministic, word-safe, and honest enough to refuse the job when a single word is longer than the width you asked for. No guesswork, no hidden hyphenation, no surprises between environments — the same code runs in your browser for free and behind the paid API.

How the algorithm behaves at the edges

If a single word — say, a URL, a serial number or a long compound — exceeds the width, the capability refuses with a clear invalid_input error naming the offending word and its length, instead of silently emitting a line that violates your limit. That refusal is deliberate: a wrapped document that secretly exceeds its column budget breaks layouts downstream, and you want to catch that at the source.

Where it fits in a pipeline

Teams call it anywhere fixed-width text is generated: formatting release notes for a changelog file, wrapping commit message bodies to the conventional seventy-two columns, preparing plain-text email templates, laying out terminal help output, or fitting descriptions into the column budget of a generated PDF or invoice. Because the output also includes the lines as an array, downstream steps can join them with their own separator, number them, or diff two wrappings without re-splitting the text themselves. Because it is a pure parse capability with no network and no model, it is fast, cheap at $0.002 per request, and deterministic enough to sit inside a test suite. Inputs are limited to 500,000 characters per request, which comfortably covers any realistic document; larger corpora should be split at natural boundaries such as paragraphs before sending.

What does it cost?

$0.002 per request. It is also free to run in your browser on this page — the same code runs in both places.

Read the full KIT documentation →