C# 字符串按数组截取方法(C/S)

 private void button1_Click(object sender, EventArgs e) {
            string[] aa = this.GetYouhouComment(“aaa王ああああああddddd”, 4);

        }

        /// <param name="PValue">元文字列</param>
        /// <param name="PLenth">桁数</param>
        /// <returns>取得する結果</returns>
        private string[] GetYouhouComment(string PValue, int PLenth) {

            string[] result = new string[5];
            string shiteiResult = PValue.Replace(",", string.Empty);
            // 文字列へ内容を転換処理を行う。
            int byteCount = Encoding.GetEncoding(932).GetByteCount(shiteiResult);

            int j = 0;

            // 指定する桁数>=文字列の桁数の場合
            if (PLenth >= byteCount) {
                shiteiResult = string.Empty.PadRight(PLenth - byteCount) + shiteiResult;
                result[j] = shiteiResult;
                // 以外の場合
            } else {
                string strValue = string.Empty;
                string strCompare = string.Empty;

                // 指定する桁数な文字列を取得する。
                for (int i = 0; i < shiteiResult.Length; i++) {
                    strCompare = shiteiResult.Substring(i, 1);

                    // GetEncodingメソッドの引数'932'がSJISを示します。
                    if (Encoding.GetEncoding(932).GetByteCount(strValue) +
                        Encoding.GetEncoding(932).GetByteCount(strCompare) > PLenth) {
                        if (j < 5) {
                            result[j] = strValue;
                            strValue = strCompare;
                            j = j + 1;
                        } else {
                            break;
                        }
                    } else {
                        strValue = strValue + shiteiResult.Substring(i, 1);
                        if (i == shiteiResult.Length - 1) {
                            result[j] = strValue;
                        }
                    }
                }
            }

            return result;
        }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值