| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 513 人关注过本帖
标题:想问一个字符串输出的问题
只看楼主 加入收藏
dzxxcxj
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2011-5-6
结帖率:0
收藏
 问题点数:0 回复次数:3 
想问一个字符串输出的问题
假设有这么一个
string words=“关于朝核问题:双方一致认为,朝鲜半岛核问题只能在六方会谈框架内通过政治外交方式解决,重申愿相互并同六方会谈其他各方继续密切协作,在恪守2005”,可以用什么方法,对他进行处理!形成类似下面的


     关于朝核问题:双方一致认为,朝鲜半岛核问题只能在六方会谈框架内通过政治外交方式解决,重申愿相互并同六方会谈其他各方继续密切协
                   作,在恪守2005
总之让后面的字符,不要顶格输出!
搜索更多相关主题的帖子: 朝鲜半岛 字符串 
2011-06-17 14:54
xydddaxia
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:贵宾
威 望:33
帖 子:466
专家分:2307
注 册:2009-3-20
收藏
得分:0 
输出的控件的宽度是固定的才好办

站在春哥的肩膀上
2011-06-18 08:58
dzxxcxj
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2011-5-6
收藏
得分:0 
以下是引用xydddaxia在2011-6-18 08:58:58的发言:

输出的控件的宽度是固定的才好办
我是想,可不可以先通过判断,找出冒号前面部分的内容,然后对后面的字符串进行左对齐,这样可以吗?
2011-06-19 21:48
xydddaxia
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:贵宾
威 望:33
帖 子:466
专家分:2307
注 册:2009-3-20
收藏
得分:0 
程序代码:
 /// <summary>
        /// 格式化字符串
        /// </summary>
        /// <param name="str">要处理的字符串</param>
        /// <param name="control">输出控件</param>
        /// <returns></returns>
        public string FormatStr(string str, System.Windows.Forms.Control control)
        {
            try
            {
                if (string.IsNullOrEmpty(str))
                {
                    return str;
                }
                if (!str.Contains(""))
                {
                    return str;
                }
                string leftStr = str.Substring(0, str.IndexOf('') + 1);
                Graphics g = control.CreateGraphics();
                Font font = control.Font;
                int leftWidth = (int)g.MeasureString(leftStr, font).Width;
                if (leftWidth > control.Width)
                {
                    return str;
                }
                int RigthWidth = control.Width - leftWidth;
                string otherStr = str.Substring(str.IndexOf('') + 1);

                int nowindex = 0;
                int startindex = 0;
                int lastindex = 0;
                List<string> strlist = new List<string>();
                while (nowindex < otherStr.Length)
                {
                    if (g.MeasureString(otherStr.Substring(startindex, nowindex - startindex), font).Width + g.MeasureString("", font).Width / 2 > RigthWidth)
                    {
                        strlist.Add(otherStr.Substring(startindex, nowindex - startindex) + "\r\n");
                        startindex = nowindex;
                        lastindex = nowindex;
                        nowindex--;
                    }
                    nowindex++;
                }
                strlist.Add(otherStr.Substring(lastindex));

                string resultStr = leftStr;
                string emptyStr = "";
                while (emptyStr.Length < leftStr.Length)
                {
                    emptyStr += " ";
                }
                if (strlist.Count > 0)
                {
                    resultStr += strlist[0];
                    for (int i = 1; i < strlist.Count; i++)
                    {
                        resultStr += emptyStr + strlist[i];
                    }
                }
                return resultStr;
            }
            catch
            {
                throw new Exception("格式化字符串错误.");
            }
        }

站在春哥的肩膀上
2011-06-20 10:10
快速回复:想问一个字符串输出的问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.016844 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved