140821●字符串、数字、日期及应用举例

brerk   彻底终断循环,跳出for语句

continue  中断当前循环,进行下一循环

字符串

  .Length         字符串长度

  .TrimStart()      截去开头空格

  .TrimEnd()        截去结尾空格

  .Trim()          截去开头跟结尾的空格

  .ToUpper()        全部大写

  .ToLower()        全部小写

  .Substring(m,n)    从左边第m(m从0开始)个开始截取字符串,截取n个

  .Replace(“m”,”n”)   用n替换m

  .IndexOf()        指定的字符串第一次出现的位置

  .LastIndexOf()      指定的字符串最后一次出现的位置

  .StartsWith()      对比开头字符串

  .EndsWith()      对比结尾字符串

  .Contains()       判断当前字符串中是否包含传入值

  .ToString(“#.##”)    “#”表示此位置有数字则显示,没有则不显示

  .ToString(“#.00”)   “0”表示此位置有数字则显示,没有则填充0

  .ToString(“#,#.00”)    “,”表示从个位数往左,三位一分隔

数字

  Math.Ceiling()      表示进位。例2.3进位为3

  Math.Floor()       表示舍位。例2.3舍位为2

  Math.Round()      表示四舍五入

  Math.Sqrt()         开方

日期时间

  DateTime dt=new DateTime(1999,3,3,3,3,3);  //创建时间1999年3月3日3时3分3秒

  DateTime dt=DateTime.Now;   //提取当前时间

  .year   month……    提取相应年份、月份……

  .DayOfYear        一年的第几天

  .DayOfWeek        一周的第几天

  .Add_____        加

  .To_____

    例:Console.WriteLine(dt.ToString(“yyyy年MM月dd日hh时mm分ss秒ms毫秒”))

附:

  Ctrl+Shift+Space      常用于查看()内如何输入

            //1、判断闰年
            Console.Write("请输入年份:");
            int n = Convert.ToInt32(Console.ReadLine());

            if (n % 100 == 0)
            {
                if (n % 400 == 0)
                {
                    Console.WriteLine("是闰年");
                }
                else
                    Console.WriteLine("不是闰年");
            }
            else if (n % 4 == 0)
            {
                Console.WriteLine("是闰年");
            }
            else
                Console.WriteLine("不是闰年");
            //2、判断年、月、日是否输入正确
            Console.Write("请输入年:");
            int year = Convert.ToInt32(Console.ReadLine());
            int n = 1;

            if (year >= 0 && year <= 9999)
            {
                if (year % 100 == 0)
                {
                    if (year % 400 == 0)
                    {
                        n = 2;
                    }
                }
                else if (year % 4 == 0)
                {
                    n = 2;
                }
            }
            else
                Console.WriteLine("输入年份错误");
            //判断月份
            Console.Write("请输入月:");
            int month = Convert.ToInt32(Console.ReadLine());

            if (month < 1 || month > 12)
            {
                Console.WriteLine("月份输入错误");
            }
            //判断日期
            Console.Write("请输入日:");
            int day = Convert.ToInt32(Console.ReadLine());

            if (day >= 1 && day <= 31 && (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12))
            {
                Console.WriteLine("输入正确");
            }
            else if (day >= 1 && day <= 30 && (month == 4 || month == 6 || month == 8 || month == 9 || month == 11))
            {
                Console.WriteLine("输入正确");
            }
            else if (day >= 1 && day <= 28 && n == 1)
            {
                Console.WriteLine("输入正确");
            }
            else if (day >= 1 && day <= 29 && n == 2)
            {
                Console.WriteLine("输入正确");
            }
            else
                Console.WriteLine("输入错误");
            //3、随机生成4位验证码
            String s = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
            String show = "";
            Random ra = new Random();

            for (int i = 1; i <= 4; i++)
            {
                int n = ra.Next(s.Length);
                show = s.Substring(n, 1) + show;
            }
            Console.WriteLine(show);
            //4、身份证号中截取生日
            Console.WriteLine("请输入身份证号:");
            String id = Console.ReadLine();

            if (id.Length == 18)
            {
                Console.WriteLine("生日:");
                Console.WriteLine(id.Substring(6, 4) + "年" + id.Substring(10, 2) + "月" + id.Substring(12, 2)+"日");
            }
            else
                Console.WriteLine("输入错误");
            //5、判断邮箱对错
            Console.WriteLine("请输入邮箱:");
            String email = Console.ReadLine();

            String em = email.Trim();
            String em1 = em.ToLower();
            String s = "[email protected].";

            int i = 0;
            for (; i < em.Length; i++)
            {
                if (!(s.Contains(em.Substring(i, 1))))
                {
                    Console.WriteLine("含有非法字符!");
                    break;
                }
            }
            if (i == em.Length)
            {
                int a = em.IndexOf("@");
                int b = em.LastIndexOf("@");
                int c = em.IndexOf(".");
                int d = em.LastIndexOf(".");

                if (a == b && a > 0 && b < em.Length - 1)   //判断“@”的位置、数量
                {
                    if (c > 0 && d < em.Length - 1 && d - b > 1)    //判断“.”的位置
                    {
                        if (!(em.Substring(b, d - b - 1)).Contains("."))
                        {
                            if (a < 17 && d - b <= 10)      //限制用户名、域名长度
                            {
                                if (em1.EndsWith(".com") || em1.EndsWith(".cn"))
                                {
                                    Console.WriteLine("输入正确!");
                                }
                                else
                                    Console.WriteLine("输入错误!");
                            }
                            else
                                Console.WriteLine("输入错误!");
                        }
                        else
                            Console.WriteLine("输入错误!");
                    }
                    else
                        Console.WriteLine("输入错误!");
                }
                else
                    Console.WriteLine("输入错误!");
            }
            

140821●字符串、数字、日期及应用举例,布布扣,bubuko.com

时间: 2024-08-02 11:00:49

140821●字符串、数字、日期及应用举例的相关文章

140821 字符串,数字,日期及应用举例

brerk   彻底终断循环,跳出for语句 continue 中断当前循环,进行下一循环 s.Length          s的长度   s.Trim()          去除两边空格 s.TrimStart()      去除前面的空格 s.TrimEnd()           去除后面的空格   s.ToUpper()      字母变大写 s.ToLower()      字母变小写   s.Substring()    1.截取位置到最后  2.(截取位置,长度)   s.Sta

Java中字符串与日期之间的转换

项目过程中,经常遇到需要字符串格式的日期和Date类型的日期之间的相互转换.使用SimpleDateFormat类,可以方便完成想要的转换. SimpleDateFormat能够实现本地化的时间格式化及转换.从选定一个自定义的模式(pattren)开始,模式由已经定义好的 'A' to 'Z' 及 'a' to 'z'字母组成,也可以在模式中引入文本,但要使用’(单括号)括住.下图就是已经定义好的模式字母表: Letter Date or Time Component Presentation

oracle 字符串与日期转换sql

常用sql语句: select to_char(sysdate,'yy-mm-dd hh24:mi:ss') from dual;   //显示:08-11-07 13:22:42 select to_date('2005-12-25,13:25:59','yyyy-mm-dd,hh24:mi:ss') from dual; //显示:2005-12-25 13:25:59 获取系统时间: select sysdate from dual; 转换的格式: 表示year的:y  表示年的最后一位

获取周 星期 的第一天 最后一天 或者 月的 日期(字符串转日期,日期转字符串,日期加减)

获取周的第一天,最后一天 System.out.println(getStartEndDate("2016-05-01", 1)); 获取星期的第一天和最后一天 System.out.println(getStartEndDate("2016-05-01", 0)); public static String getStartEndDate(String aDay, int type) { SimpleDateFormat df = new SimpleDateFo

输入一个日期,判断这个日期在一年中是哪一天,是星期几,计算两个日期间的天数,使用字符串输出日期

之前写了一个博文(http://blog.csdn.net/shiwazone/article/details/45053739)是用基本函数实现的,这次使用类的设计方法,也就是面向对象的方法改写一下,并加入了日期转换成字符串的实现.这里的程序也可以解决编程珠玑习题3.4的问题. #include"calendar.h" int main() { Time t; t.initialTime(); t.Show(); t.StrShow(); Time t1; t1.initialTim

日期(字符串转日期,日期转字符串,日期加减)

这几天在研究字符串与指定类型的转换,阴差阳错地研究起 java 的日期应用了,记录下来,希望你有帮助. 根据指定格式的字符串,转换为 Date(可研究根据指定格式的字符串,转化为其他指定的类型,如 json 转换为 javaBean) 需要使用到的特殊类:import java.text.ParsePosition;     /**      * <p>Parses a string representing a date by trying a variety of different pa

Java字符串与日期互转

Java字符串与日期的相互转换 1.字符串转日期 字符串的格式与日期的格式一定要对应,并且字符串格式可以比日期格式多,但不能少,数字大小不自动计算日期.其中需要主要大小写 年yyyy 月MM 日dd 时HH 分mm 秒ss 毫秒SS String str = "2018/12/32"; SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd"); Date strDate = simp

SpringMVC后台使用对象接受参数字符串转日期

在springMVC配置文件中加入: <bean id="dateConvert" class="com.iomp.util.DateConvert"/> <bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean"> <property

mysql str_to_date字符串转换为日期

mysql内置函数,在mysql里面利用str_to_date()把字符串转换为日期. 示例:分隔符一致,年月日要一致 select str_to_date('2008-4-2 15:3:28','%Y-%m-%d %H:%i:%s'); select str_to_date('2008-08-09 08:9:30', '%Y-%m-%d %h:%i:%s'); 对于这个已经理解,但是为何查询字段时使用这种方法查询出来的数据为null???? 刚发现的问题,尝试在client上查询,发现可以出现