## Hl7Segment class

The Hl7Segment class represents one HL7 segment. A segment is a collection of fields. The name field is the first field.

**Signature:**

```typescript
export declare class Hl7Segment
```

## Constructors

| Constructor | Modifiers | Description |
| --- | --- | --- |
| [(constructor)(fields, context)](/content/docs/sdk/core.hl7segment._constructor_/index.html) |  | Creates a new HL7 segment.<br>Field strings are not parsed until they are accessed via [Hl7Segment.getField()](/content/docs/sdk/core.hl7segment.getfield) or via the [Hl7Segment.fields](/content/docs/sdk/core.hl7segment.fields) getter. |

## Properties

| Property | Modifiers | Type | Description |
| --- | --- | --- | --- |
| [context](/content/docs/sdk/core.hl7segment.context) | `readonly` | [Hl7Context](/content/docs/sdk/core.hl7context) |  |
| [fields](/content/docs/sdk/core.hl7segment.fields) | `readonly` | [Hl7Field](/content/docs/sdk/core.hl7field) | Returns all HL7 fields, parsing any unparsed field strings on first access.<br>Prefer [Hl7Segment.getField()](/content/docs/sdk/core.hl7segment.getfield) when you only need a subset of fields; that method avoids parsing unrelated fields. |
| [name](/content/docs/sdk/core.hl7segment.name) | `readonly` | string |  |

## Methods

| Method | Modifiers | Description |
| --- | --- | --- |
| [get(index)](/content/docs/sdk/core.hl7segment.get) |  | Returns an HL7 field by index. |
| [getComponent(fieldIndex, component, subcomponent, repetition)](/content/docs/sdk/core.hl7segment.getcomponent) |  | Returns an HL7 component by field index and component index.<br>This is a shortcut for `getField(field).getComponent(component)`.<br>Note that both indices are 1-based, not 0-based.<br>For example, to get the first component, use `getComponent(1, 1)`.<br>This aligns with HL7 component names such as MSH.9.2. |
| [getField(index)](/content/docs/sdk/core.hl7segment.getfield) |  | Returns an HL7 field by index.<br>Note that the index is 1-based, not 0-based.<br>For example, to get the first field, use `getField(1)`.<br>This aligns with HL7 field names such as PID.1, PID.2, etc.<br>Field zero is the segment name.<br>Fields are lazily parsed; the requested field is parsed and cached on demand. |
| [parse(text, context)](/content/docs/sdk/core.hl7segment.parse) | `static` | Parses an HL7 segment string into an Hl7Segment object. |
| [setComponent(fieldIndex, component, value, subcomponent, repetition)](/content/docs/sdk/core.hl7segment.setcomponent) |  | Sets a component value by field index and component index. This is a shortcut for `getField(field).setComponent(component, value)`. Note that both indices are 1-based, not 0-based. |
| [setField(index, field)](/content/docs/sdk/core.hl7segment.setfield) |  | Sets a field at the specified index. If that index does not exist, it will be added. Note that the index is 1-based, not 0-based. |
| [toString()](/content/docs/sdk/core.hl7segment.tostring) |  | Returns the HL7 segment as a string.<br>Unparsed fields are emitted directly from their original string form, which preserves the source text without forcing a parse. |
