C#02字符串处理

规则:

1. 方法命名是用帕斯卡
2. 变量使用驼峰
3. is开头的都是bool I开头的都是接口 abs开头的是抽象类 
4. 弄一个变量在这里必须赋初值

char.IsDigit: 判断该char是否是10进制数值

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

namespace day02
{
    class Program
    {
        //判断该字符串是否是10进制数值
        static bool IsNumber(string s)
        {
            bool returnValue = true; // 返回值
            for (int i = 0; i < s.Length; i++)
            {
             //   s[i] = 'a';
                if (!char.IsDigit(s[i]))
                {
                    returnValue = false;
                    break;
                }
            }
            //返回对应的数值
            return returnValue;
        }

        static void Main(string[] args)
        {
            //int.TryParse()
            Console.WriteLine(IsNumber("111124465767"));
        }
    }
}

字符串 –> 特殊的字符数组, 有下标 能遍历但是不能改

字符串

转义字符前加@
1. 比较字符串(忽略大小写) Compare: 0代表相等 非0 代表不相等 (strA,StrB, true) 忽略大小写
2. 比较字符串(不忽略大小写)

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

namespace 字符串比较
{
    class Program
    {
        static void Main(string[] args)
        {
            string str01 = "ABC";
            string str02 = "ABc";
           // Console.WriteLine(str02.Equals(str02));
           //返回为0  代表        01 == 02                                       true 代表忽略大小写
        int tmpInt  =   string.Compare(str01, str02, true);
            Console.WriteLine(tmpInt);
        }
    }
}
3. 分割字符串               split(char/char[]) 按照char/char[] 进行分割, 返回一个字符串数组
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 字符串分割
{
    class Program
    {
        static void Main(string[] args)
        {
            string stuInfo = "张三-男-18-12345678901-8888@qq.com";
            string[] infos = stuInfo.Split('-');
            Console.WriteLine(infos.Length);



            Console.WriteLine("===================");
            foreach (string s in infos)
            {

                Console.WriteLine(s);
            }
        }
    }
}
4. 查找字符串                indexof/lastindexof 返回其所在下标位置 若-1 就代表该字符串没有对应的东东
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 查找
{
    class Program
    {
        static void Main(string[] args)
        {
            string str = "C#编程我喜欢,C#编程我呵呵哒C#";
         //int tmpIndex =    str.IndexOf("C#");
         int tmpIndex =    str.LastIndexOf("C#");
            Console.WriteLine(tmpIndex);
            Console.ReadLine();
        }
    }
}
5. 替换字符串 都换           Replace(old,new) 都换
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 替换字符串
{
    class Program
    {
        static void Main(string[] args)
        {
            string str = "呵呵哒,呵呵哒你好世界,呵呵哒";
            string newStr = str.Replace("呵呵哒", "C# 编程");
            Console.WriteLine(newStr);
            Console.ReadLine();
        }
    }
}
6. 截取字符串                01.Substring(index, count) --> 截取之间的字符串        02.Substring(index) --> 截取之后的字符串
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 截取字符串
{
    class Program
    {
        static void Main(string[] args)
        {
            string str = "你好世界,呵呵哒";
            string newStr = str.Substring(0, 3);
            //string newStr = str.Substring(5);
            Console.WriteLine(newStr);
            Console.ReadLine();
        }
    }
}
7. 空格处理                   str.Trim() --> 去掉字符串前后空格
8. 空字符串
       string str01 = ""; // 空值
        string str02 = string.Empty; // 空值
        string str03; // 空对象
Equals true 相等
string.IsNullOrEmpty(str) 判断str是否是空对象或者是empty
out: 传出参数,在方法内部必须赋值
ref: 传入参数,在方法外部必须赋值

OOP编程

对象是一个抽象概念, 用代码表示就是new出来的基本都是对象
在unity中最能体现OOP三大特性中的哪一个?继承
命名空间就是包含功能类似的class的一个集合
public: 对象和子类都能访问
protected:子类可以访问,子类对象不能访问
private: 对象和子类都不能访问
base: 就是访问父类成员(公开和保护的)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace OOP01
{
    class MyClass01
    {
        public int PubInt;
        protected int ProInt;

        private int _PriInt;

        public int PriInt
        {
            get { return _PriInt; }
            set { _PriInt = value; }
        }


        protected void MyClass01Func()
        {
            Console.WriteLine("MyClass01Func");
        }
    }

    class MyClass: MyClass01
    {
        //子类调用父类方法应该在方法中调用
        public void ShowMyClass01Func()
        {
            base.MyClass01Func();
        }
    }



    class Program
    {
        static void Main(string[] args)
        {
            MyClass myClass = new MyClass();

        }
    }
}

封装:
私有

作业

  1. 求负数的次方
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 李国贺9._11
{
    //求负数的次方
    class Program
    {
        static int CiFang(int number, int cifang)
        {
            int returnValue = 0;
            if (number == 0)
            {
                return returnValue;
            }
            if (cifang == 0)
            {
                returnValue=1;
                return returnValue;
            }
            if (cifang == 1)
            {
                returnValue = number;
                return returnValue;
            }
            if (number < 0 && cifang == 1)
            {
                returnValue = number;
                return returnValue;
            }
            returnValue = number;
            for (int i = 1; i < cifang; i++)
            {
                returnValue *= number;
            }
            return returnValue;
        }
        static void Main(string[] args)
        {
            Console.WriteLine(CiFang(-3,3));
            Console.ReadLine();
        }
    }
}
  1. 按照要求输入 exp: name,sex,age,phone,email
    张三,男,18,12345678901,8888@qq.com
    2.1: 年龄 0~120
    2.2: 电话: 11位
    2.3: 邮箱: 必须是qq邮箱
    若不满足上述条件统统重新来过
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
//按照要求输入 exp: name,sex,age,phone,email
//           张三,男,18,12345678901,8888@qq.com
//           2.1: 年龄 0~120
//           2.2: 电话: 11位
//           2.3: 邮箱: 必须是qq邮箱
//    若不满足上述条件统统重新来过

namespace _2
{
    class Program
    {
        static void Main(string[] args)
        {
            bool x=true;
            do
            {
                Console.WriteLine("请依次输入name,sex,age,phone,email并以','隔开");
                string shuru = Console.ReadLine();
                string[] infos = shuru.Split(',');
                //Console.WriteLine(infos.Length);
                try
                {
                    Convert.ToInt32(infos[2]);
                    Convert.ToInt64(infos[3]);
                }
                catch
                {
                    Console.WriteLine("您的输入格式有误,请重新输入");
                    x = false;
                }
                if (Convert.ToInt32(infos[2]) <= 0 || Convert.ToInt32(infos[2]) >= 120)
                {
                    Console.WriteLine("您的输入年龄有误,请重新输入");
                    x = false;
                }
                else if (infos[3].Length != 11)
                {
                    Console.WriteLine("您的输入电话号码有误,请重新输入");
                    x = false;
                }
                else if (infos[4].LastIndexOf("@qq.com")==-1)
                {
                    Console.WriteLine("您的输入qq邮箱有误,请重新输入");
                    x = false;
                }
                foreach (string s in infos)
                {
                   Console.WriteLine(s);
                }
                Console.ReadLine();
            } while (x==false);
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值