Q: 字符串all存放了一个字符串,当使用某种字体时,希望显示字符串前面100像素长度的子串
A:
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code
string all="aakdjhadskjkjhkjhkfdshifjhskdjhfakjfhksahfafdhkjhfs";
int pix = 100;
Font font = new Font("",12); //字体12像素
for(int i=0;;i++)
{
string s = all.Substring(0, all.Length-1-i);
if(Graphics.MeasureString(s,font).Width <pix)
break;
}
all = all.Substring(0, all.Length-1-i);