準備
(なし)
デザイン
- フォーム (Form1) にボタン (button1) を配置します。
- フォーム (Form1) にリストボックス (listBox1) を配置します。
サンプルコード (C#)
using System.Text.RegularExpressions;
namespace WinFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
string s1 = "とうきょうと";
listBox1.Items.Add(Regex.IsMatch(s1, @"^\p{IsHiragana}*$"));
string s2 = "トウキョウト";
listBox1.Items.Add(Regex.IsMatch(s2, @"^\p{IsKatakana}*$"));
string s3 = "東京都";
listBox1.Items.Add(Regex.IsMatch(s3, @"^\p{IsCJKUnifiedIdeographs}*$"));
}
}
}
解説
正規表現で定義されている日本語の名前によって文字列の日本語チェックを行っています。日本語の名前の定義は、ひらがな / カタカナ / 漢字の 3 つに分かれています。チェック対象となる文字列の文字すべてが、ひらがな / カタカナ / 漢字のいずれかで構成されている場合にチェック可能です。
結果
動作確認環境
Visual Studio 2022 Professional (.NET 7 C#11)
ログ
初版:2016.04.10 Visual Studio 2015 Professional (C# 6.0)
