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. RestTemplate---Spring提供的轻量Http Rest 风格API调用工具

    前言 今天在学习Spring Cloud的过程中无意发现了 RestTemplate 这个Spring 提供的Http Rest风格接口之间调用的模板工具类,感觉比Apache提供的HttpClien ...

  2. sas9.2 windows7系统 10年11月后 建立永久数据集时,提示:“用户没有与逻辑库相应的授权级别

    先把你这个逻辑库删掉,在桌面创立空的新文件夹,然后用这个新文件夹在sas里新建逻辑库,名字照旧,再把你要的数据放进空文件夹就好了

  3. Docker基础内容之镜像

    概念 镜像是一个包含程序运行必要依赖环境和代码的只读文件,它采用分层的文件系统,将每一次改变以读写层的形式增加到原来的只读文件上.镜像是容器运行的基石. 下图展示的是Docker镜像的系统结构.其中, ...

  4. oracle问题之SYSTEM表空间不足 (二)

    杂症二.SYSTEM表空间不足报错 一.杂症: PLSQL登录,报错: ORA-00604: 递归 SQL 层  出现错误 ORA-01653: 表.无法通过(在表空间中)扩展 ORA-02002: ...

  5. P4语言环境安装(一)前端编译器p4c、后端编译器p4c-bm2-ss

    这个P4安装环境是在2020-2-8安装的,安装环境卡了我好几天,把遇到的问题记录下来,有需要的同学可以参考一下,要是说错了或者有问题的话,评论或mail:guidoahead@163.com联系我都 ...

  6. Django面试集锦(51-65)

    目录 51.Django中filter和exclude的区别? 52.Django中values和values_list的区别? 53.如何使用django orm批量创建数据? 54.Django的 ...

  7. Android: Fragment编程指南

    本文来自于www.lanttor.org Fragment代表了Activity里的一个行为,或者Activity UI的一部分.你可以在一个activity里构造多个Fragment,也可以在多个a ...

  8. [win]更改win终端编码

    更改cmd的编码格式 chcp: 显示当前的编码格式 chcp 65001: 更改当前编码格式为UTF-8 字体选择`Lucida Console` 更改PowerShell编码格式(from zhi ...

  9. 推荐一个很棒的开源工作流elsa-core

    开源项目orchard主要开发人员Sipke Schoorstra 开源了一个netcore 工作流项目,地址:https://github.com/elsa-workflows/elsa-core, ...

  10. linux入门系列11--Centos7网络服务管理

    通过前面文章的学习已经掌握了Linux系统配置管理的知识,本文讲解Centos7网络配置知识. Linux要对外提供服务,需要保证网络通信正常,因此需要正确配置网络参数.本文将讲解如何使用Networ ...