zoukankan      html  css  js  c++  java
  • Windows BAT字符串操作

    ::操作系统:windows xp sp3

    @echo off

    ::初始化变量
    set str1=This is string1
    set str2=This is string2
    set str3=This is string3

    ::先打印出原始数据
    echo str1=%str1%
    echo str2=%str2%
    echo str3=%str3%


    ::类似strcpy,将一个字符串复制到另一个字符型指针或字符数组,覆盖原来的字符串
    ::实现方法:set 目标字符串=%源字符串%
    echo.
    echo -----------------------------------------------
    set strcpy=%str1%
    echo strcpy=%strcpy%


    ::类似strcat,将一个字符串连接到另一个字符型指针或字符数组的末尾。
    ::实现方法:set 目标字符串=%目标字符串%%源字符串%
    echo.
    echo -----------------------------------------------
    set strcat=%str1%%str2%
    echo strcat=%strcat%


    ::字符串截取,C中没有这种函数。
    ::实现方法:set 目标字符串=%源字符串:~起始值,截取长度%
    ::注意,起始值从0开始!
    ::截取长度是可选的,如果省略逗号和截取长度,将会从起始值一直截取到字符串的结尾。
    echo.
    echo -----------------------------------------------
    set strInterception1=%str1:~0,4%
    set strInterception2=%str1:~1,4%
    set strInterception3=%str1:~5%

    echo strInterception1=%strInterception1%
    echo strInterception2=%strInterception2%
    echo strInterception3=%strInterception3%

    ::类似strlen,取得字符串的长度。
    ::实现方法:利用goto和标签,形成循环结构,不断将字符串截短1字符,并用变量记录截短的次数,直到字符串变成空串
    echo.
    echo -----------------------------------------------
    set str=%str1%
    :next
    if not "%str%"=="" (
    set /a num+=1
    set "str=%str:~1%"
    goto next
    )
    echo "%str1%"字符串的长度为:%num%

    PAUSE

  • 相关阅读:
    C#串口通信程序实现无感知签到与答题
    C# 调用adb command 读取手机型号和IMEI
    sql 截取字符串
    .NET下的ORM框架有哪些
    linq-to-sql实现left join,group by,count
    C# 生成二维码
    数据库面试题.net
    .net面试中的一些常见问题与答案
    Jquery判断其中任意一个文本框的值是否被修改
    矩阵乘法的MPI并行计算
  • 原文地址:https://www.cnblogs.com/flay/p/5059259.html
Copyright © 2011-2022 走看看