zy89、90_C#中字符串及控制字符串的常用函数

1.字符串

1.1程序代码

static void Main(string[] args)
{
    //********************string类*******************
    string sentence = "Hello!";
    char letter = sentence[0];
    Console.WriteLine(letter);

    char[] letter1 = { 'C', 'H', 'I', 'C', 'K', 'E', 'N' };
    string ss = new string(letter1);
    Console.WriteLine(ss);

    string ss1 = new string('A', 10);//重复执行10遍
    Console.WriteLine(ss1);

    Console.WriteLine("The letters in \"Hello\" are:");
    for (int i = 0; i < sentence.Length; i++)
    {
        char letter2 = sentence[i];
        Console.Write(letter2 + "\t");
    }

    //********************复制字符串*******************
    // =:
    string sentence1 = "就快被融化";
    string sentence2 = sentence1;
    //sentence1 = "再靠近一点";
    Console.WriteLine("\n{0}\n{1}", sentence1, sentence2);

    //Copy()
    string sentence3 = "再多看一眼";
    string sentence4 = string.Copy(sentence3);
    Console.WriteLine(sentence3);
    Console.WriteLine(sentence4);

    //CopyTo()
    string source = "someone";
    char[] destination = { '*', '*', '*', '*', '*', '*', '*', '*', '*', '*' };
    int sourceIndex = 2;
    int destinationIndex = 3;
    int count = 2;
    source.CopyTo(sourceIndex, destination, destinationIndex, count);
    for (int i = 0; i < destination.Length; i++)
    {
        Console.Write(destination[i]);
    }

    //********************比较字符串*******************
    //1.相等运算符==
    string string5 = "Hello";
    string string6 = "hello";
    if (string5 == string6)
    {
        Console.WriteLine("\nEqual");
    }
    else
    {
        Console.WriteLine("\nUnequal");
    }
    //2.Equsls()
    string string7 = "Hello";
    if (string7.Equals("Hello"))
    {
        Console.WriteLine("\nEqual");
    }
    else
    {
        Console.WriteLine("\nUnequal");
    }
    //3.静态Equal()
    string string8 = "Dog";
    string string9 = "dog";
    if (string.Equals(string8, string9))
    {
        Console.WriteLine("\nEqual");
    }
    else
    {
        Console.WriteLine("\nUnequal");
    }
    //4.CompareTo()
    string string10 = "better";
    string string11 = "best";
    switch (string10.CompareTo(string11))
    {
        case -1: Console.WriteLine(string10 + "<" + string11); break;
        case 0: Console.WriteLine(string10 + "=" + string11); break;
        case 1: Console.WriteLine(string10 + ">" + string11); break;
        default: Console.WriteLine("********"); break;
    }
}

2.控制字符串的常用函数

2.1程序代码

static void Main(string[] args)
{

    验证字符串的首尾
    //1.StartWith()判断字符串是否以特定的子串开头
    string[] strings = { "started", "starting", "ended", "ending" };
    for (int i = 0; i < strings.Length; i++)
    {
        if (strings[i].StartsWith("st"))
        {
            Console.WriteLine("\"{0}\" starts with \"st\"", strings[i]);
        }
    }
    //2.EndsWith()判断字符串是否以特定的子串结尾
    for (int i = 0; i < strings.Length; i++)
    {
        if (strings[i].EndsWith("ed"))
        {
            Console.WriteLine("\"{0}\" starts with \"ed\"", strings[i]);
        }
    }
    定位字符或子串
    //1.IndexOf()找到指定字符或子串在字符串中第一次出现的位置。如果找不到,返回-1
    string sentence = "You love to hear Let her go.";
    int index1 = sentence.IndexOf('o', 3);//从第三个字符之后开始找指定元素
    int index2 = sentence.IndexOf("You", 20);
    Console.WriteLine(index1);
    Console.WriteLine(index2);

    //2.LastIndexOf()找到指定字符或子串在字符串中最后一次出现的位置。如果找不到,返回-1
    int index3 = sentence.IndexOf('o', 6);//从后向前搜索
    int index4 = sentence.IndexOf("You", 20,4);//向前搜索4个字符
    Console.WriteLine(index3);
    Console.WriteLine(index4);

    //3.IndexOfAny()同时搜索多个指定的字符,搜索到任意一个停止
    char[] searchLetters= { 'e', 'h' };
    int index5 = sentence.IndexOfAny(searchLetters);
    int index6 = sentence.IndexOfAny(searchLetters,10);//第十个元素开始
    int index7 = sentence.IndexOfAny(searchLetters,10,3);
    Console.WriteLine(index5);
    Console.WriteLine(index6);
    Console.WriteLine(index7);

    //4.LastIndexOfAny()
    int index8 = sentence.LastIndexOfAny(searchLetters);
    int index9 = sentence.LastIndexOfAny(searchLetters, 10);
    int index10 = sentence.LastIndexOfAny(searchLetters, 10, 3);
    Console.WriteLine(index8);
    Console.WriteLine(index9);
    Console.WriteLine(index10);

    ///截取子串Substring()
    string sub=sentence.Substring(12);//截取12之后的字符串
    Console.WriteLine(sub);
    string sub1=sentence.Substring(5,4);//在第5个之后截取4个
    Console.WriteLine(sub1);

    //拆分字符串Split()
    char[] seperator = { ' ','o' };//遇到字符断开
    string[] words=sentence.Split(seperator);
    foreach (string word in words)
    {
        Console.WriteLine(word);
    }

    //更改大小写
    string string1 = "ok";
    string string2=string1.ToUpper();
    Console.WriteLine(string2);
    string string3 = string2.ToLower();
    Console.WriteLine(string3);

    //修改字符串
    string string4 = "tree";
    string string5 = string4.Insert(1, "h");
    Console.WriteLine(string5);
    string string6 = string4.Replace('e', 'd');
    Console.WriteLine(string6);
    string string7 = string4.Replace("re", "ffd");
    Console.WriteLine(string7);
    string string8 = string4.Remove(2);//2以后所有元素
    Console.WriteLine(string8);
    string string9=string4.Remove(1,2);//1以后的2个字符
    Console.WriteLine(string9);

    插入格式化变量
    int i1 = 360;
    int j1 = 60;
    string result = string.Format("{0,6}\n+{1,5}\n----------\n{2,6}", i1, j1, i1 + j1);//{0}6个空间
    Console.WriteLine(result);
}

``` public class Main { public static void main(String[] args) { for (char i = 'z'; i >= 'a'; i--) { for (char j = 'z'; j >= 'a'; j--) { System.out.print(i); System.out.print(j); System.out.print(" "); } } } } ``` 输出结果为: ``` zz zy zx zw zv zu zt zs zr zq zp zo zn zm zl zk zj zi zh zg zf ze zd zc zb za yz yy yx yw yv yu yt ys yr yq yp yo yn ym yl yk yj yi yh yg yf ye yd yc yb ya xz xy xx xw xv xu xt xs xr xq xp xo xn xm xl xk xj xi xh xg xf xe xd xc xb xa wz wy wx ww wv wu wt ws wr wq wp wo wn wm wl wk wj wi wh wg wf we wd wc wb wa vz vy vx vw vv vu vt vs vr vq vp vo vn vm vl vk vj vi vh vg vf ve vd vc vb va uz uy ux uw uv uu ut us ur uq up uo un um ul uk uj ui uh ug uf ue ud uc ub ua tz ty tx tw tv tu tt ts tr tq tp to tn tm tl tk tj ti th tg tf te td tc tb ta sz sy sx sw sv su st ss sr sq sp so sn sm sl sk sj si sh sg sf se sd sc sb sa rz ry rx rw rv ru rt rs rr rq rp ro rn rm rl rk rj ri rh rg rf re rd rc rb ra qz qy qx qw qv qu qt qs qr qq qp qo qn qm ql qk qj qi qh qg qf qe qd qc qb qa pz py px pw pv pu pt ps pr pq pp po pn pm pl pk pj pi ph pg pf pe pd pc pb pa oz oy ox ow ov ou ot os or oq op oo on om ol ok oj oi oh og of oe od oc ob oa nz ny nx nw nv nu nt ns nr nq np no nn nm nl nk nj ni nh ng nf ne nd nc nb na mz my mx mw mv mu mt ms mr mq mp mo mn mm ml mk mj mi mh mg mf me md mc mb ma lz ly lx lw lv lu lt ls lr lq lp lo ln lm ll lk lj li lh lg lf le ld lc lb la kz ky kx kw kv ku kt ks kr kq kp ko kn km kl kj ki kh kg kf ke kd kc kb ka jz jy jx jw jv ju jt js jr jq jp jo jn jm jl jk jj ji jh jg jf je jd jc jb ja iz iy ix iw iv iu it is ir iq ip io in im il ik ij ii ih ig if ie id ic ib ia hz hy hx hw hv hu ht hs hr hq hp ho hn hm hl hk hj hi hh hg hf he hd hc hb ha gz gy gx gw gv gu gt gs gr gq gp go gn gm gl gk gj gi gh gg gf ge gd gc gb ga fz fy fx fw fv fu ft fs fr fq fp fo fn fm fl fk fj fi fh fg ff fe fd fc fb fa ez ey ex ew ev eu et es er eq ep eo en em el ek ej ei eh eg ef ee ed ec eb ea dz dy dx dw dv du dt ds dr dq dp do dn dm dl dk dj di dh dg df de de dc db da cz cy cx cw cv cu ct cs cr cq cp co cn cm cl ck cj ci ch cg cf ce cd cc cb ca bz by bx bw bv bu bt bs br bq bp bo bn bm bl bk bj bi bh bg bf be bd bc bb ba az ay ax aw av au at as ar aq ap ao an am al ak aj ai ah ag af ae ad ac ab aa ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值