Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: MS-DOS 8.0: Determine String Length?  (Read 103078 times)

0 Members and 1 Guest are viewing this topic.

Ana21

    Topic Starter


    Newbie

    • Experience: Beginner
    • OS: Unknown
    MS-DOS 8.0: Determine String Length?
    « on: November 19, 2020, 03:03:29 AM »
    Using MS-DOS 8.0, what is the best way to determine the length of a string?

    I looked through Computer Hope (http://www.computerhope.com/msdos.htm), but no commands jumped out at me...

    Is there a built in command or must a function be built to address this?

    Thanks a ton.

    Sidewinder



      Guru

      Thanked: 139
    • Experience: Familiar
    • OS: Windows 10
    Re: MS-DOS 8.0: Determine String Length?
    « Reply #1 on: November 19, 2020, 02:41:09 PM »
    This might help:

    Code: [Select]
    @echo off
    setlocal
    set /p var=Enter String Value:

    :length
      if defined var (set var=%var:~1%& set /a length+=1 & goto length)
      echo String length is: %length% characters

    Quirk Alert: %& must be concatenated in "if defined" line

    MS-DOS 8? Wow!  8)

    The true sign of intelligence is not knowledge but imagination.

    -- Albert Einstein

    patio

    • Moderator


    • Genius
    • Maud' Dib
    • Thanked: 1769
      • Yes
    • Experience: Beginner
    • OS: Windows 7
    Re: MS-DOS 8.0: Determine String Length?
    « Reply #2 on: November 19, 2020, 06:24:12 PM »
    Never heard of it...
    " Anyone who goes to a psychiatrist should have his head examined. "

    clarkegriffinx



      Newbie

      • Experience: Beginner
      • OS: Unknown
      Re: MS-DOS 8.0: Determine String Length?
      « Reply #3 on: May 27, 2021, 03:02:07 PM »
      Using MS-DOS 8.0, what is the best way to determine the length of a string?

      I looked through Computer Hope (http://www.computerhope.com/msdos.htm), but no commands jumped out at me...

      Is there a built in command or must a function be built to address this?
      McDVOICE MyBKExperience Liteblue

      Thanks a ton


      I got and example and this is it cleaned up a litte

      Code: [Select]
      @echo off
      setlocal
      set #=%1
      set length=0
      :loop
      if defined # (set #=%#:~1%&set /A length += 1&goto loop)
      echo %1 is %length% characters long!
      endlocal

      Here's another option. Pass the string as a paramter like this:

      Code: [Select]
         LEN "this is a long string"
      Here's the code:

      Code: [Select]
         @echo off

         echo.%~1>len
         for %%a in (len) do set /a len=%%~za -2

         echo %len%
      Copy and paste the code into Notepad and save it as LEN.BAT.

      NOTE: The fullstop following the ECHO statement is vital should a NUL string be entered. The -2 is required because ECHO automatically adds a CR & LF at the end of each line.