StringHelper

出自 ProgWiki
前往: 導覽搜尋

StringHelper,用於字串處理的相關功能

應用

C#

using System;
using System.Collections.Generic;
using System.Text;
 
class StringHelper
{
 
    public static int GetWidth(string contxt) 
    {
        int len = 0;
        for (int i = 0; i < contxt.Length; i++)
        {
            byte[] byte_len = Encoding.Default.GetBytes(contxt.Substring(i, 1));
            if (byte_len.Length > 1)
                len += 2;
            else
                len += 1;
            i += 1;
        }
        return len;
    }
 
 
    /// <summary>
    /// 依長度自動換行
    /// </summary>
    /// <param name="contxt"></param>
    /// <returns></returns>
    public static string FixedWidthToNewline(string contxt, int length)
    {
        StringBuilder result = new StringBuilder();
 
        int len = 0;
        int star = 0;
        int templen = 0;
        for (int i = 0; i < contxt.Length; i++)
        {
            byte[] byte_len = Encoding.Default.GetBytes(contxt.Substring(i, 1));
            if (byte_len.Length > 1)
                len += 2;
            else
                len += 1;
 
            if (len > length)
            {
                templen = i - star;
                result.Append(contxt.Substring(star, templen) + "\r\n");
                star = i;
                len = 0;
            }
            else if (i == contxt.Length - 1)
            {
                result.Append(contxt.Substring(star) + "\r\n");
                star = i;
                len = 0;
            }
 
        }
 
        return result.ToString();
    }
 
    /// <summary>
    /// 依長度自動切割字串到字串陣列
    /// </summary>
    /// <param name="contxt"></param>
    /// <param name="length"></param>
    /// <returns></returns>
    public static string[] FixedWidthToSplit(string contxt, int length)
    {
        if (string.IsNullOrEmpty(contxt))
            return null;
 
        List<string> result = new List<string>();
        StringBuilder temp = new StringBuilder();
 
        int len = 0;
        int star = 0;
        int templen = 0;
        for (int i = 0; i < contxt.Length; i++)
        {
            byte[] byte_len = Encoding.Default.GetBytes(contxt.Substring(i, 1));
            if (byte_len.Length > 1)
                len += 2;
            else
                len += 1;
 
            if (len > length)
            {
                templen = i - star;
                result.Add(contxt.Substring(star, templen));
                star = i;
                len = 0;
            }
            else if (i == contxt.Length - 1)
            {
                result.Add(contxt.Substring(star));
                star = i;
                len = 0;
            }
        }
 
        return result.ToArray();
    }
}

VB.NET

Imports System
Imports System.Text
Imports System.Collections.Generic
 
 
''' <summary>
''' 字串處理用
''' </summary>
''' <remarks></remarks>
Public Class StringHelper
 
    Public Shared Function GetWidth(ByVal contxt As String) As Integer
        Dim len As Integer = 0
        Dim i As Integer = 0
        Do While (i < contxt.Length)
            Dim byte_len() As Byte = Encoding.Default.GetBytes(contxt.Substring(i, 1))
            If (byte_len.Length > 1) Then
                len += 2
            Else
                len += 1
            End If
            i += 1
        Loop
        Return len
    End Function
 
    Public Shared Function FixedWidthToNewline(ByVal contxt As String, ByVal length As Integer) As String
        Dim result As StringBuilder = New StringBuilder
        Dim len As Integer = 0
        Dim star As Integer = 0
        Dim templen As Integer = 0
        Dim i As Integer = 0
        Do While (i < contxt.Length)
            Dim byte_len() As Byte = Encoding.Default.GetBytes(contxt.Substring(i, 1))
            If (byte_len.Length > 1) Then
                len += 2
            Else
                len += 1
            End If
            If (len >= length) Then
                templen = (i - star)
                result.Append(contxt.Substring(star, templen)).Append(vbCrLf)
                star = i
                len = 0
            ElseIf (i = (contxt.Length - 1)) Then
                result.Append(contxt.Substring(star)).Append(vbCrLf)
                star = i
                len = 0
            End If
            i += 1
        Loop
        Return result.ToString
    End Function
 
    Public Shared Function FixedWidthToSplit(ByVal contxt As String, ByVal length As Integer) As String()
        If String.IsNullOrEmpty(contxt) Then
            Return Nothing
        End If
 
        Dim result As List(Of String) = New List(Of String)
        Dim temp As StringBuilder = New StringBuilder
        Dim len As Integer = 0
        Dim star As Integer = 0
        Dim templen As Integer = 0
        Dim i As Integer = 0
        Do While (i < contxt.Length)
            Dim byte_len() As Byte = Encoding.Default.GetBytes(contxt.Substring(i, 1))
            If (byte_len.Length > 1) Then
                len += 2
            Else
                len += 1
            End If
            If (len >= length) Then
                templen = (i - star)
                result.Add(contxt.Substring(star, templen))
                star = i
                len = 0
            ElseIf (i = (contxt.Length - 1)) Then
                result.Add(contxt.Substring(star))
                star = i
                len = 0
            End If
            i += 1
        Loop
        Return result.ToArray
    End Function
End Class