数组的用法和字符串的用法

来源:互联网 发布:js鼠标经过图片放大 编辑:程序博客网 时间:2024/04/16 12:35

数组的定义  例如定义一个整数数组1. int【】num={1,32,3,4};2int【】 num=new int【数组的长度】;3,int[] nums = new int[3]{5,3,8}

遍历数组中的每一个元素用foreach

例如foreach(int numbers in num){console.writeLine(numbers);}

字符串的处理

字符串有一个重要的特性 是不可变性 字符串一旦被声明就不能再被改变

1, 首先定义个字符串 如  string str=“3323323eb”;

 截取字符串是用substring 比如定义个字符串汲取该字符串的指定长度

string  s=str.substring(0,4);

2, 查找某个字符的位置

 比如查找一题中字符第一个e的位置

int a=str.lastof('e')

3,比较两个字符串的大小

StringComparision 不区分大小写

string s="Aabc“;

string b=”aBbc";

if(s.Equals(b,StringComparison.OrdinalIgnoreCase))

{

    console.write("两个数相等");

}

4,ToLower得到字符串的小写字母

     ToUpper 得到字符串的大写字母

bool contains(string value)判断字符串中是否含有value

bool Startswith(string value)判断字符串是否已value开头

bool  EndsWith(string value) 判断字符串是否已value结尾

5,可变参数

 static void Main(string[] args)
        {

            Console.WriteLine("最大值为{0}",getnum(1,3,4,5,3,2,6));
            Console.ReadLine();

         }

public static  int getnum(params)

{

           int max=0;
            for (int i = 0; i < a.Length; i++)
            {
                if (max< a[i])
                {
                    max = a[i];
                }
            }
           
            return max;

}

 

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/hbsiyaozishun/archive/2011/02/23/6203374.aspx