• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

C# StringInfo类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了C#中System.Globalization.StringInfo的典型用法代码示例。如果您正苦于以下问题:C# StringInfo类的具体用法?C# StringInfo怎么用?C# StringInfo使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。



StringInfo类属于System.Globalization命名空间,在下文中一共展示了StringInfo类的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的C#代码示例。

示例1: Main

//引入命名空间
using System;
using System.Text;
using System.Globalization;

public sealed class App {
   static void Main() {
      // The string below contains combining characters.
      String s = "a\u0304\u0308bc\u0327";

      // Show each 'character' in the string.
      EnumTextElements(s);

      // Show the index in the string where each 'character' starts.
      EnumTextElementIndexes(s);
   }

   // Show how to enumerate each real character (honoring surrogates) in a string.
   static void EnumTextElements(String s) {
      // This StringBuilder holds the output results.
      StringBuilder sb = new StringBuilder();

      // Use the enumerator returned from GetTextElementEnumerator 
      // method to examine each real character.
      TextElementEnumerator charEnum = StringInfo.GetTextElementEnumerator(s);
      while (charEnum.MoveNext()) {
         sb.AppendFormat(
           "Character at index {0} is '{1}'{2}",
           charEnum.ElementIndex, charEnum.GetTextElement(),
           Environment.NewLine);
      }

      // Show the results.
      Console.WriteLine("Result of GetTextElementEnumerator:");
      Console.WriteLine(sb);
   }

   // Show how to discover the index of each real character (honoring surrogates) in a string.
   static void EnumTextElementIndexes(String s) {
      // This StringBuilder holds the output results.
      StringBuilder sb = new StringBuilder();

      // Use the ParseCombiningCharacters method to 
      // get the index of each real character in the string.
      Int32[] textElemIndex = StringInfo.ParseCombiningCharacters(s);

      // Iterate through each real character showing the character and the index where it was found.
      for (Int32 i = 0; i < textElemIndex.Length; i++) {
         sb.AppendFormat(
            "Character {0} starts at index {1}{2}",
            i, textElemIndex[i], Environment.NewLine);
      }

      // Show the results.
      Console.WriteLine("Result of ParseCombiningCharacters:");
      Console.WriteLine(sb);
   }
}
开发者ID:.NET开发者,项目名称:System.Globalization,代码行数:58,代码来源:StringInfo

输出:

Result of GetTextElementEnumerator:
Character at index 0 is 'a-"'
Character at index 3 is 'b'
Character at index 4 is 'c,'

Result of ParseCombiningCharacters:
Character 0 starts at index 0
Character 1 starts at index 3
Character 2 starts at index 4


示例2: Main

//引入命名空间
using System;
using System.Globalization;

public class Example
{
   public static void Main()
   {
      // The Unicode code points specify Arabic base characters and 
      // combining character sequences.
      string strCombining = "\u0627\u0655\u0650\u064A\u0647\u064E" +
                            "\u0627\u0628\u064C";

      // The Unicode code points specify private surrogate pairs.
      string strSurrogates = Char.ConvertFromUtf32(0x10148) +
                             Char.ConvertFromUtf32(0x20026) + "a" +
                             Char.ConvertFromUtf32(0xF1001);
      
      EnumerateTextElements(strCombining);
      EnumerateTextElements(strSurrogates);
   }

   public static void EnumerateTextElements(string str)
   {
      // Get the Enumerator.
      TextElementEnumerator teEnum = null;      

      // Parse the string using the ParseCombiningCharacters method.
      Console.WriteLine("\nParsing with ParseCombiningCharacters:");
      int[] teIndices = StringInfo.ParseCombiningCharacters(str);
      
      for (int i = 0; i < teIndices.Length; i++) {
         if (i < teIndices.Length - 1)
            Console.WriteLine("Text Element {0} ({1}..{2})= {3}", i, 
               teIndices[i], teIndices[i + 1] - 1, 
               ShowHexValues(str.Substring(teIndices[i], teIndices[i + 1] - 
                             teIndices[i])));
         else
            Console.WriteLine("Text Element {0} ({1}..{2})= {3}", i, 
               teIndices[i], str.Length - 1, 
               ShowHexValues(str.Substring(teIndices[i])));
      }
      Console.WriteLine();

      // Parse the string with the GetTextElementEnumerator method.
      Console.WriteLine("Parsing with TextElementEnumerator:");
      teEnum = StringInfo.GetTextElementEnumerator(str);

      int teCount = - 1;

      while (teEnum.MoveNext()) {
         // Displays the current element.
         // Both GetTextElement() and Current retrieve the current
         // text element. The latter returns it as an Object.
         teCount++;
         Console.WriteLine("Text Element {0} ({1}..{2})= {3}", teCount, 
            teEnum.ElementIndex, teEnum.ElementIndex + 
            teEnum.GetTextElement().Length - 1, ShowHexValues((string)(teEnum.Current)));
      }
   }
   
   private static string ShowHexValues(string s)
   {
      string hexString = "";
      foreach (var ch in s)
         hexString += String.Format("{0:X4} ", Convert.ToUInt16(ch));

      return hexString;
   }
}
开发者ID:.NET开发者,项目名称:System.Globalization,代码行数:70,代码来源:StringInfo

输出:

Parsing with ParseCombiningCharacters:
Text Element 0 (0..2)= 0627 0655 0650
Text Element 1 (3..3)= 064A
Text Element 2 (4..5)= 0647 064E
Text Element 3 (6..6)= 0627
Text Element 4 (7..8)= 0628 064C

Parsing with TextElementEnumerator:
Text Element 0 (0..2)= 0627 0655 0650
Text Element 1 (3..3)= 064A
Text Element 2 (4..5)= 0647 064E
Text Element 3 (6..6)= 0627
Text Element 4 (7..8)= 0628 064C

Parsing with ParseCombiningCharacters:
Text Element 0 (0..1)= D800 DD48
Text Element 1 (2..3)= D840 DC26
Text Element 2 (4..4)= 0061
Text Element 3 (5..6)= DB84 DC01

Parsing with TextElementEnumerator:
Text Element 0 (0..1)= D800 DD48
Text Element 1 (2..3)= D840 DC26
Text Element 2 (4..4)= 0061
Text Element 3 (5..6)= DB84 DC01



注:本文中的System.Globalization.StringInfo类示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C# TextElementEnumerator类代码示例发布时间:2022-05-24
下一篇:
C# SortVersion类代码示例发布时间:2022-05-24
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap