Csharp/C#教程:检测字符串中的CJK字符(C#)分享


检测字符串中的CJK字符(C#)

我正在使用iTextSharp生成一系列PDF,使用Open Sans作为默认字体。 有时,名称会插入到PDF的内容中。 但是我的问题是我需要插入的一些名称包含CJK字符(存储在SQL Server中的nvarchar列中),据我所知,Open Sans目前不支持CJK字符。 我需要继续使用Open Sans作为我的默认字体,所以理想情况下我想尝试检测从数据库中抓取的字符串中的CJK字符,并在打印出这些字符时切换到CJK字体。

正则表达式是最好的选择吗? 不幸的是,我无法找到任何有助于此的正则表达式模式。

在此先感谢您的帮助!

使用iTextSharp.text.pdf.FontSelector;

iTextSharp.text.pdf.FontSelector selector = new iTextSharp.text.pdf.FontSelector(); // add 2 type of font to FontSelector selector.AddFont(openSansfont); selector.AddFont(chinesefont); iTextSharp.text.Phrase phrase = selector.Process(yourTxt); 

FontSelector将为您使用正确的字体!

源文件FontSelector.cs的详细描述。

选择包含正确呈现文本所需的字形的相应字体。 按顺序检查字体,直到找到该字符。

我忘了先搜索哪个订单! 请体验一下! 编辑:顺序是从第一个addFont到最后一个addFont。

https://itextpdf.com/examples/iia.php?id=214

只是想让任何人在这个问题上遇到麻烦,我找到了另一个使用正则表达式中列出的unicode块( https://msdn.microsoft.com/en-us/library/20bw873z.aspx#SupportedNamedBlocks )的解决方案。

 var Name = "Joe Bloggs"; var Regex = new Regex(@"p{IsCJKUnifiedIdeographs}"); if(Regex.IsMatch(Name)) { //switch to CJK font } else { //keep calm and carry on } 

编辑:

您可能需要匹配的不仅仅是统一表意文字,请尝试使用此作为正则表达式:

 string r = @"p{IsHangulJamo}|"+ @"p{IsCJKRadicalsSupplement}|"+ @"p{IsCJKSymbolsandPunctuation}|"+ @"p{IsEnclosedCJKLettersandMonths}|"+ @"p{IsCJKCompatibility}|"+ @"p{IsCJKUnifiedIdeographsExtensionA}|"+ @"p{IsCJKUnifiedIdeographs}|"+ @"p{IsHangulSyllables}|"+ @"p{IsCJKCompatibilityForms}"; 

这适用于我试过的所有韩文文本。

好吧,我做了编辑daves回答让它工作,但显然只有我可以看到,直到其同行审查所以我将发布解决方案作为我自己的答案。 基本上戴夫只需要将他的正则表达式扩展到这个:

 string regex = @"p{IsHangulJamo}|"+ @"p{IsCJKRadicalsSupplement}|"+ @"p{IsCJKSymbolsandPunctuation}|"+ @"p{IsEnclosedCJKLettersandMonths}|"+ @"p{IsCJKCompatibility}|"+ @"p{IsCJKUnifiedIdeographsExtensionA}|"+ @"p{IsCJKUnifiedIdeographs}|"+ @"p{IsHangulSyllables}|"+ @"p{IsCJKCompatibilityForms}"; 

这样使用时会检测韩文字符:

上述就是C#学习教程:检测字符串中的CJK字符(C#)分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!

 string subject = "도형이"; Match match = Regex.Match(subject, regex); if(match.Success) { //change to Korean font } else { //keep calm and carry on { 

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/cdevelopment/1027535.html

(0)
上一篇 2022年1月10日
下一篇 2022年1月10日

精彩推荐