# string() function

> The string() function declares a string field of at most a number of characters.

The **`string()`** function declares a `string` field of at most `max` characters, for [`entity()`](/docs/room/entity) or [`call()`](/docs/room/call).

## Syntax

```ts
string(max)
```

### Parameters

`max`
: The most characters, a whole number from 1 to 16,384, counted as `value.length` counts them, the way `<input maxlength>` does.

### Return value

A field declaration, typed `string`.

### Exceptions

`RangeError`
: Thrown if `max` isn't a whole number from 1 to 16,384.

## Description

A longer value is refused like any value over its field's limit: a write or `create` throws a `RangeError`, and a call arg fails the call's check. The 16 KB of UTF-8 every `string` field has still applies. The bound is part of the schema hash.

## Examples

### Bounding a player's chosen name

```ts
// names.ts
import { call, defineCalls, defineRoom, defineSchema, entity, string } from "@irtio/room";

const Tag = entity("tag", { player: "string", text: string(16) }, { key: "player" });
const schema = defineSchema({ entities: [Tag], calls: defineCalls({ rename: call({ text: string(16) }) }) });

export default defineRoom({
  schema,
  calls: {
    rename({ text }, { room, connection }) {
      const tag = room.entities.get(Tag, connection.player) ?? room.entities.create(Tag, { player: connection.player });
      tag.text = text;
    },
  },
});
```

## See also

- [`entity()`](/docs/room/entity)
- [`json()`](/docs/room/json)
