在特定字符后替换char数

Jon*_*ist 8 c# string replace braille

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

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

1     = #a
123   = #abc
12 45 = #ab #de
Run Code Online (Sandbox Code Playgroud)

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

Jonas = ,jonas
JONAS = ,,jonas
Run Code Online (Sandbox Code Playgroud)

我在将盲文转换回正常方面遇到了问题.我不能只将每个转换a1等等.数字可以通过检查#,然后将字符更改为下一个空格,但我不知道如何.字母前面的逗号很难与文本中的其他逗号分开.

这是我转换为盲文的课程:

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;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

Jod*_*ell 1

考虑到这一点,也许您真正想做的是实现自己的编码,称为PrintableSwedishBrailleAsciiEncodingEncoding基类继承之类的东西。

using System.Text;

public sealed PrintableSwedishBrailleAsciiEncoding : Encoding
{
    ...
}
Run Code Online (Sandbox Code Playgroud)

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


针对您对我现已删除的答案的评论,我认为您是在问,

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

所以你可以使用像这样的正则表达式#,我认为这会匹配后跟许多非空白字符。

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

var alteredMatch = SomeTextAlteringFunction(firstMatch);

var yourNewText = numberMatcher.Replace(yourText, alteredMatch);
Run Code Online (Sandbox Code Playgroud)