---
title: "3 Top Ideas to Get String Length in COBOL Unstring"
description: "In COBOL unstring, you can find output length using three clauses. It also shows examples of how to use these clauses."
url: https://srinimf.com/2022/10/01/3-top-ideas-to-get-string-length-in-cobol-unstring/
date: 2022-10-01
modified: 2026-10-10
author: "Srini"
image: https://i0.wp.com/srinimf.com/wp-content/uploads/2022/10/pexels-photo-10894941.jpeg?fit=1880%2C1253&ssl=1
categories: ["COBOL"]
tags: ["Unstring Length"]
type: post
lang: en
---

# 3 Top Ideas to Get String Length in COBOL Unstring

Here’re three top ideas to get string length during unstring in COBOL. The function of the unstring is it splits the input string based on a delimiter. The delimiter can be SPACE, COMMA, etc. After getting unstring’s length, you can use it for validation to avoid an exception.

Related[COBOL: All About Null-terminated Strings](https://srinimf.com/2016/01/29/apply-these-technics-to-handle-null-terminated-strings-in-cobol/)

Here are the clauses **WITH POINTER**,** LENGTH OF**, and** COUNT IN **you can use to get the length of outputs.

## 1. WITH POINTER

WITH POINTER clause, you will get the length of the string in the ws_count. You will have the value of ( 7 – 1), which means 6. The space count is ‘1’. Note that before using the pointer, initialize it.

```
MOVE   1       TO ws_count
MOVE "MY NAME"   TO ws_mystr
UNSTRING ws-mystr DELIMITED BY SPACE
INTO ws-string1, ws-string2
WITH POINTER ws-count
ON OVERFLOW DISPLAY "STRING SIZE OVERFLOWED"
NOT ON OVERFLOW DISPLAY "STRING IS NOT OVERFLOWED"
END-UNSTRING.
```

## 2. LENGTH OF

The logic below forces the ws_out to accommodate the string from the input. To do it, use the LENGTH OF clause in the unstring.

Related[Real Logic COBOL Date, Fraction and Strings](https://srinimf.com/2013/04/24/cobol-data-challenges2-of-2/)

```
UNSTRING WS-1
 INTO WS-OUT(1:LENGTH OF WS_OUT)
END-UNSTRING
```

Advertisements

## 3. COUNT IN

The clause COUNT IN you can use to get the output string’s length. It is the preferable idea. Here the WS-COUNT1 and WS-COUNT2 will have the unstring outputs length.

```
01 WS-3      PIC X(10) VALUE "MY NAME".
01 WS-OUT1   PIC X(4).
01 WS-OUT2   PIC X(4).
01 WS-COUNT1 COMP.
01 WS-COUNT2 COMP.

UNSTRING WS-3
  DELIMITED BY ALL SPACES,
  INTO WS-OUT1 COUNT IN WS-COUNT1,
       WS-OUT2 COUNT IN WS-COUNT2
END-UNSTRING
```

Related

- [COBOL strings popular ideas to learn now (2 of 2)](https://srinimf.com/2015/07/27/mainframecobol-strings-inspectconcatenation-splitting-2-of-2/)DateJuly 27, 2015
- [How to Fix COBOL Value-clause and UNSTRING Errors](https://srinimf.com/2020/09/06/cobol-value-clause-vs-unstring-errors/)DateSeptember 6, 2020
- [3 Popular Ways to Handle DB2 VARCHAR Field in COBOL](https://srinimf.com/2014/08/14/3-ways-to-handle-db2-varchar-filed-in-cobol/)DateAugust 14, 2014

References

- [Unstring clause-IBM](https://www.ibm.com/docs/en/cobol-zos/6.2?topic=evaluation-corrective-action-unstring-operand)

### Share this:

-
- [Tweet](https://twitter.com/share)
- [![](https://i0.wp.com/assets.pinterest.com/images/pidgets/pinit_fg_en_rect_gray_20.png)](https://www.pinterest.com/pin/create/link/?url=https%3A%2F%2Fsrinimf.com%2F2022%2F10%2F01%2F3-top-ideas-to-get-string-length-in-cobol-unstring%2F&media=https%3A%2F%2Fi0.wp.com%2Fsrinimf.com%2Fwp-content%2Fuploads%2F2022%2F10%2Fpexels-photo-10894941.jpeg%3Ffit%3D1200%252C800%26ssl%3D1&description=3%20Top%20Ideas%20to%20Get%20String%20Length%20in%20COBOL%20Unstring)
-
- [Share on Tumblr](https://www.tumblr.com/share)
- [Share on WhatsApp (Opens in new window) WhatsApp](https://srinimf.com/2022/10/01/3-top-ideas-to-get-string-length-in-cobol-unstring/?share=jetpack-whatsapp)
- [More](#)
-

- [Print (Opens in new window) Print](https://srinimf.com/2022/10/01/3-top-ideas-to-get-string-length-in-cobol-unstring/?share=print)
- [Share on Reddit (Opens in new window) Reddit](https://srinimf.com/2022/10/01/3-top-ideas-to-get-string-length-in-cobol-unstring/?share=reddit)
- [Share on Telegram (Opens in new window) Telegram](https://srinimf.com/2022/10/01/3-top-ideas-to-get-string-length-in-cobol-unstring/?share=telegram)
- [Email a link to a friend (Opens in new window) Email](mailto:?subject=%5BShared%20Post%5D%203%20Top%20Ideas%20to%20Get%20String%20Length%20in%20COBOL%20Unstring&body=https%3A%2F%2Fsrinimf.com%2F2022%2F10%2F01%2F3-top-ideas-to-get-string-length-in-cobol-unstring%2F&share=email)
-

### Like this:

Like Loading…
