1,字符----------在.net中,字符总是16位的Unicode代码值.每个字符都是一个System.Char结构(值类型)的一个实列.

using System;

public class CharStructureSample
{
public static void Main()
{
char chA = 'A';
char ch1 = '1';
string str = "test string"; Console.WriteLine(chA.CompareTo('B')); //----------- Output: "-1" (meaning 'A' is 1 less than 'B')
Console.WriteLine(chA.Equals('A')); //----------- Output: "True"
Console.WriteLine(Char.GetNumericValue(ch1)); //----------- Output: "1"
Console.WriteLine(Char.IsControl('\t')); //----------- Output: "True"
Console.WriteLine(Char.IsDigit(ch1)); //----------- Output: "True"
Console.WriteLine(Char.IsLetter(',')); //----------- Output: "False"
Console.WriteLine(Char.IsLower('u')); //----------- Output: "True"
Console.WriteLine(Char.IsNumber(ch1)); //----------- Output: "True"
Console.WriteLine(Char.IsPunctuation('.')); //----------- Output: "True"
Console.WriteLine(Char.IsSeparator(str, 4)); //----------- Output: "True"
Console.WriteLine(Char.IsSymbol('+')); //----------- Output: "True"
Console.WriteLine(Char.IsWhiteSpace(str, 4)); //----------- Output: "True"
Console.WriteLine(Char.Parse("S")); //----------- Output: "S"
Console.WriteLine(Char.ToLower('M')); //----------- Output: "m"
Console.WriteLine('x'.ToString()); //----------- Output: "x"
}
}

可以使用GetUnicodeCategory来处理字符的类别

using System;
using System.Globalization; class Example
{
public static void Main()
{
// Define a string with a variety of character categories.
String s = "The red car drove down the long, narrow, secluded road.";
// Determine the category of each character.
foreach (var ch in s)
Console.WriteLine("'{0}': {1}", ch, Char.GetUnicodeCategory(ch));
}
}
// The example displays the following output:
// 'T': UppercaseLetter
// 'h': LowercaseLetter
// 'e': LowercaseLetter
// ' ': SpaceSeparator
// 'r': LowercaseLetter
// 'e': LowercaseLetter
// 'd': LowercaseLetter
// ' ': SpaceSeparator
// 'c': LowercaseLetter
// 'a': LowercaseLetter
// 'r': LowercaseLetter
// ' ': SpaceSeparator
// 'd': LowercaseLetter
// 'r': LowercaseLetter
// 'o': LowercaseLetter
// 'v': LowercaseLetter
// 'e': LowercaseLetter
// ' ': SpaceSeparator
// 'd': LowercaseLetter
// 'o': LowercaseLetter
// 'w': LowercaseLetter
// 'n': LowercaseLetter
// ' ': SpaceSeparator
// 't': LowercaseLetter
// 'h': LowercaseLetter
// 'e': LowercaseLetter
// ' ': SpaceSeparator
// 'l': LowercaseLetter
// 'o': LowercaseLetter
// 'n': LowercaseLetter
// 'g': LowercaseLetter
// ',': OtherPunctuation
// ' ': SpaceSeparator
// 'n': LowercaseLetter
// 'a': LowercaseLetter
// 'r': LowercaseLetter
// 'r': LowercaseLetter
// 'o': LowercaseLetter
// 'w': LowercaseLetter
// ',': OtherPunctuation
// ' ': SpaceSeparator
// 's': LowercaseLetter
// 'e': LowercaseLetter
// 'c': LowercaseLetter
// 'l': LowercaseLetter
// 'u': LowercaseLetter
// 'd': LowercaseLetter
// 'e': LowercaseLetter
// 'd': LowercaseLetter
// ' ': SpaceSeparator
// 'r': LowercaseLetter
// 'o': LowercaseLetter
// 'a': LowercaseLetter
// 'd': LowercaseLetter
// '.': OtherPunctuation
  • 单个字符可能由多个Char对象构成

2,String类

3,Enum类

4,数组

 int[][] i = new int[3][];//交错数组,表示i 是一个指向 int [] 类型实列的数组
i[0] = new int[10];
i[1] = new int[11];
i[2] = new int[12];
int[,] j = new int[3, 5];//多维素组

4.1 数组转型

  • 必须维数一致,下基一致
  • 并且存在从元素源类型到目标源类型的隐式转换.
     object[] obj1= new object[] { new object(), new object() };
    string[] str1 = { "abc", "efg" };
    obj1 = str1;
    obj1 = new object[str1.Length];
    Array.Copy(str1, obj1, str1.Length);

使用,System.Buffer.BlockCopy

 public class ArrayRef
{
public static void DisplayArray(Array arr, string name)
{
Console.WindowWidth = 120;
Console.Write("{0,11}:", name);
for (int ctr = 0; ctr < arr.Length; ctr++)
{
byte[] bytes;
if (arr is long[])
bytes = BitConverter.GetBytes((long)arr.GetValue(ctr));
else
bytes = BitConverter.GetBytes((short)arr.GetValue(ctr)); foreach (byte byteValue in bytes)
Console.Write(" {0:X2}", byteValue);
}
Console.WriteLine();
} // Display the individual array element values in hexadecimal.
public static void DisplayArrayValues(Array arr, string name)
{
// Get the length of one element in the array.
int elementLength = Buffer.ByteLength(arr) / arr.Length;
string formatString = String.Format(" {{0:X{0}}}", 2 * elementLength);
Console.Write("{0,11}:", name);
for (int ctr = 0; ctr < arr.Length; ctr++)
Console.Write(formatString, arr.GetValue(ctr)); Console.WriteLine();
}
public static void test()
{
// These are the source and destination arrays for BlockCopy.
short[] src = { 258, 259, 260, 261, 262, 263, 264,
265, 266, 267, 268, 269, 270 };
long[] dest = { 17, 18, 19, 20 }; // Display the initial value of the arrays in memory.
Console.WriteLine("Initial values of arrays:");
Console.WriteLine(" Array values as Bytes:");
DisplayArray(src, "src");
DisplayArray(dest, "dest");
Console.WriteLine(" Array values:");
DisplayArrayValues(src, "src");
DisplayArrayValues(dest, "dest");
Console.WriteLine(); // Copy bytes 5-10 from source to index 7 in destination and display the result.
Buffer.BlockCopy(src, 5, dest, 7, 6);
Console.WriteLine("Buffer.BlockCopy(src, 5, dest, 7, 6 )");
Console.WriteLine(" Array values as Bytes:");
DisplayArray(src, "src");
DisplayArray(dest, "dest");
Console.WriteLine(" Array values:");
DisplayArrayValues(src, "src");
DisplayArrayValues(dest, "dest");
Console.WriteLine(); // Copy bytes 16-20 from source to index 22 in destination and display the result.
Buffer.BlockCopy(src, 16, dest, 22, 5);
Console.WriteLine("Buffer.BlockCopy(src, 16, dest, 22, 5)");
Console.WriteLine(" Array values as Bytes:");
DisplayArray(src, "src");
DisplayArray(dest, "dest");
Console.WriteLine(" Array values:");
DisplayArrayValues(src, "src");
DisplayArrayValues(dest, "dest");
Console.WriteLine(); // Copy overlapping range of bytes 4-10 to index 5 in source.
Buffer.BlockCopy(src, 4, src, 5, 7);
Console.WriteLine("Buffer.BlockCopy( src, 4, src, 5, 7)");
Console.WriteLine(" Array values as Bytes:");
DisplayArray(src, "src");
DisplayArray(dest, "dest");
Console.WriteLine(" Array values:");
DisplayArrayValues(src, "src");
DisplayArrayValues(dest, "dest");
Console.WriteLine(); // Copy overlapping range of bytes 16-22 to index 15 in source.
Buffer.BlockCopy(src, 16, src, 15, 7);
Console.WriteLine("Buffer.BlockCopy( src, 16, src, 15, 7)");
Console.WriteLine(" Array values as Bytes:");
DisplayArray(src, "src");
DisplayArray(dest, "dest");
Console.WriteLine(" Array values:");
DisplayArrayValues(src, "src");
DisplayArrayValues(dest, "dest");
}
}
Initial values of arrays:
Array values as Bytes:
src: 02 01 03 01 04 01 05 01 06 01 07 01 08 01 09 01 0A 01 0B 01 0C 01 0D 01 0E 01
dest: 11 00 00 00 00 00 00 00 12 00 00 00 00 00 00 00 13 00 00 00 00 00 00 00 14 00 00 00 00 00 00 00
Array values:
src: 0102 0103 0104 0105 0106 0107 0108 0109 010A 010B 010C 010D 010E
dest: 0000000000000011 0000000000000012 0000000000000013 0000000000000014 Buffer.BlockCopy(src, 5, dest, 7, 6 )
Array values as Bytes:
src: 02 01 03 01 04 01 05 01 06 01 07 01 08 01 09 01 0A 01 0B 01 0C 01 0D 01 0E 01
dest: 11 00 00 00 00 00 00 01 05 01 06 01 07 00 00 00 13 00 00 00 00 00 00 00 14 00 00 00 00 00 00 00
Array values:
src: 0102 0103 0104 0105 0106 0107 0108 0109 010A 010B 010C 010D 010E
dest: 0100000000000011 0000000701060105 0000000000000013 0000000000000014 Buffer.BlockCopy(src, 16, dest, 22, 5)
Array values as Bytes:
src: 02 01 03 01 04 01 05 01 06 01 07 01 08 01 09 01 0A 01 0B 01 0C 01 0D 01 0E 01
dest: 11 00 00 00 00 00 00 01 05 01 06 01 07 00 00 00 13 00 00 00 00 00 0A 01 0B 01 0C 00 00 00 00 00
Array values:
src: 0102 0103 0104 0105 0106 0107 0108 0109 010A 010B 010C 010D 010E
dest: 0100000000000011 0000000701060105 010A000000000013 00000000000C010B Buffer.BlockCopy( src, 4, src, 5, 7)
Array values as Bytes:
src: 02 01 03 01 04 04 01 05 01 06 01 07 08 01 09 01 0A 01 0B 01 0C 01 0D 01 0E 01
dest: 11 00 00 00 00 00 00 01 05 01 06 01 07 00 00 00 13 00 00 00 00 00 0A 01 0B 01 0C 00 00 00 00 00
Array values:
src: 0102 0103 0404 0501 0601 0701 0108 0109 010A 010B 010C 010D 010E
dest: 0100000000000011 0000000701060105 010A000000000013 00000000000C010B Buffer.BlockCopy( src, 16, src, 15, 7)
Array values as Bytes:
src: 02 01 03 01 04 04 01 05 01 06 01 07 08 01 09 0A 01 0B 01 0C 01 0D 0D 01 0E 01
dest: 11 00 00 00 00 00 00 01 05 01 06 01 07 00 00 00 13 00 00 00 00 00 0A 01 0B 01 0C 00 00 00 00 00
Array values:
src: 0102 0103 0404 0501 0601 0701 0108 0A09 0B01 0C01 0D01 010D 010E
dest: 0100000000000011 0000000701060105 010A000000000013 00000000000C010B }
  • BitConvert----用于将基数据类型转换为Byte数组,或者将Byte数组转换为基数据类型.
  • Array的许多静态方法:
  • public static int BinarySearch (Array array, object value);
  • 如果搜索到该值,则返回其索引,否则返回一个负数,~result只是第一个比该value大的数组元素的位置.
    public static void CallArrayBinSearch()
    {
    int[] r1 = { 1, 2, 3, 7, 8, 9 };
    int result = Array.BinarySearch(r1, 6);
    int result1 = Array.BinarySearch(r1, 7);
    Console.WriteLine(~result + " " + result1);
    }
    //结果 -4(3),3 表明,比6大的第一个元素是3

聊一聊排序:  1,元素的顺序根本比较是 x.CompareTo( y)

    • x<y ,return –1 定义的是升序排列----意思是从左到右边,从上到下来看. 对于升序排列的元素则,所有的x.CompareTo(y),返回-1
    • x=y,return 0,定义相等,x.CompareTo(y)返回 0
    • x>y,return 1,表明是降序----意思是从x.
      • using System;
        using System.Collections.Generic; public class ReverseComparer: IComparer<string>
        {
        public int Compare(string x, string y)
        {
        // Compare y and x in reverse order.
        return y.CompareTo(x);
        }
        } public class Example
        {
        public static void Main()
        {
        string[] dinosaurs = {"Pachycephalosaurus",
        "Amargasaurus",
        "Tyrannosaurus",
        "Mamenchisaurus",
        "Deinonychus",
        "Edmontosaurus"}; Console.WriteLine();
        foreach( string dinosaur in dinosaurs )
        {
        Console.WriteLine(dinosaur);
        } ReverseComparer rc = new ReverseComparer(); Console.WriteLine("\nSort");
        Array.Sort(dinosaurs, rc); Console.WriteLine();
        foreach( string dinosaur in dinosaurs )
        {
        Console.WriteLine(dinosaur);
        } Console.WriteLine("\nBinarySearch for 'Coelophysis':");
        int index = Array.BinarySearch(dinosaurs, "Coelophysis", rc);
        ShowWhere(dinosaurs, index); Console.WriteLine("\nBinarySearch for 'Tyrannosaurus':");
        index = Array.BinarySearch(dinosaurs, "Tyrannosaurus", rc);
        ShowWhere(dinosaurs, index);
        } private static void ShowWhere<T>(T[] array, int index)
        {
        if (index<0)
        {
        // If the index is negative, it represents the bitwise
        // complement of the next larger element in the array.
        //
        index = ~index; Console.Write("Not found. Sorts between: "); if (index == 0)
        Console.Write("beginning of array and ");
        else
        Console.Write("{0} and ", array[index-1]); if (index == array.Length)
        Console.WriteLine("end of array.");
        else
        Console.WriteLine("{0}.", array[index]);
        }
        else
        {
        Console.WriteLine("Found at index {0}.", index);
        }
        }
        } /* This code example produces the following output: Pachycephalosaurus
        Amargasaurus
        Tyrannosaurus
        Mamenchisaurus
        Deinonychus
        Edmontosaurus Sort Tyrannosaurus
        Pachycephalosaurus
        Mamenchisaurus
        Edmontosaurus
        Deinonychus
        Amargasaurus BinarySearch for 'Coelophysis':
        Not found. Sorts between: Deinonychus and Amargasaurus. BinarySearch for 'Tyrannosaurus':
        Found at index 0.
        */
    • CompareTo(y)返回1.

对于

BinarySearch(Array, Object, IComparer)//元素实现 int Compare(T x, T y)接口
public static int BinarySearch (Array array, int index, int length, object value, System.Collections.IComparer comparer);指定了搜索范围

  • Array.Clear(Array, Int32, Int32) 方法
    public static void Clear (Array array, int index, int length);
  • Array.ConstraineCopy----完全拷贝Array的内容,深拷贝.拷贝后,两个区域无关.
  • public static void ConstrainedCopy (Array sourceArray, int sourceIndex, Array destinationArray, int destinationIndex, int length);
  • Array.ConvertAll---数组进行转换,这也是进行深度拷贝的方法.可以生成新对象.
    public static TOutput[] ConvertAll<TInput,TOutput> (TInput[] array, Converter<TInput,TOutput> converter);
    //使用Converter委托进行数据类型转换 public delegate TOutput Converter<in TInput,out TOutput>(TInput input);
    public class SamplesArray2{
    
       public static void Main()  {
    // Creates and initializes the source Array.
    Array myArrayZero=Array.CreateInstance( typeof(String), 3 );
    myArrayZero.SetValue( "zero", 0 );
    myArrayZero.SetValue( "one", 1 ); // Displays the source Array.
    Console.WriteLine( "The array with lower bound=0 contains:" );
    PrintIndexAndValues( myArrayZero ); // Creates and initializes the target Array.//创建非零数组
    int[] myArrLen = { 4 };
    int[] myArrLow = { 2 };
    Array myArrayTwo=Array.CreateInstance( typeof(String), myArrLen, myArrLow );
    myArrayTwo.SetValue( "two", 2 );
    myArrayTwo.SetValue( "three", 3 );
    myArrayTwo.SetValue( "four", 4 );
    myArrayTwo.SetValue( "five", 5 ); // Displays the target Array.
    Console.WriteLine( "The array with lower bound=2 contains:" );
    PrintIndexAndValues( myArrayTwo ); // Copies from the array with lower bound=0 to the array with lower bound=2.
    myArrayZero.CopyTo( myArrayTwo, 3 ); // Displays the modified target Array.
    Console.WriteLine( "\nAfter copying to the target array from index 3:" );
    PrintIndexAndValues( myArrayTwo );
    } public static void PrintIndexAndValues( Array myArray ) {
    for ( int i = myArray.GetLowerBound(0); i <= myArray.GetUpperBound(0); i++ )
    Console.WriteLine( "\t[{0}]:\t{1}", i, myArray.GetValue( i ) );
    }
    }
    /*
    This code produces the following output. The array with lower bound=0 contains:
    [0]: zero
    [1]: one
    [2]:
    The array with lower bound=2 contains:
    [2]: two
    [3]: three
    [4]: four
    [5]: five After copying to the target array from index 3:
    [2]: two
    [3]: zero
    [4]: one
    [5]:
    */
  • Array.CreateInstance---
    重载
    
    CreateInstance(Type, Int32)
    创建使用从零开始的索引、具有指定 Array 和长度的一维 Type。
    CreateInstance(Type, Int32[])
    创建索引从零开始、具有指定 Array 和维长的多维 Type。 维的长度在一个 32 位整数数组中指定。
    CreateInstance(Type, Int64[])
    创建索引从零开始、具有指定 Array 和维长的多维 Type。 维的长度在一个 64 位整数数组中指定。
    CreateInstance(Type, Int32, Int32)
    创建使用从零开始的索引、具有指定 Array 和维长的二维 Type。
    CreateInstance(Type, Int32[], Int32[])
    创建具有指定下限、指定 Array 和维长的多维 Type。
    CreateInstance(Type, Int32, Int32, Int32)
    创建使用从零开始的索引、具有指定 Array 和维长的三维 Type。*/
  • Array.Exsits---
  • public static bool Exists<T> (T[] array, Predicate<T> match);
    //----------指定委托,判断元素是否匹配
    public delegate bool Predicate<in T>(T obj);
    //-----------委托
    public static T Find<T> (T[] array, Predicate<T> match);
    //----返回找到的元素T,如果没找到则是default(T)
    public static T[] FindAll<T> (T[] array, Predicate<T> match);
    //返回所有找到的元素T.
    public static int FindIndex<T> (T[] array, Predicate<T> match);
    //返回匹配元素的index
    public static T FindLast<T> (T[] array, Predicate<T> match);
    //返回最后匹配的元素T
    public static int FindLastIndex<T> (T[] array, Predicate<T> match);
    //返回最后匹配的元素T的索引
    public static void ForEach<T> (T[] array, Action<T> action);
    //对所有元素执行方法ACTION
    public int GetLength (int dimension);
    //获取数组某维的长度
    public long GetLongLength (int dimension);
    //获取数组某维64位长度

clr from c# 字符 ,字符串 和 文本处理的更多相关文章

  1. [Clr via C#读书笔记]Cp14字符字符串和文本处理

    Cp14字符字符串和文本处理 字符 System.Char结构,2个字节的Unicode,提供了大量的静态方法:可以直接强制转换成数值: 字符串 使用最频繁的类型:不可变:引用类型,在堆上分配,但是使 ...

  2. <NET CLR via c# 第4版>笔记 第14章 字符,字符串和文本处理

    14.1 字符 三种数值类型与 Char 实例的相互转换: static void Main() { Char c; Int32 n; //方法一: 通过C#转型(强制类型转换)实现数字与字符的相互转 ...

  3. [CLR via C#]14. 字符、字符串和文本处理

    一.字符 在.NET Framewole中,字符总是表示成16位Unicode代码值,这简化了国际化应用程序的开发. 每个字符都表示成System.Char结构(一个值类型) 的一个实例.System ...

  4. 重温CLR(十) 字符、字符串和文本处理

    本章将介绍.net中处理字符和字符串的机制 字符 在.NET Framewole中,字符总是表示成16位Unicode代码值,这简化了国际化应用程序的开发. 每个字符都表示成System.Char结构 ...

  5. CLR via C#字符串和文本处理

    一.字符   在.NET Framewole中,字符总是表示成16位Unicode代码值,这简化了国际化应用程序的开发.   每个字符都表示成System.Char结构(一个值类型) 的一个实例.Sy ...

  6. 读书笔记—CLR via C#字符串及文本

    前言 这本书这几年零零散散读过两三遍了,作为经典书籍,应该重复读反复读,既然我现在开始写博了,我也准备把以前觉得经典的好书重读细读一遍,并且将笔记整理到博客中,好记性不如烂笔头,同时也在写的过程中也可 ...

  7. 《Python CookBook2》 第一章 文本 - 过滤字符串中不属于指定集合的字符 && 检查一个字符串是文本还是二进制

    过滤字符串中不属于指定集合的字符 任务: 给定一个需要保留的字符串的集合,构建一个过滤函数,并可将其应用于任何字符串s,函数返回一个s的拷贝,该拷贝只包含指定字符集合中的元素. 解决方案: impor ...

  8. Python3-Cookbook总结 - 第二章:字符串和文本

    第二章:字符串和文本 几乎所有有用的程序都会涉及到某些文本处理,不管是解析数据还是产生输出. 这一章将重点关注文本的操作处理,比如提取字符串,搜索,替换以及解析等. 大部分的问题都能简单的调用字符串的 ...

  9. [转]python3字符串与文本处理

    转自:python3字符串与文本处理 阅读目录 1.针对任意多的分隔符拆分字符串 2.在字符串的开头或结尾处做文本匹配 3.利用shell通配符做字符串匹配 4.文本模式的匹配和查找 5.查找和替换文 ...

随机推荐

  1. cmd命令行窗口和文件目录资源管理器快速切换

    本文主要描述如何在指定目录下快速打开当前路径的命令行窗口和在命令行中快速打开指定目录的资源管理器两种快捷方法. 1.在指定目录下快速打开当前路径的命令行窗口 2.在命令行中快速打开当前目录的资源管理器 ...

  2. Qt使用QAxObject快速批量读取Excel内容

    网上各种教程用的方法主要是如下这一句: QAxObject * range = worksheet->querySubObject("Cells(int,int)", 1, ...

  3. 异想家Win10系统安装的软件与配置

    1.C盘推荐一个硬盘,256G,安装好驱动,显卡配置好高性能,激活Win10,屏蔽WIn10驱动更新(Show or hide updates.diagcab),改电脑名称为Sandeepin-PC. ...

  4. JVM第一弹

    JVM第一弹 基本概念 JVM是可运行java代码的假想计算机,包括一套字节码指令集,一组寄存器,一个栈,一个垃圾回收.堆和一个存储方法域.JVM是运行在操作系统之上的,它与硬件没有直接的交互. 运行 ...

  5. Bootstrap自带的那些常用插件

    1.Bootstrap自带的那些常用插件. 1.1模态框 模态框的HTML代码放置的位置 务必将模态框的HTML代码放在文档的最高层级内(也就是说,尽量作为 body 标签的直接子元素),以避免其他组 ...

  6. 平滑重启更新(GR机制)

    平滑重启更新(GR机制) 什么是平滑启动机制 是一种在协议重启时保证转发业务不中断的机制. 什么时候用到平滑重启 平滑重启一般应用于业务更新或者版本发布过程中,能够避免因为代码发布重启服务导致的暂时性 ...

  7. HDU_1495_模拟

    http://acm.split.hdu.edu.cn/showproblem.php?pid=1495 自己用模拟写的,先除以三个数的最大公约数,弱可乐为奇数,则无解,然后开始模拟. 利用大杯子和小 ...

  8. 《Python学习手册 第五版》 -第4章 介绍Python对象类型

    本章的内容主要是介绍了Python的核心对象类型,后续的5.6.7.8.9章针对这些核心类型分别展开详细的说明 本章我认为重要的有几点 1.作者有谈到Python的知识结构,这个我感觉是一个大框架,可 ...

  9. python笔记21(面向对象课程三)

    今日内容 嵌套 特殊方法:__init__ type/isinstance/issubclass/super 异常处理 内容回顾 def login(): pass login() class Acc ...

  10. SpringCloud五大神兽之Eureka

    注册中心概述 什么是注册中心? 相当于服务之间的'通讯录',记录了服务和服务地址之间的映射关系.在分布式架构中服务会注册到这里.当服务需要调用其他服务时,就在注册中心找到其他服务的地址,进行调用 注册 ...