Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace numbers of char after a specific char

I´m creating an application that converts text to Braille. Converting to Braille is not a problem, but I don´t know how to convert it back.

Example 1: Converting numbers to Braille

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

Example 2: Converting capitals to Braille

Jonas = ,jonas
JONAS = ,,jonas

I have a problem converting Braille back to normal. I can't just convert every a to 1 and so on. The numbers can be checked by the # and then change the chars after it to the next space, but I dont know how. The comma before the letter is harder to separate from other commas in the text.

Here is my class for converting to braille:

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;
        }
    }
}
like image 319
Jonas Löfkvist Avatar asked Sep 11 '12 07:09

Jonas Löfkvist


People also ask

How do I remove all characters from a string after a specific character?

The indexOf method returns the index of the first occurrence of a character in the string. If you need to remove everything after the last occurrence of a specific character, use the lastIndexOf method.

How do I cut a string after a specific character in C #?

Solution 1. string str = "this is a #string"; string ext = str. Substring(0, str. LastIndexOf("#") + 1);

How do you remove all characters from a string after a specific character in Python?

To remove everything after the first occurrence of the character '-' in a string, pass the character '-' as a separator in the partition() function. Then assign the part before the separator to the original string variable. It will affect that we have deleted everything after the character '-' in a string.

Which function is used to replace a sequence of characters with another set of characters?

The Oracle/PLSQL REGEXP_REPLACE function is an extension of the REPLACE function. This function, introduced in Oracle 10g, will allow you to replace a sequence of characters in a string with another set of characters using regular expression pattern matching.


1 Answers

Thinking about this, perhaps, what you actually want to do is implement your own encoding, called somthing like PrintableSwedishBrailleAsciiEncoding inheriting from the Encoding base class.

using System.Text;

public sealed PrintableSwedishBrailleAsciiEncoding : Encoding
{
    ...
}

That would maximise the resuability of you code and enable you to simply use the rest of the framework to do your work.


In response to your comment on my now deleted answer, I think you are asking,

How can I replace a certain character, followed by any number of non whitespace chars, up until the first whitespace char. Or, more generally, whole words beginning with a certain character?

So you could use a Regex something like this, I think this would match a # followed by a number of non whitespace characters.

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

var alteredMatch = SomeTextAlteringFunction(firstMatch);

var yourNewText = numberMatcher.Replace(yourText, alteredMatch);
like image 68
Jodrell Avatar answered Oct 12 '22 12:10

Jodrell