在特定字符后替换char数

时间:2012-09-11 07:32:39

标签: c# string replace braille

我正在创建一个将文本转换为盲文的应用程序。转换为盲文不是问题,但我不知道如何将其转换回来。

示例1:将数字转换为盲文

1     = #a
123   = #abc
12 45 = #ab #de

示例2:将大写字母转换为盲文

Jonas = ,jonas
JONAS = ,,jonas

我在将盲文转换回正常方面遇到了问题。我无法将每个a转换为1,依此类推。可以通过#检查数字,然后将其后的字符更改为下一个空格,但我不知道如何。字母前面的逗号很难与文中的其他逗号分开。

以下是我转换为盲文的课程:

using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;

namespace BrailleConverter
{
    class convertingBraille
    {
        public Font getIndexBrailleFont()
        {
            return new Font("Index Braille Font", (float)28.5, FontStyle.Regular);
        }

        public Font getPrintableFontToEmbosser()
        {
            return new Font("Lucida Console", (float)28.5, FontStyle.Regular);
            //return new Font("Index Black Text Font", (float)28.5, FontStyle.Regular);
        }

        public string convertCapitalsToUnderscore(string text)
        {
            if (string.IsNullOrEmpty(text))
            {
                return "";
            }

            text = " " + text;

            text = text.Replace('.', '\'');
            text = text.Replace(',', '1');
            text = text.Replace('?', '5');
            text = text.Replace('!', '6');
            text = text.Replace(':', '3');
            text = text.Replace('=', '7');
            text = text.Replace('+', '4');
            text = text.Replace('*', '9');
            text = text.Replace('é', '=');

            StringBuilder newText = new StringBuilder(text.Length * 2);
            newText.Append(text[0]);

            bool firstCapLetterInWord = true;

            for (int i = 1; i < text.Length; i++)
            {
                char letter = text[i]; // Aktuell bokstav
                char nextLetter = ' '; // Nästa bokstav

                try
                {
                    nextLetter = text[i + 1];
                }
                catch
                {

                }

                // Är det stor bokstav?
                if (char.IsUpper(letter))
                {
                    // Är nästa bokstav stor?
                    if (char.IsUpper(nextLetter))
                    {
                        // Är det början av ett helt ord med caps?
                        if (firstCapLetterInWord)
                        {
                            newText.Append(",,"); // 2 st understräck framför ordet

                            firstCapLetterInWord = false; // Ändra så att inte nästa bokstav får 2 st understräck
                        }
                    }
                    else // Annars bara ett understräck
                    {
                        if (firstCapLetterInWord)
                        {
                            newText.Append(","); // Sätt understräck framför bokstav
                        }

                        firstCapLetterInWord = true; // Förbereda för nästa capsord
                    }
                }

                newText.Append(text[i]);
            }

            string finishedText = newText.ToString().TrimStart(); // Ta bort mellanslaget i början

            finishedText = finishedText.ToLower();

            finishedText = finishedText.Replace('å', '*');
            finishedText = finishedText.Replace('ä', '>');
            finishedText = finishedText.Replace('ö', '[');

            return finishedText;
        }

        public string convertNumbersToBrailleNumbers(string text)
        {
            if (string.IsNullOrEmpty(text))
            {
                return "";
            }

            text = " " + text;

            StringBuilder newText = new StringBuilder(text.Length * 2);
            newText.Append(text[0]);

            bool firstNumberInNumber = true;

            for (int i = 1; i < text.Length; i++)
            {
                char letter = text[i]; // Aktuell tecken
                char nextLetter = ' '; // Nästa tecken

                try
                {
                    nextLetter = text[i + 1];
                }
                catch
                {

                }

                char convertedChar = text[i];

                // Är tecknet en siffra?
                if (char.IsNumber(letter))
                {
                    // Är nästa tecken en siffra?
                    if (char.IsNumber(nextLetter))
                    {
                        // Är det början av ett flertaligt nummer?
                        if (firstNumberInNumber)
                        {
                            newText.Append('#'); // Brädkors framför nummret

                            firstNumberInNumber = false; // Ändra så att inte nästa siffra får brädkors
                        }
                    }
                    else // Annars bara ett understräck
                    {
                        if (firstNumberInNumber)
                        {
                            newText.Append('#'); // Sätt brädkors framför siffran

                        }

                        firstNumberInNumber = true; // Förbereda för nästa flertaliga nummer
                    }
                }

                newText.Append(convertedChar);
            }

            string finishedText = newText.ToString().TrimStart();

            finishedText = finishedText.Replace('1', 'a');
            finishedText = finishedText.Replace('2', 'b');
            finishedText = finishedText.Replace('3', 'c');
            finishedText = finishedText.Replace('4', 'd');
            finishedText = finishedText.Replace('5', 'e');
            finishedText = finishedText.Replace('6', 'f');
            finishedText = finishedText.Replace('7', 'g');
            finishedText = finishedText.Replace('8', 'h');
            finishedText = finishedText.Replace('9', 'i');
            finishedText = finishedText.Replace('0', 'j');

            return finishedText;
        }

        public string convertBackToPrint(string oldText)
        {
            string newText = oldText.Replace(",", "");
            newText = newText.Replace("#", "");
            newText = newText.Replace("*", "å");
            newText = newText.Replace(">", "ä");
            newText = newText.Replace("[", "ö");
            newText = newText.Replace('\'', '.');
            newText = newText.Replace('1', ',');
            newText = newText.Replace('5', '?');
            newText = newText.Replace('6', '!');
            newText = newText.Replace('3', ':');
            newText = newText.Replace('7', '=');
            newText = newText.Replace('4', '+');
            newText = newText.Replace('9', '*');
            newText = newText.Replace('=', 'é');

            return newText;
        }
    }
}

2 个答案:

答案 0 :(得分:1)

考虑到这一点,或许,你真正想要做的是实现你自己的编码,称为PrintableSwedishBrailleAsciiEncoding继承自Encoding基类的东西。

using System.Text;

public sealed PrintableSwedishBrailleAsciiEncoding : Encoding
{
    ...
}

这样可以最大限度地提高代码的可恢复性,并使您能够简单地使用框架的其余部分来完成工作。


回应你对我现在删除的答案的评论,我想你在问,

  

如何替换某个字符,后跟任意数量的非空格字符,直到第一个空白字符。或者,更一般地说,整个单词以某个字符开头?

所以你可以使用Regex这样的东西,我认为这会匹配一个#后跟一些非空白字符。

var numberMatcher = new Regex(@"#\w+")
var firstMatch = numberMatcher.Match(yourText)

var alteredMatch = SomeTextAlteringFunction(firstMatch);

var yourNewText = numberMatcher.Replace(yourText, alteredMatch);

答案 1 :(得分:0)

这是我的解决方案(非常感谢Jodrell)

        public string convertBackToPrint(string oldText)
        {
            string newText = oldText.Replace(",", "");
            newText = newText.Replace("*", "å");
            newText = newText.Replace(">", "ä");
            newText = newText.Replace("[", "ö");
            newText = newText.Replace('\'', '.');
            newText = newText.Replace('1', ',');
            newText = newText.Replace('5', '?');
            newText = newText.Replace('6', '!');
            newText = newText.Replace('3', ':');
            newText = newText.Replace('7', '=');
            newText = newText.Replace('4', '+');
            newText = newText.Replace('9', '*');
            newText = newText.Replace('=', 'é');

            var numberMatcher = new Regex(@"#\w+");
            var firstMatch = numberMatcher.Match(newText);
            string alteredMatch = convertToNum(firstMatch.ToString());
            string yourNewText = numberMatcher.Replace(newText, alteredMatch);

            return yourNewText;
        }

        private string convertToNum(string oldText)
        {
            string newText = "";

            newText = oldText.Replace("a", "1");
            newText = newText.Replace("b", "2");
            newText = newText.Replace("c", "3");
            newText = newText.Replace("d", "4");
            newText = newText.Replace("e", "5");
            newText = newText.Replace("f", "6");
            newText = newText.Replace("g", "7");
            newText = newText.Replace("h", "8");
            newText = newText.Replace("i", "9");
            newText = newText.Replace("j", "0");
            newText = newText.Replace("#", "");

            return newText;
        }
相关问题