zoukankan      html  css  js  c++  java
  • String类的方法应用

    String类的几个方法的应用示例:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;

    namespace ConsoleApplication1
    {
    class Program
    {
    static void Main(string[] args)
    {
    string str1 = "Dood morning!";
    string str2 = "How are you?";
    //输出两个字符串
    Console.WriteLine(str1);
    Console.WriteLine(str2);
    //输出两个字符串的长度
    Console.WriteLine("字符串str1的长度为{0}", str1.Length);
    Console.WriteLine("字符串str2的长度为{0}", str2.Length);
    //比较字符串的长度
    Console.WriteLine(str1.CompareTo(str2));
    //判断字符串是否相等
    Console.WriteLine(str1.Equals(str2));
    //输出和给定字符串在原字符串中的位置
    Console.WriteLine("字符串"are"在str2中的位置是:{0}", str2.IndexOf("are"));
    //输出和给定字符串在原字符串中最后出现的位置
    Console.WriteLine("字符串"are"在str2中最后出现出现的位置是:{0}", str2.LastIndexOf("are"));
    //判断字符串是否以给定字符串开始
    Console.WriteLine(str1.StartsWith("Good"));
    //判断字符串是否以给定字符串结尾
    Console.WriteLine(str1.EndsWith("morning"));
    //提取字符串,从第五位开始,提取七个字符,输出新字符串
    Console.WriteLine(str1.Substring(5, 7));
    //在原字符串中从第八位开始,插入给定字符串
    Console.WriteLine(str1.Insert(8, "hello"));
    //从原字符串的第六位开始,删除三个字符
    Console.WriteLine(str1.Remove(6, 3));
    //去掉字符串前后的空格
    Console.WriteLine(str1.Trim());
    Console.ReadLine();
    }
    }
    }

    运行结果如下:


  • 相关阅读:
    C# 不用添加WebService引用,调用WebService方法
    贪心 & 动态规划
    trie树 讲解 (转载)
    poj 2151 Check the difficulty of problems (检查问题的难度)
    poj 2513 Colored Sticks 彩色棒
    poj1442 Black Box 栈和优先队列
    啦啦啦
    poj 1265 Area(pick定理)
    poj 2418 Hardwood Species (trie树)
    poj 1836 Alignment 排队
  • 原文地址:https://www.cnblogs.com/liyajuan333/p/11649501.html
Copyright © 2011-2022 走看看