# Hyphenating Text in Code

> Get the hyphenation points of words with GetHyphenationPoints and GetHyphenationPoint: a C# console app with TXSpell and the Windows Forms hyphenation sample.

- **Product:** TX Spell .NET for Windows Forms
- **Section:** Documentation
- **Kind:** Topic
- **Path:** TX Spell .NET for Windows Forms > Hyphenation > Hyphenating Text in Code
- **2279 words**
- **Web URL:** https://docs.textcontrol.com/tx-spell-dotnet-winforms/hyphenation/in-code/
- **LLMs URL:** https://docs.textcontrol.com/tx-spell-dotnet-winforms/hyphenation/in-code/llms.txt
- **LLMs-Full URL:** https://docs.textcontrol.com/tx-spell-dotnet-winforms/hyphenation/in-code/llms-full.txt

---

TX Spell .NET finds the places where a word can be divided, for your own code to use: a label that wraps, a report layout, a text box that shows the syllables of a word. Two methods answer the question, one with every hyphenation point of a word and one with the last point before a given position. A console application shows both, with its real output, followed by the hyphenation sample of the setup.

### The Two Methods

The [TXSpell](https://docs.textcontrol.com/tx-spell-dotnet-winforms/reference/txtextcontrol-proofing/txspell/llms-full.txt) class and the [TXSpellChecker](https://docs.textcontrol.com/tx-spell-dotnet-winforms/reference/txtextcontrol-proofing/txspellchecker/llms-full.txt) component have the same two methods:

| Method | Returns |
|---|---|
| [GetHyphenationPoints](https://docs.textcontrol.com/tx-spell-dotnet-winforms/reference/txtextcontrol-proofing/txspell/gethyphenationpoints-method/llms-full.txt) | All hyphenation points of a word, as an array of positions |
| [GetHyphenationPoint](https://docs.textcontrol.com/tx-spell-dotnet-winforms/reference/txtextcontrol-proofing/txspell/gethyphenationpoint-method/llms-full.txt) | The last hyphenation point before a divide position, for a word that has to be divided there |

Both use the default hyphenation list, the one [HyphenationLanguage](https://docs.textcontrol.com/tx-spell-dotnet-winforms/reference/txtextcontrol-proofing/txspell/hyphenationlanguage-property/llms-full.txt) selects, or the list of a language you pass. [Hyphenation Lists](https://docs.textcontrol.com/tx-spell-dotnet-winforms/hyphenation/lists/llms-full.txt) describes the lists. Use `TXSpell` in code without a user interface, and the methods of `TXSpellChecker` in a Windows Forms application that already has the component on a form.

### Create the Console Application

The console application uses the package `TXTextControl.TXSpell.Core.SDK`, which contains the `TXSpell` class and the US English language data, including the hyphenation list *hyph\_en\_US.dic*. Create the project and install the package as [Checking Text in Code](https://docs.textcontrol.com/tx-spell-dotnet-winforms/getting-started/in-code/llms-full.txt) describes, with the name *Hyphenate*:

```bash
dotnet new console -n Hyphenate
cd Hyphenate
dotnet add package TXTextControl.TXSpell.Core.SDK
```

Replace the contents of *Program.cs*:

```csharp
using System;
using System.Collections.Generic;
using TXTextControl.Proofing;

// Create the spell checker and initialize it before first use
TXSpell spell = new TXSpell();
spell.Create();

// The hyphenation lists loaded from the HyphenationLists folder
foreach (HyphenationList list in spell.HyphenationLists)
{
    Console.WriteLine($"Hyphenation list: {list.Name} ({list.Language})");
}

string[] words = { "hyphenation", "documentation", "international", "application" };

foreach (string word in words)
{
    // The positions where the word can be divided
    int[] points = spell.GetHyphenationPoints(word);

    // Split the word at these positions and join the parts with hyphens
    var parts = new List<string>();
    int index = 0;

    foreach (int point in points)
    {
        if (point > index)
        {
            parts.Add(word.Substring(index, point - index));
        }

        index = point;
    }

    Console.WriteLine($"{word}: {string.Join("-", parts)} [{string.Join(", ", points)}]");
}

// The last position at which "documentation" can be divided before position 8
int divide = spell.GetHyphenationPoint("documentation", 8);
Console.WriteLine($"documentation, divided before position 8: {"documentation".Substring(0, divide)}-");
```

Run it with `dotnet run`. The output lists the hyphenation list, each word with its parts and positions, and the division of "documentation":

```text
Hyphenation list: hyph_en_US.dic (en-US)
hyphenation: hy-phen-ation [0, 2, 6, 11]
documentation: doc-u-men-ta-tion [0, 3, 4, 7, 9, 13]
international: in-ter-na-tion-al [0, 2, 5, 7, 11, 13]
application: ap-pli-ca-tion [0, 2, 5, 7, 11]
documentation, divided before position 8: documen-
```

The build shows CA1416 warnings, as [Checking Text in Code](https://docs.textcontrol.com/tx-spell-dotnet-winforms/getting-started/in-code/#run-the-application) explains; they don't stop the application.

#### What the Code Does

- [Create](https://docs.textcontrol.com/tx-spell-dotnet-winforms/reference/txtextcontrol-proofing/txspell/create-method/llms-full.txt) loads the language data. Before it, `GetHyphenationPoints` throws an `ArgumentException` that asks for `Create`.
- [HyphenationLists](https://docs.textcontrol.com/tx-spell-dotnet-winforms/reference/txtextcontrol-proofing/txspell/hyphenationlists-property/llms-full.txt) holds the list chosen by `HyphenationLanguage`, here the English list of the package.
- `GetHyphenationPoints` returns the positions as character offsets from the start of the word. The loop cuts the word at each position and skips the empty part before the first one.
- `GetHyphenationPoint` returns 7 for "documentation" and position 8, so the word is divided after "documen".

### How the Positions Work

The array of `GetHyphenationPoints` begins with 0 and ends with the length of the word; the hyphenation points are the positions in between. For "hyphenation", `[0, 2, 6, 11]` means the parts "hy", "phen" and "ation". A word that can't be divided, such as "cat", returns `[0, 3]`.

Pass single words. For a text with spaces, the positions of the spaces are returned as well.

`GetHyphenationPoint` takes the word and a divide position, the number of characters that fit into the line. It returns the last hyphenation point at or before that position, 0 when there is none, and the length of the word when the position is at or behind its end. For "documentation" and the positions 0 to 14, it returns 0, 0, 0, 3, 4, 4, 4, 7, 7, 9, 9, 9, 9, 13 and 13.

### Use the List of Another Language

Both methods have an overload with a `CultureInfo`, which hyphenates with the list of that language. The list must be in `HyphenationLists` or in the *HyphenationLists* folder. The British English list, from the archive of that language, divides "colourful" differently from the American one:

```csharp
using System.Globalization;

int[] points = spell.GetHyphenationPoints("colourful", new CultureInfo("en-GB"));
// col-our-ful with hyph_en_GB.dic, colour-ful with hyph_en_US.dic
```

The methods look for a list of the exact language first. Without one, they take a list of nearly the same language, such as *hyph\_de\_DE.dic* for Austrian German. When no list fits at all, the word isn't divided: the array holds only 0 and the length.

### The Hyphenation Sample of the Setup

The setup installs a Windows Forms sample, in the *Hyphenation* folder below *TX Spell 11.0.NET for Windows Forms\\Samples\\WinForms* in your *Documents* folder, in C# and Visual Basic. It hyphenates the word typed into its text box with the rules of the options below it:

![The window of the Hyphenation sample: the word Hyphenation in the text box, the options Hyphenate word in UPPERCASE and the minimum numbers of characters at word begin and end, a Hyphenate button, and the result -Hy-phen-ation- with blue parts and red hyphens.](https://docs.textcontrol.com/assets/img/tx-spell-dotnet-winforms/hyphenation-sample.70716661.webp)

The sample calls the method of the `TXSpellChecker` on its form, [GetHyphenationPoints](https://docs.textcontrol.com/tx-spell-dotnet-winforms/reference/txtextcontrol-proofing/txspellchecker/gethyphenationpoints-method/llms-full.txt), and writes each part in blue and a hyphen in red. Its options set [HyphenateCapitalizedWords](https://docs.textcontrol.com/tx-spell-dotnet-winforms/reference/txtextcontrol-proofing/hyphenationlist/hyphenatecapitalizedwords-property/llms-full.txt), [MinCharsAtWordBegin](https://docs.textcontrol.com/tx-spell-dotnet-winforms/reference/txtextcontrol-proofing/hyphenationlist/mincharsatwordbegin-property/llms-full.txt) and [MinCharsAtWordEnd](https://docs.textcontrol.com/tx-spell-dotnet-winforms/reference/txtextcontrol-proofing/hyphenationlist/mincharsatwordend-property/llms-full.txt) of the default list. Its loop doesn't skip the positions 0 and the word length, which is why the result begins and ends with a hyphen; the loop of the console application above avoids that.

[Dictionary and Language Examples](https://docs.textcontrol.com/tx-spell-dotnet-winforms/examples/dictionaries/llms-full.txt) lists this sample with the other samples on language data.

### Next Steps

- [Hyphenation Lists](https://docs.textcontrol.com/tx-spell-dotnet-winforms/hyphenation/lists/llms-full.txt): the lists of other languages, and their settings.
- [Hyphenation with TX Text Control](https://docs.textcontrol.com/tx-spell-dotnet-winforms/hyphenation/tx-text-control/llms-full.txt): hyphenate the text of an editor automatically.
- [Checking Text in Code](https://docs.textcontrol.com/tx-spell-dotnet-winforms/getting-started/in-code/llms-full.txt): spell check text with the same `TXSpell` class.

---

## Nearby

- **Previous:** [Hyphenation With TX Text Control](https://docs.textcontrol.com/tx-spell-dotnet-winforms/hyphenation/tx-text-control/llms-full.txt)
- **Next:** [Synonyms and Thesaurus](https://docs.textcontrol.com/tx-spell-dotnet-winforms/synonyms/llms-full.txt)
