C# 如何高效地获取最后一个Unicode文本元素
如何在不迭代整个字符串的情况下获取字符串的最后一个Unicode文本元素?System.Globalization.StringInfo提供了两种方法,但我怀疑它们可以枚举整个字符串:C# 如何高效地获取最后一个Unicode文本元素,c#,C#,如何在不迭代整个字符串的情况下获取字符串的最后一个Unicode文本元素?System.Globalization.StringInfo提供了两种方法,但我怀疑它们可以枚举整个字符串: [TestMethod] [TestCategory("Verification")] public void GetLastTextElement_TextEndsWithSurrogatePair_GetsSurrogatePair() { // Arrange
[TestMethod]
[TestCategory("Verification")]
public void GetLastTextElement_TextEndsWithSurrogatePair_GetsSurrogatePair()
{
// Arrange
const string OsmanyaDigitOne = "\U000104A1";
const string OsmanyaDigitTwo = "\U000104A2";
const string Target = "abc" + OsmanyaDigitOne + "de" + OsmanyaDigitTwo;
// Act
int length = Target.Length;
string lastSubstring = Target.Substring(length - 1);
StringInfo stringInfo = new StringInfo(Target);
int lengthInTextElements = stringInfo.LengthInTextElements;
string lastTextElement = stringInfo.SubstringByTextElements(lengthInTextElements - 1);
string lastTextElementInOneExpression = Target.Substring(StringInfo.ParseCombiningCharacters(Target).Last());
// Assert
Assert.AreEqual(9, length, @"Wrong length");
Assert.AreNotEqual(OsmanyaDigitTwo, lastSubstring, @"Unexpectedly got last text element");
Assert.AreEqual(7, lengthInTextElements, @"Wrong length in text elements");
Assert.AreEqual(OsmanyaDigitTwo, lastTextElement, @"Wrong last text element");
Assert.AreEqual(OsmanyaDigitTwo, lastTextElementInOneExpression, @"Wrong last text element");
}
最后一个Unicode文本元素
如果您只是指最后一个unicode代码点,那么很容易:
string unicode = Target.Length >= 2 && char.IsLowSurrogate(Target, Target.Length - 1) && char.IsHighSurrogate(Target, Target.Length - 2)
? Target.Substring(Target.Length - 2, 2)
: Target.Substring(Target.Length - 1, 1).ToString();
如果您指的是最后一个图形(例如,最后一个代码点及其后面的组合标记,因此可能有多个代码点,如e
+◌̃
)它更复杂