Replace char numbers after specific char

I am creating an application that converts text to braille. Converting to braille is not a problem, but I do not know how to convert it.

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 to normal. I cannot just convert all a to 1 and so on. The numbers can be checked with # , and then change the characters after it to the next space, but I don't know how to do it. The comma before the letter is more difficult to separate the text from other commas.

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; } } } 
+8
string c # replace braille
source share
2 answers

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

 using System.Text; public sealed PrintableSwedishBrailleAsciiEncoding : Encoding { ... } 

This will maximize the profitability of your code and allow you to simply use the rest of the framework to do your work.


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

How can I replace a specific character, followed by any number of characters without spaces, up to the first char space. Or, in a more general sense, whole words starting with a certain character?

So, you could use Regex something like this, I think it would match # , followed by a few non-white space characters.

 var numberMatcher = new Regex(@"#\w+") var firstMatch = numberMatcher.Match(yourText) var alteredMatch = SomeTextAlteringFunction(firstMatch); var yourNewText = numberMatcher.Replace(yourText, alteredMatch); 
+1
source share

That was my decision (Thanks a lot to 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; } 
0
source share

All Articles