2021年3月15日月曜日

C# SHIFT-JISで指定バイトで文字列を分割、分割時に最後の文字が漢字の左半分なら文字化けしないように全角で分割を行う

ある意味、力業

UniCodeからshift-jisに文字変換して指定バイトで文字列を分割しますが

分割の最後が全角の判定を行い、文字化けしないようにする。

 

using System;
using System.Collections.Generic;
using System.Text;

             var vList = new List<string>();
            var wordList = text.Split(new[] { "\r\n", "\n", "\r" }, StringSplitOptions.None);
            int vLoop, vStart, vLength;
            int vSize = 101;
            byte[] btBytes = null;
            string strTemp = null;
            bool vZenkaku;


            Encoding hEncoding = Encoding.GetEncoding("Shift_JIS");

            foreach (var item in wordList) {

                btBytes = hEncoding.GetBytes(item);
                vLength = btBytes.Length;

                if (vLength > vSize) {
                    vLoop = (vLength + vSize) / vSize;
                    vStart = 0;
                    for (int j = 0; j < vLoop; j++) {

                        if ((vStart + vSize) < vLength) {

                            //最後の文字が漢字の左半分の場合、切出の先頭は泣き別れになる
                            vZenkaku = is_zenkaku(btBytes[vStart + vSize - 1]);
                            if (vZenkaku) {
                                strTemp = hEncoding.GetString(btBytes, vStart, (vSize - 1));
                                vStart = vStart + vSize - 1;
                            }
                            else {
                                strTemp = hEncoding.GetString(btBytes, vStart, vSize);
                                vStart = vStart + vSize;
                            }

                            vList.Add(strTemp);
                        }
                        else {
                            //vList.Add(item.Substring(vStart, (item.Length - vStart)));
                            vList.Add(hEncoding.GetString(btBytes, vStart, (vLength - vStart)));
                        }
                    }
                }
                else {
                    vList.Add(item);
                }
            }

            foreach (var rec in vList) {
                Console.WriteLine(rec);
            }
        }

        // 全角判定(シフトJIS)
        static private bool is_zenkaku(byte c) {
            if ((c >= 0x81 && c <= 0x9f) || (c >= 0xe0 && c <= 0xfc)) {    // 全角の第1バイト
                return true;
            }
            return false;
        }

Shit-JIS 全角判定

 

全角判定

// 全角判定(シフトJIS)
is_zenkaku(char *s){
    unsigned char c = s[0];
    if ( (c>=0x81 && c<=0x9f) || (c>=0xe0 && c<=0xfc)){    // 全角の第1バイト
        c=s[1];
        if (c>=0x40 && c<=0xfc) return 1;    // 全角の第2バイト
    }
    return 0;
}


2021年3月13日土曜日

ASP.NET MVC RAZORでdesableにする方法

 ASP.NET MVC RAZORでdesableにする方法

 @Html.EditorFor(m => m.タイトル, new { htmlAttributes = new { id = "SPT_Title",
            @class = "SPT_Title", @disabled = "true" } }) 

@Html.TextAreaFor(m => m.問合せ内容,    // プロパティ
                10,                       // 行数
                600,                      // 桁数
                new { id = "SPT_Body", @class = "SPT_Body", @disabled = "true" }
            )

2021年2月25日木曜日

dapper で identityを取得する方法 INSERT OUTPUT句を使用する

dapper で identityを取得する方法 INSERT OUTPUT句を使用する

string sql = @"insert into aaa (notes)
OUTPUT INSERTED.[Id]
values (@notes)";

var id = conn.Query<int>(sql, new { notes = "aaaaaaaaaaaa" }).Single();

2020年11月13日金曜日

ASP.NET MVC 入力filter

 

半角カタカナ

[RegularExpression(@"[。-゚+]+", ErrorMessage = "半角カタカナのみ入力できます。")]
public string HalfKatakana { get; set; }

全角カタカナ

[RegularExpression(@"[ァ-ヶ]+", ErrorMessage = "全角カタカナのみ入力できます。")]
public string FullKatakana { get; set; }

2020年10月29日木曜日

複数入力のEditの印刷で行数を指定して分割を行う

複数入力のEditの印刷で行数を指定して分割を行う

背景:複数行入力の印刷で印刷枠を超える行数を入力される場合

入力値を全て印刷しない現象が発生する。

上記の印刷されていない 部分を印刷する為、出力行数を超えた場合

2分割を行う処理を行う。

 

public static System.Boolean funcStringSplit(System.String iStr, System.Int32 iLine, ref System.String oStr1, ref System.String oStr2)
    {
        bool vRet = false;
        oStr1 = "";
        oStr2 = "";
       
        //改行で分解
        var lines = iStr.Split(new[]{"\r\n","\n","\r"},StringSplitOptions.None);
       
        if (lines.Length > iLine) {
            for (int i = 0; i < iLine; i++) { oStr1 = oStr1 + lines[i] + System.Environment.NewLine; };
            for (int j = iLine; j < lines.Length; j++) { oStr2 = oStr2 + lines[j] + System.Environment.NewLine; };
            vRet = true;
        }
        else {
            oStr1 = iStr;
            oStr2 = "";
            vRet = false;
        }
       
        return vRet;
    }
}