# How to use strings in Ada?

**URL:** https://forum.ada-lang.io/t/how-to-use-strings-in-ada/1317
**Category:** General
**Tags:** ada
**Created:** [October 12, 2024, 7:37pm UTC](https://forum.ada-lang.io/t/how-to-use-strings-in-ada/1317 "2024-10-12T19:37:39Z")
**Posts on this page:** 11
**Page:** 2

<div class="post-metadata">

### Author: ![simonjwright](https://forum.ada-lang.io/user_avatar/forum.ada-lang.io/simonjwright/32/11_2.png) [@simonjwright](https://forum.ada-lang.io/u/simonjwright)
#### Post date: [October 15, 2024, 9:32pm UTC](https://forum.ada-lang.io/t/how-to-use-strings-in-ada/1317/22 "2024-10-15T21:32:50Z")

</div>

There’s nothing wrong with heap allocation; for a short-lived not-too-large program, let the OS clean up on program exit.

For a long-lived program, deallocation needs to be taken care of. Most of the time, you can rely on well-written libraries, like `Ada.Strings.Unbounded` or the indefinite containers.

---

<div class="post-metadata">

### Author: ![gast](https://forum.ada-lang.io/letter_avatar_proxy/v4/letter/g/9fc29f/32.png) [@gast](https://forum.ada-lang.io/u/gast)
#### Post date: [October 15, 2024, 10:34pm UTC](https://forum.ada-lang.io/t/how-to-use-strings-in-ada/1317/23 "2024-10-15T22:34:07Z")

</div>

> Of course, in Ada you can have a specially designed memory pool to improve performance like mark-and-release pool.

Is there any sample code for such an implementation?

---

<div class="post-metadata">

### Author: ![stevelitt](https://forum.ada-lang.io/letter_avatar_proxy/v4/letter/s/dec6dc/32.png) [@stevelitt](https://forum.ada-lang.io/u/stevelitt)
#### Post date: [October 15, 2024, 11:51pm UTC](https://forum.ada-lang.io/t/how-to-use-strings-in-ada/1317/24 "2024-10-15T23:51:24Z")

</div>

[dmitry-kazakov] How would I implement your type Roman\_Latin if I wanted UTF-8 characters? Thanks.

---

<div class="post-metadata">

### Author: ![AJ-Ianozi](https://forum.ada-lang.io/user_avatar/forum.ada-lang.io/aj-ianozi/32/39_2.png) [@AJ-Ianozi](https://forum.ada-lang.io/u/AJ-Ianozi)
#### Post date: [October 16, 2024, 2:22am UTC](https://forum.ada-lang.io/t/how-to-use-strings-in-ada/1317/25 "2024-10-16T02:22:11Z")

</div>

Coincidentally @dmitry-kazakov Has written in one of the most influential string libraries with utf8 support in Ada back in the day, [Strings\_Edit](http://www.dmitry-kazakov.de/ada/strings_edit.htm), part of The Goliath known as [Simple Components](http://www.dmitry-kazakov.de/ada/components.htm).

---

<div class="post-metadata">

### Author: ![jere](https://forum.ada-lang.io/user_avatar/forum.ada-lang.io/jere/32/87_2.png) [@jere](https://forum.ada-lang.io/u/jere)
#### Post date: [October 16, 2024, 2:33am UTC](https://forum.ada-lang.io/t/how-to-use-strings-in-ada/1317/26 "2024-10-16T02:33:38Z")

</div>

> [@gast](#):
>
> Is there any sample code for such an implementation?

You could check out dmitry’s at: [Simple components for Ada](http://www.dmitry-kazakov.de/ada/components.htm#Pools_etc)

---

<div class="post-metadata">

### Author: ![dmitry-kazakov](https://forum.ada-lang.io/user_avatar/forum.ada-lang.io/dmitry-kazakov/32/522_2.png) [@dmitry-kazakov](https://forum.ada-lang.io/u/dmitry-kazakov)
#### Post date: [October 16, 2024, 11:49am UTC](https://forum.ada-lang.io/t/how-to-use-strings-in-ada/1317/27 "2024-10-16T11:49:57Z")

</div>

That is a way different question. If you wanted an enumeration type encoded in a specific way, e.g. UTF-8 then Ada has representation clause for that:

```ada
   type Roman_Latin is
        ( ' ','a','b','c','d','e','f','g','h','i','j','k','l',
           'm','n','o','p','q','r','s','t','v','x','y','z'
        );
   for Roman_Latin use
        ( 'a' => 97, 'b' => 98, 'c' => 99, 'd' => 100,
           'e' => 101, 'f' => 102, 'g' => 103, 'h' => 104,
           'i' => 105, 'j' => 106, 'k' => 107, 'l' => 108,
           'm' => 109, 'n' => 110, 'o' => 111, 'p' => 112,
           'q' => 113, 'r' => 114, 's' => 115, 't' => 116,
           'v' => 117, 'x' => 118, 'y' => 119, 'z' => 120,
           ' ' => 32
        );
   type Roman_String is array (Positive range <>) of Roman_Latin;

   A : constant Roman_String := "veni vidi vici";

```

Here Roman\_Latin characters have Unicode code points as values.  
What you cannot do is to make Roman\_Latin a subtype of Character or Wide\_Character. The Ada type system supports only range constraints, no holes.

---

<div class="post-metadata">

### Author: ![evanescente-ondine](https://forum.ada-lang.io/user_avatar/forum.ada-lang.io/evanescente-ondine/32/451_2.png) [@evanescente-ondine](https://forum.ada-lang.io/u/evanescente-ondine)
#### Post date: [October 16, 2024, 12:42pm UTC](https://forum.ada-lang.io/t/how-to-use-strings-in-ada/1317/28 "2024-10-16T12:42:04Z")

</div>

I think it’s bad form… Whenever I can reasonably predict a size, or the number of iterations of a loop a problem requires, I beat myself until I got the algorithm perfect. Isn’t this similar to the discussion over garbage collection ? I fear some practices make us lazy then we wonder why lightning fast complete graphical OSes used to fit on a few Mbs.

---

<div class="post-metadata">

### Author: ![OneWingedShark](https://forum.ada-lang.io/user_avatar/forum.ada-lang.io/onewingedshark/32/305_2.png) [@OneWingedShark](https://forum.ada-lang.io/u/OneWingedShark)
#### Post date: [October 16, 2024, 11:44pm UTC](https://forum.ada-lang.io/t/how-to-use-strings-in-ada/1317/29 "2024-10-16T23:44:06Z")

</div>

> [@dmitry-kazakov](#):
>
> What you cannot do is to make Roman\_Latin a subtype of Character or Wide\_Character. The Ada type system supports only range constraints, no holes.

IIRC, you _can_ do that with the `Static_Predicate`.

```ada
-- Subtype renaming for continent switching of subtype derivation.
Subtype Code_Points is Wide_Wide_Character;

-- Subtypes defining the parts.
Subtype Upper is Code_Points range 'A'..'Z';
Subtype Lower is Code_Points range 'a'..'z';
Subtype Digit is Code_Points range '0'..'9';
Subtype Other is Code_Points
   with Static_Predicate => ' ' | '.' | ',' | '?' | '(' | ')' | '"' | ''' | ':';

-- New type.
Type New_Roman_Latin is new Code_Points
  with Static_Predicate => Upper | Lower | Digit | Other;

```

---

<div class="post-metadata">

### Author: ![dmitry-kazakov](https://forum.ada-lang.io/user_avatar/forum.ada-lang.io/dmitry-kazakov/32/522_2.png) [@dmitry-kazakov](https://forum.ada-lang.io/u/dmitry-kazakov)
#### Post date: [October 17, 2024, 7:30am UTC](https://forum.ada-lang.io/t/how-to-use-strings-in-ada/1317/30 "2024-10-17T07:30:18Z")

</div>

Yes, I forgot that. Then it would be:

```ada
   subtype Roman_Latin is Character
      with Static_Predicate => Roman_Latin in
           ' '|'a'|'b'|'c'|'d'|'e'|'f'|'g'|'h'|'i'|'j'|'k'|'l'|
           'm'|'n'|'o'|'p'|'q'|'r'|'s'|'t'|'v'|'x'|'y'|'z';

```

---

<div class="post-metadata">

### Author: ![OneWingedShark](https://forum.ada-lang.io/user_avatar/forum.ada-lang.io/onewingedshark/32/305_2.png) [@OneWingedShark](https://forum.ada-lang.io/u/OneWingedShark)
#### Post date: [October 17, 2024, 6:25pm UTC](https://forum.ada-lang.io/t/how-to-use-strings-in-ada/1317/31 "2024-10-17T18:25:36Z")

</div>

You can simplify that:

```ada
   subtype Roman_Latin is Character
      with Static_Predicate => Roman_Latin in ' '|'a'..'z';

```

---

<div class="post-metadata">

### Author: ![dmitry-kazakov](https://forum.ada-lang.io/user_avatar/forum.ada-lang.io/dmitry-kazakov/32/522_2.png) [@dmitry-kazakov](https://forum.ada-lang.io/u/dmitry-kazakov)
#### Post date: [October 17, 2024, 6:59pm UTC](https://forum.ada-lang.io/t/how-to-use-strings-in-ada/1317/32 "2024-10-17T18:59:25Z")

</div>

Latin alphabet has no U and W.

```ada
   subtype Roman_Latin is Character
      with Static_Predicate => Roman_Latin in
           ' ' | 'a'..'t'|'v'|'x'..'z';

```

[Previous page](https://forum.ada-lang.io/t/how-to-use-strings-in-ada/1317.md?page=1)
