用户输入一段字符串,比如csdn,lun tan后,正确的显示结果应该是csdn, lun tan(所有标点后面都自动加一个空格),可是这段程序显示出来的结果是csdn , lun tan,在标点前面也加了一个空格,没找出原因,请高人指点.MWHelper.cs
namespace MWConvert
{
    public class MWHelper
    {
        //常用的配置项
        public static int PicWidth = 12;
        public static int PicHeight = 31;
        public static int RowCount = 28;
        public static int ColumnCount = 28;        private static List<MWLetter> List;        /// <summary>
        /// 获取解析结果,转成一个一个的word
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static List<MWWord> GetResult(string str)
        {
            
            List<MWWord> rList = new List<MWWord>();
            rList.Add(new MWWord());
            if (List == null)
                LoadMWList();            //默认都是按照规则输入,如果需要规范用户输入,请在此先将str转换
            str = str.ToLower();
            //str = str.Replace(" ","*");            //开始匹配
            do
            {
                //最后的单词
                var lastWord = rList.LastOrDefault();//最后的单词
                if (str.StartsWith("\r\n"))
                {
                    //如果是回车,需要插入一个换行word
                    lastWord.NextLine = true;
                    rList.Add(new MWWord());
                    lastWord = rList.LastOrDefault();
                    str = str.Substring(2, str.Length - 2);                }
                //如果碰到的是空格另起一个word
                if (str.StartsWith(" "))
                {
                    
                    rList.Add(new MWWord());
                    str = str.Substring(1, str.Length - 1);
                    
                }
                else
                {
                    bool finded = false;
                    foreach (var item in List)
                    {
                        if (finded == false && str.StartsWith(item.Name))
                        {
                            if (item.IsMark)
                            {
                                //如果是标点的话,需要新起一个新word
                                rList.Add(new MWWord());
                                lastWord = rList.LastOrDefault();
                                lastWord.Letters.Add(item);
                                rList.Add(new MWWord());
                            }
                            else
                            {
                                lastWord.Letters.Add(item);
                            }
                            //寻找下一个字符串匹配
                            str = str.Substring(item.Name.Length, str.Length - item.Name.Length);                            finded = true;
                        }
                    }
                    ///没有找到对应的匹配字母
                    if (!string.IsNullOrEmpty(str) && finded == false)
                    {
                        //报告错误
                        throw new Exception("找不到匹配:" + str);
                        
                    }
                }
            } while (str.Length > 0);
            //最后是标点的话,给过滤掉
            rList.RemoveAll(o => o.Letters.Count < 1);
            return rList;
        }        /// <summary>
        /// 获取结果为图片列表
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static List<Bitmap> GetImageResult(string str)
        {
            var list = GetResult(str);
            string dir = Path.Combine(Application.StartupPath, "Letters");
            var baseImage = new Bitmap(Path.Combine(dir, "background.jpg"));            var column = 0;//列位移
            var row = 0;//行位移
            var index = 0;
            List<Bitmap> imgList = new List<Bitmap>();
            imgList.Add(new Bitmap(baseImage, MWHelper.PicWidth * ColumnCount, PicHeight * RowCount));            foreach (var word in list)
            {
                var g = Graphics.FromImage(imgList.LastOrDefault());                if (column + word.WordSize > ColumnCount || (index > 0 && list[index - 1].NextLine))
                {
                    //补充空格,换行
                    column = 0;
                    row++;
                    //如果行数超过限制,另起一个图片
                    if (row >= RowCount)
                    {
                        imgList.Add(new Bitmap(baseImage, MWHelper.PicWidth * ColumnCount, PicHeight * RowCount));
                        g = Graphics.FromImage(imgList.LastOrDefault());
                        row = 0;
                    }
                }
                foreach (var letter in word.Letters)
                {
                    var m = new Bitmap(Path.Combine(dir, letter.PicName + ".gif"));                    g.DrawImage(m, column * PicWidth, row * PicHeight, PicWidth * letter.PicSize, PicHeight);
                    column = column + letter.PicSize;
                }
                //所有单词和标点都补空格(如果有逻辑变动再改)
                if (column < ColumnCount)
                {
                    var mm = new Bitmap(Path.Combine(dir, "空格.gif"));
                    g.DrawImage(mm, column * PicWidth, row * PicHeight, PicWidth * 1, PicHeight);
                    column = column + 1;
                }
                index++;
            }
            return imgList;
        }        /// <summary>
        /// 加载配置
        /// </summary>
        private static void LoadMWList()
        {
            List = new List<MWLetter>();
            var xml = XElement.Load("MWConfig.xml");
            var letters = from c in xml.Element("MWLetters").Elements("MWLetter")
                          select c;
            foreach (var letter in letters)
            {
                MWLetter l = new MWLetter();
                l.Name = letter.Attribute("Name").Value;
                l.PicName = letter.Attribute("PicName").Value;
                l.PicSize = int.Parse(letter.Attribute("PicSize").Value);
                l.IsMark = bool.Parse(letter.Attribute("IsMark").Value);
                List.Add(l);
            }            var config = xml.Element("MWHelperConfig");
            PicWidth = int.Parse(config.Attribute("PicWidth").Value);
            PicHeight = int.Parse(config.Attribute("PicHeight").Value);
            RowCount = int.Parse(config.Attribute("RowCount").Value);
            ColumnCount = int.Parse(config.Attribute("ColumnCount").Value);
        }
    }
}

解决方案 »

  1.   

    MWWord.cs
    public class MWWord
        {
            public MWWord()
            {
                Letters = new List<MWLetter>();
            }        /// <summary>
            /// 包括字母列表
            /// </summary>
            public List<MWLetter> Letters { get; set; }        /// <summary>
            /// 整个单词的图片占用大小
            /// </summary>
            public int WordSize
            {
                get
                {
                    return Letters.Sum(o => o.PicSize);
                }
            }        //是否需要添加空格
            public bool AddSpace
            {
                get
                {
                    return Letters.Any(o => o.IsMark == true);
                }
            }        /// <summary>
            /// 是否从此开始换行
            /// </summary>
            public bool NextLine { get; set; }
        }
    ********************************************
    MWConfig.XML<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
    <MWConfig>
      <MWHelperConfig PicWidth="12" PicHeight="31" RowCount="28" ColumnCount="28"></MWHelperConfig>
      <MWLetters>
        <MWLetter Name="iow" PicName="iow" PicSize="1" IsMark="false"/>
        <MWLetter Name="ian" PicName="ian" PicSize="1" IsMark="false"/>
        <MWLetter Name="iaw" PicName="iaw" PicSize="1" IsMark="false"/>
        <MWLetter Name="uai" PicName="uai" PicSize="1" IsMark="false"/>
        <MWLetter Name="uan" PicName="uan" PicSize="1" IsMark="false"/>
        <MWLetter Name="uaw" PicName="uaw" PicSize="1" IsMark="false"/>
        <MWLetter Name="van" PicName="van" PicSize="1" IsMark="false"/>
        <MWLetter Name="zh" PicName="zh" PicSize="1" IsMark="false"/>
        <MWLetter Name="ch" PicName="ch" PicSize="1" IsMark="false"/>
        <MWLetter Name="sh" PicName="sh" PicSize="1" IsMark="false"/>
        <MWLetter Name="ai" PicName="ai" PicSize="1" IsMark="false"/>
        <MWLetter Name="an" PicName="an" PicSize="1" IsMark="false"/>
        <MWLetter Name="aw" PicName="aw" PicSize="1" IsMark="false"/>
        <MWLetter Name="ei" PicName="ei" PicSize="1" IsMark="false"/>
        <MWLetter Name="ou" PicName="ou" PicSize="1" IsMark="false"/>
        <MWLetter Name="ow" PicName="ow" PicSize="1" IsMark="false"/>
        <MWLetter Name="ia" PicName="ia" PicSize="1" IsMark="false"/>
        <MWLetter Name="ie" PicName="ie" PicSize="1" IsMark="false"/>
        <MWLetter Name="io" PicName="io" PicSize="1" IsMark="false"/>
        <MWLetter Name="iu" PicName="iu" PicSize="1" IsMark="false"/>
        <MWLetter Name="in" PicName="in" PicSize="1" IsMark="false"/>
        <MWLetter Name="ua" PicName="ua" PicSize="1" IsMark="false"/>
        <MWLetter Name="ue" PicName="ue" PicSize="1" IsMark="false"/>
        <MWLetter Name="ui" PicName="ui" PicSize="1" IsMark="false"/>
        <MWLetter Name="un" PicName="un" PicSize="1" IsMark="false"/>
        <MWLetter Name="uw" PicName="uw" PicSize="1" IsMark="false"/>
        <MWLetter Name="ve" PicName="ve" PicSize="1" IsMark="false"/>
        <MWLetter Name="vn" PicName="vn" PicSize="1" IsMark="false"/>
        <MWLetter Name="'" PicName="'" PicSize="1" IsMark="false"/>
        <MWLetter Name="0" PicName="0" PicSize="2" IsMark="false"/>
        <MWLetter Name="1" PicName="1" PicSize="2" IsMark="false"/>
        <MWLetter Name="2" PicName="2" PicSize="2" IsMark="false"/>
        <MWLetter Name="3" PicName="3" PicSize="2" IsMark="false"/>
        <MWLetter Name="4" PicName="4" PicSize="2" IsMark="false"/>
        <MWLetter Name="5" PicName="5" PicSize="2" IsMark="false"/>
        <MWLetter Name="6" PicName="6" PicSize="2" IsMark="false"/>
        <MWLetter Name="7" PicName="7" PicSize="2" IsMark="false"/>
        <MWLetter Name="8" PicName="8" PicSize="2" IsMark="false"/>
        <MWLetter Name="9" PicName="9" PicSize="2" IsMark="false"/>
        <MWLetter Name="b" PicName="b" PicSize="1" IsMark="false"/>
        <MWLetter Name="p" PicName="p" PicSize="1" IsMark="false"/>
        <MWLetter Name="m" PicName="m" PicSize="1" IsMark="false"/>
        <MWLetter Name="f" PicName="f" PicSize="1" IsMark="false"/>
        <MWLetter Name="d" PicName="d" PicSize="1" IsMark="false"/>
        <MWLetter Name="t" PicName="t" PicSize="1" IsMark="false"/>
        <MWLetter Name="y" PicName="y" PicSize="1" IsMark="false"/>
        <MWLetter Name="l" PicName="l" PicSize="1" IsMark="false"/>
        <MWLetter Name="g" PicName="g" PicSize="1" IsMark="false"/>
        <MWLetter Name="k" PicName="k" PicSize="1" IsMark="false"/>
        <MWLetter Name="h" PicName="h" PicSize="1" IsMark="false"/>
        <MWLetter Name="j" PicName="j" PicSize="1" IsMark="false"/>
        <MWLetter Name="q" PicName="q" PicSize="1" IsMark="false"/>
        <MWLetter Name="x" PicName="x" PicSize="1" IsMark="false"/>
        <MWLetter Name="r" PicName="r" PicSize="1" IsMark="false"/>
        <MWLetter Name="z" PicName="z" PicSize="1" IsMark="false"/>
        <MWLetter Name="c" PicName="c" PicSize="1" IsMark="false"/>
        <MWLetter Name="i" PicName="i" PicSize="1" IsMark="false"/>
        <MWLetter Name="s" PicName="s" PicSize="1" IsMark="false"/>
        <MWLetter Name="a" PicName="a" PicSize="1" IsMark="false"/>
        <MWLetter Name="e" PicName="e" PicSize="1" IsMark="false"/>
        <MWLetter Name="o" PicName="o" PicSize="1" IsMark="false"/>
        <MWLetter Name="n" PicName="n" PicSize="1" IsMark="false"/>
        
        <MWLetter Name="u" PicName="u" PicSize="1" IsMark="false"/>
        <MWLetter Name="v" PicName="v" PicSize="1" IsMark="false"/>
        <MWLetter Name="w" PicName="w" PicSize="1" IsMark="false"/>
        <MWLetter Name=" " PicName="空格" PicSize="1" IsMark="true"/>
        
        <MWLetter Name="," PicName="逗号" PicSize="1" IsMark="true"/>
        <MWLetter Name=";" PicName="分号" PicSize="1" IsMark="true"/>
        <MWLetter Name="!" PicName="感叹号" PicSize="2" IsMark="true"/>
        <MWLetter Name=". " PicName="句号" PicSize="2" IsMark="true"/>
        
        <MWLetter Name="(" PicName="左括号" PicSize="2" IsMark="true"/>
        <MWLetter Name=")" PicName="右括号" PicSize="2" IsMark="true"/>
        <MWLetter Name="&quot;" PicName="左引号" PicSize="1" IsMark="true"/>
        <MWLetter Name="&quot;" PicName="右引号" PicSize="1" IsMark="true"/>
        <MWLetter Name=":" PicName="冒号" PicSize="1" IsMark="true"/>
        
        <MWLetter Name="..." PicName="省略号" PicSize="3" IsMark="true"/>
        <MWLetter Name="《" PicName="左书名号" PicSize="2" IsMark="true"/>
        <MWLetter Name="》" PicName="右书名号" PicSize="2" IsMark="true"/>
        <MWLetter Name="?" PicName="问号" PicSize="2" IsMark="true"/>
        <MWLetter Name="、" PicName="顿号" PicSize="1" IsMark="true"/>
      </MWLetters>
    </MWConfig>
      

  2.   

    MWLetter又是啥东东阿??????
      

  3.   

    把你出问题的地方标出来,别人也好有个头绪,编码要养成好习惯。
    如果有空格就trim()下吧
      

  4.   

    主要功能在MWHelper.cs 里,我在这句加上if (column < ColumnCount && word.AddSpace)后,就变成csdn, luntan了,原本用户输入的空格没有了
    public class MWLetter
        { 
            /// <summary>
            /// 用户输入字母
            /// </summary>
            public string Name { get; set; }        /// <summary>
            /// 图片名
            /// </summary>
            public string PicName { get; set; }        /// <summary>
            /// 转成图片的大小
            /// </summary>
            public int PicSize{ get; set; }        /// <summary>
            /// 是否标点符号,数字,字母,和'分隔符不是标点
            /// </summary>
            public bool IsMark { get; set; }    }
      

  5.   

    引用LZ
    用户输入一段字符串,比如csdn,lun tan后,正确的显示结果应该是csdn, lun tan(所有标点后面都自动加一个空格),可是这段程序显示出来的结果是csdn , lun tan,在标点前面也加了一个空格,没找出原因,请高人指点. 
    代码太长了   你可以先试着打个断点跟踪下 然后那具体不明白的
    再提出来  大家可以帮你.....