填充包含重音符号的字符串 [英] Padding strings that contain accents

查看:104
本文介绍了填充包含重音符号的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在填充一个包含重音符号的字符串.但是,由于包含的口音,最终的字符串长度会更大.我想这与ASCII/Unicode的差异有关,但我还没有找出确切的原因.

I'm padding a string that contains accents in it. However, the final string length is larger due to the accents that it contains. I guess it has to do with ASCII / Unicode differences, but I haven't figured out the exact reason.

我已经尝试过: myString =圣何塞·德尔卡波(SanJosédel Cabo)"; myString.PadRight('',20);

I already tried: myString = "San José del Cabo"; myString.PadRight(' ', 20);

myString = "San José del Cabo";
string.format("{0,-20}", myString);  

实际结果是21个字符长.

And the actual result is 21 characters long.

C#中是否有一个内置方法考虑了字符串编码以返回正确的最终长度?

Is there a builtin method in C# that takes into account the string encoding to return the correct final length?

预先感谢

推荐答案

您也许可以从

You may be able to get the length that you're looking for out of StringInfo.LengthInTextElements. It would probably look something like:

var si = new StringInfo(myString);
int length = si.LengthInTextElements;

这篇关于填充包含重音符号的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆