We are trying to add emojis our game using Emoji One and Unity with TextMeshPro. We have a window opening up which shows all the emojis and then the user can pick the one they want, we have run into problems with deleting them and typing text after them.
Here is the code we have have, the name of the sprite is this: 1f609, however the output when we run the code and add it to the input field is this: \ud83d\ude09
string name = _image.sprite.name;
int hexNumber = int.Parse(name, System.Globalization.NumberStyles.HexNumber);
string c = char.ConvertFromUtf32(hexNumber);
_inputField.text += c;
The expected result is that we should see just a single unicode character (I think) so that we can properly delete emoji's with the backspace key and when you enter an emoji it does not create two characters to be deleted and it does not place the character between the two emoji spots so when we enter text it breaks.
Right now if we delete text it will leave us with empty square and if we add text afterwards through typing in the box it will split the characters in two breaking them.
Unicode in C# world means UTF-16, so there are these "surrogate pairs" that you have to deal with now. One solution would be to detect if you just removed one half of a surrogate pair and in that case remove the other half as well. First half of a surrogate pair is always in range [D800h, DBFFh] and second half is always in range [DC00h, DFFFh], so when you remove a char in one of those ranges you know that you should remove one more char, and you also know where that char is (before or after the one just removed).
Related
So I want to create a script that first prints out values from a (pretty big) 2D array and then change one character by erasing it and printing a new one without clearing out the whole console and redrawing it because that creates flickering which is very annoying when i want to make often changes. So is there any way to replace or erase one character that has been already printed or any other efficient way to do it? I just don't want flickering.
move the cursor and write a space " ". (Or whatever character you want to use as a replacement.)
To delete the last character input in the console you can use the following command:
Console.Write("\b");
However, if you're attempting to remove a character in the middle of the string you'd have to do some clever backspacing to the character you want, and then reprint the rest of the string. You could write a loop to backspace until the character you wish to remove, add the removed characters to a stack, and pop them off to rewrite it.
The second approach would be very inefficient due to console commands being naturally slow, and also really misusing the console backspace command.
I've looked online for this but not been able to find an answer unfortunately (sorry if there is something I have missed).
I have some code which filters out a specific string (which can change depending on what is read from the serial port). I want to be able to delete all of the characters which I am not using.
e.g. the string I want from the text below is "ThisIsTheStringIWant"
efefhokiehfdThisIsTheStringIWantcbunlokew
Now, I already have a function with some code which will identify this and print it to where I want. However, as the comms could be coming in from multiple ports at any frequency, before printing the string to where I want it, I need to have a piece of code which will recognise everything I don't want and delete it from my buffer.
e.g. Using the same random text above, I want to get rid of the two random strings at the ends (which are before and after "ThisIsTheStringIWant" in the middle).
efefhokiehfdThisIsTheStringIWantcbunlokew
I have tried using the highest voted answer from this question, however I can't find a way to delete the unwanted text before my wanted string. Remove characters after specific character in string, then remove substring?
If anyone can help, that would be great!
Thanks!
Edit:
Sorry, I should have probably made my question clearer.
Any possible number of characters could be before and/or after the actual string I want, and as the string I want is coming from a serial port it will be different every time depending on what comms are coming in from the serial port. On my application I have a cell in a DGV called "Extract" and by typing in the first bit of the comms I am expecting (in this case, the extract would be This). But that will be different depending on what I am doing.
Find the position of the string you want, delete from the beginning to the predecessor of that position, then delete everything from the length of your string to the end.
String: efefhokiehfdThisIsTheStringIWantcbunlokew
Step 1 - "ThisIsTheStringIWant" starts at position 13, so delete the first twelve, leaving...
String: ThisIsTheStringIWantcbunlokew
Step 2 - "ThisIsTheStringIWant" is 20 characters long, so delete from character 21 to the length of the string, leaving:
String: ThisIsTheStringIWant
The following console app works fine:
class Program
{
static void Main(string[] args)
{
string plainx = "‘test data’ random suffix";
plainx = Regex.Replace(plainx, #"‘", string.Empty);
Console.WriteLine(plainx);
}
}
However its giving me trouble in an ASP.Net application.. I am attaching a screenshot of the VS Debug watch window and Immediate window
(Click for larger view)
As you can see, the Regex.Replace in the Immediate Window works - but somehow it is not working in the code (line 71). I've also used String.Replace without success.
Edit
It seems the value that was stored in the DB is something than what the editor shows... kind of weird..
Have you actually examined the text being compared? What Unicode code points does it contain?
Your code shows you trying to replace the glyph '‘', which is a left "smart quote". The character's name is LEFT SINGLE QUOTATION MARK and its code point is 0x2018 (aka '\u2018'). This is a character you can't ordinarily enter on a keyboard.
What you are probably seeing is the glyph '`', a "backtick". Its character name is GRAVE ACCENT and its code point is 0x0060 (aka '\u0060'). This is the character typed when you press the [unshifted] tilde key on a standard US keyboard (leftmost key on the number row).
It might, of course, be any of a number of other characters whose glyph is similar to a single quote. See Commonly Confused Characters for more information.
The single quote in your code is not the same single quote in the string you are testing.
Use the hex value returned from testx[0] directly to guarantee that we are using the correct quote.
plainx = Regex.Replace(plainx, "\u2018", string.Empty);
try to replace :
#"‘" to #"\‘"
code :
string plainx = "‘test data’ random suffix";
plainx = System.Text.RegularExpressions.Regex.Replace(plainx, #"\‘", string.Empty);
Console.WriteLine(plainx);
Console.Read();
I need to compare a value in a string to what user typed in a richtextbox.
For example: if a richtextbox holds string rtbText = "aaaka" and I compare this to another variable string comparable = "ka"(I want it to compare backwards). I want the last 2 letters from rtbText (comparable has only 2 letters) to be replaced with something that was predetermined(doesn't really matter what).
So rtbText should look like this:
rtbText = "aaa(something)"
This doesn't really have to be compared it can just count letters in comparable and based on that it can remove 2 letters from rtbText and replace them with something else.
UPDATE:
Here is what I have:
int coLen = comparable.Length;
comparable = null;
TextPointer caretBack = rtb.CaretPosition.GetPositionAtOffset(coLen, LogicalDirection.Backward);
TextRange rtbText = new TextRange(rtb.CaretPosition, caretBack);
string text = rtbText.Text;
rtbText returns an empty string or I get an error for everything longer than 3 characters. What am I doing wrong?
Let me elaborate it a little bit further. I have a listbox that holds replacements for values that user types in rtb. The values(replacements) are coming from there, meaning that I don't really need to go through the whole text to check values. I just need to check the values right before caret. I am comparing these values to what I have stored in another variable (comparable).
Please let me know if you don't understand something.
I did my best to explain what needs to be done.
Thank you
You could use Regex.Replace.
// this replaces all occurances of "ka" with "Replacement"
Regex replace = new Regex("ka");
string result = replace.Replace("aaaka","Replacemenet");
gumenimeda, I had similar problems few weeks ago. I found my self doing the following (I asume you will have more than one occurance in the RichTextBox that you will need to change), note that I did it for Windows Forms where I have access directly to the Rtf text of the control, not quite sure if it will work well in your scenario:
I find all the occurancies of the string (using IndexOf for example) and store them in a List for example.
Sort the list in descending order (max index goes first, the one before him second, etc)
Start replacing the occurancies directly in the RichTextBox, by removing the characters I don't need and appending the characters I need.
The sorting in step 2 is necessary as we always want to start from the last occurance going up to the first. Starting from the first occurance or any other and going down will have an unpleasant surprise - if the length of the chunk you want to remove and the length of the chunk you want to append are different in length, the string will be modified and all other occurancies will be invalid (for example if the second occurance was in at position 12 and your new string is 2 characters longer than the original, it will become 14th). This is not an issue if we go from the last to the first occurance as the change in string will not affect the next occurance in the list).
Ofcourse I can not be sure that this is the fastest way that can be used to achieve the desired result. It's just what I came up with and what worked for me.
Good luck!
I'm writing a console app that needs to print some atypical (for a console app) unicode characters such as musical notes, box drawing symbols, etc.
Most characters show up correctly, or show a ? if the glyph doesn't exist for whatever font the console is using, however I found one character which behaves oddly which can be demonstrated with the lines below:
Console.Write("ABC");
Console.Write('♪'); //This is the same as: Console.Write((char)0x266A);
Console.Write("XYZ");
When this is run it will print ABC then move the cursor back to the start of the line and overwrite it with XYZ. Why does this happen?
The console doesn't use Uncode, so the characters has to be translated to an 8-bit code page. The ♪ character is converted to the character with code 13 (hex 0x0d), which is CR or Carrage Return.
In most code pages, for example code page 850, the CR chararacter glyph resembles a quarter note, and the 266a character is specified as the Unicode equivalent.
However, if you write the CR character to the console, it will not display the quarter note glyph, instead it is interpreted as the control character CR which moves the cursor to the beginning of the line.
Console.Write('♪'); is considered Unicode. My guess it is it translates it to the closest ASCII character. You should be using U+1D160 or the appropriate unicode, musical equivalent.
There are the required primitives to generate musical output in the Unicode code set (starting at U+1D100). For example, U+1D11A is a 5-line staff, U+1D158 is a closed notehead.
See http://www.unicode.org/charts/PDF/U1D100.pdf
..then the issue becomes making sure that you have a typeface with the appropriate glyphs included (and dealing with the issues of spacing things correctly, etc.)
IF you're looking to generate printed output, you should look at Lilypond, which is an OSS music notation package that uses a text file format to define the musical content and then generates gorgeous output.