字符串(含全角和半角)的分割

本文介绍了一个用于分割字符串的VB.NET函数SplitString。该函数允许用户指定起始位置和长度来从输入字符串中提取子串。文章提供了函数的具体实现及示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

例を挙げる、A="1あ2い3う4え5お"
 
SplitString(A,1,4)="1あ2"
SplitString(A,1,6)="1あ2い"
 
    ''' -----------------------------------------------------------------------------
    ''' <summary>
    ''' 入力文字列分割
    ''' </summary>
    ''' <param name="str">入力文字列</param>
    ''' <param name="startIndex">ターゲット内の、テキスト置換を開始する文字の位置です。Start は 1 から始まるインデックスです。</param>
    ''' <param name="len">置き換える文字数を指定します。省略すると、すべての文字列が使用されます。 </param>
    ''' <returns></returns>
    ''' <remarks>
    ''' </remarks>
    ''' <history>
    '''  [renzhijia] 2007/07/04 Created
    ''' </history>
    ''' -----------------------------------------------------------------------------
    Private Function SplitString(ByVal str As String, ByVal startIndex As Integer, ByVal len As Integer) As String
 
        Dim pos As Integer                      '現在の文字の位置
        Dim encoding As System.Text.Encoding
        Dim strValue As String                  'スプレッドで表示される内容
        Dim intStartKeta As Integer             'ファイルに表示される内容の開始位置
        Dim splitted As ArrayList               '分割された文字列
        Dim bytes() As Byte
        Dim newbytes() As Byte
        Dim intBytes As Integer
        Dim enc As System.Text.Encoding
        enc = encoding.GetEncoding("SHIFT_JIS")
        splitted = New ArrayList
        intStartKeta = 0
        bytes = enc.GetBytes(str)
        strValue = ""
 
        Try
            If startIndex = 0 Then
                startIndex = 1
            End If
            If bytes.Length < len Then
                len = bytes.Length - startIndex + 1
            End If
            ReDim newbytes(len - 1)
 
            For intBytes = 0 To len - 1
                newbytes(intBytes) = bytes(startIndex - 1 + intBytes)
            Next
            strValue = enc.GetString(newbytes)
 
        Catch ex As Exception
            strValue = ""
        Finally
        End Try
        ' 文字列型配列として値を返す
        Return strValue
    End Function
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值