I'm saving some strings from a third party into my database (postgres). Sometimes these strings are too long and need to be truncated to fit into the column in my table.
On some random occasions I accidentally truncate the string right where there is a Unicode character, which gives me a "broken" string that I cannot save into the database. I get the following error: Unable to translate Unicode character \uD83D at index XXX to specified code page
.
I've created a minimal example to show you what I mean. Here I have a string that contains a Unicode character ("Small blue diamond" U+1F539). Depending on where I truncate, it gives me a valid string or not.
var myString = @"This is a string before an emoji: This is after the emoji.";
var brokenString = myString.Substring(0, 34);
// Gives: "This is a string before an emoji:☐"
var test3 = myString.Substring(0, 35);
// Gives: "This is a string before an emoji:"
Is there a way for me to truncate the string without accidentally breaking any Unicode chars?