有特殊字符批量获取字符串长度 [英] batch get string length with special characters

查看:203
本文介绍了有特殊字符批量获取字符串长度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含两个文本列的文件。使用批处理文件,我想提取文本的第二列,并获得字符串的长度,然后写入字符串的长度和字符串文本输出文件。 ,挑战我的是确定哪些具有特殊字符的字符串长度的一步。例如,输入文件如下:

I have a file containing two columns of text. Using a batch file, I would like to extract the second column of text and get the string length then write the string length and the string text to an output file. The step that challenges me is determining the string length which has special characters. For example, the input file looks like:


escitalopram CN(C)CCC[C@@]1(C2=C(CO1)C=C(C=C2)C#N)C3=CC=C(C=C3)F
ibuprofen CC(C)CC1=CC=C(C=C1)C(C)C(=O)O
keflex CC1=C(N2[C@@H]([C@@H](C2=O)NC(=O)[C@@H](C3=CC=CC=C3)N)SC1)C(=O)O 
aspirin CC(=O)OC1=CC=CC=C1C(=O)O 
linoleic_acid CCCCC/C=C\C/C=C\CCCCCCCC(=O)O

我可以读取文件中提取使用批处理命令行参数和1%两种令牌。我已经尝试了一些我在讨论组中发现的子程序,但我不能让他们的工作。在=号,也许是其他特殊字符会导致问题。我寻找这将产生象一个输出文件中的溶液。忽略了@,/和\\的迹象:

I can read the file extracting the two tokens using a batch command line and argument %1. I have tried a few of the subroutines I found in the discussion groups but I can not get them to work. The "=" sign and perhaps the other special characters cause problems. I looking for a solution that would produce an output file like. ignoring the "@","/" and "\" signs:


escitalopram 49
ibuprofen 29 
keflex 58 
aspirin 24
linoleic_acid 25 

我的计划迄今如下:

My program thus far looks like:

@echo off
setLocal EnableDelayedExpansion enableextensions


set arg1=%1

FOR /F "tokens=1,2 delims= " %%r IN (%1) DO (
set teststring="%%s"
echo "Passing     " %%s
call :GetStrLength %%s
echo.%%s
goto :EOF
)
  ::========================
  :GetStrLength
  setlocal enableextensions

set s=%1
echo " counting.... " %1

:: Get the length of the quoted string assuming a max of 255
set charCount=0
for /l %%c in (0,1,255) do (
  set si=!s:~%%c!
  if defined si set /a charCount+=1)
if %charCount% EQU 256 set charCount=0
echo The length of "%s%" is %charCount% characters
endlocal & goto :EOF

任何帮助将是AP preciated。

Any help would be appreciated.

推荐答案

您可以使用strlen函数,但你应该使用,而不是BYVAL参数牛栏。

You can use a strlen function, but you should use byre instead of byval parameters.

此功能可以处理任何字符串,它总是需要13循环,以确定长度。结果
作为批变量可以包含不超过8191个字符,这是不够的。

This function can handle any string and it needs always 13 loops to determine the length.
As a variable in batch can contain not more than 8191 characters this is enough.

echo off
set "myString=Any content"
call :strlen result myString
echo %result%
exit /b

:strlen <resultVar> <stringVar>
(   
    setlocal EnableDelayedExpansion
    set "s=!%~2!#"
    set "len=0"
    for %%P in (4096 2048 1024 512 256 128 64 32 16 8 4 2 1) do (
        if "!s:~%%P,1!" NEQ "" ( 
            set /a "len+=%%P"
            set "s=!s:~%%P!"
        )
    )
)
( 
    endlocal
    set "%~1=%len%"
    exit /b
)

这篇关于有特殊字符批量获取字符串长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆