C#获取字符串的长度
C#获取字符串的长度
作者:ylclass 来源:博客园 发布时间:2009-10-30 16:25 阅读:193 次 原文链接 [收藏]
1、 使用g.MeasureString()获得
使用MeasureString测量出来的字符宽度,总是比实际宽度大一些,而且随着字符的长度增大,貌似实际宽度和测量宽度的差距也越来越大了。查了一下MSDN,找到了下面这个理由:
MeasureString 方法旨在与个别字符串一起使用,它在字符串前后包括少量额外的空格供突出的标志符号使用。
string str;
str = "大";
Font f = new Font("SimSun", 7F, System.Drawing.FontStyle.Regular);
Graphics g = this.CreateGraphics();
//单位为mm
g.PageUnit = GraphicsUnit.Millimeter;
SizeF sim = g.MeasureString(str, f);
2、使用TextRenderer.MeasureText获得,提供使用指定尺寸创建文本初始边框时,使用指定的设备上下文、字体和格式说明所绘制的指定文本的尺寸(以像素为单位)。
private void MeasureText(PaintEventArgs e)
{
string str;
str = "大家好";
Font f = new Font("SimSun", 7F, System.Drawing.FontStyle.Regular);
Size sif = TextRenderer.MeasureText(e.Graphics, str, f, new Size(0, 0), TextFormatFlags.NoPadding);
M
相关文档:
在数据库编程中,常会遇到要把数据库表信息导入Excel中, 有时则是把Excel内容导入数据库中。在这里,将介绍一种比较方便快捷的方式,也是比较普遍的。其实,这方法你并不陌生。原理很简单,把数据库表或Excel内容读取到dataset类型的变量中,再逐 ......
今天做项目的时候,将null传入Oracle的表中,就是不成功
经过尝试得出了两个解决方案:
1.传入OracleDateTime.NULL
2.Nullable<DateTime> optime = DBNull.Value; 传入optime(开始网上找的答案是Nullable<DateTime> optime = null发现还是会报错) ......
需要引用的类名空间
using System.Security.Cryptography;
using System.IO;
using System.text;
/// <summary>
/// 加密
// ......
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
namespace 将枚举作为键值的形式存储
{
enum Myenum
{
First=3,
Second ......