3月5日 String类

1.Length    --取字符串长度

string x = Console.ReadLine();
            int i = x.Length;
            Console.WriteLine(i);

执行结果:

2.Substring(从哪一位开始,截取几位)   --截取字符串,从第六位往后截取3位,包括空格在内

string x = Console.ReadLine();
           string p = x.Substring(6, 3);
           Console.WriteLine(p);

执行结果:

3.Trim()  --压缩前后空格     TrimStart():压缩前面空格    TrimEnd():压缩后面空格

string x = Console.ReadLine();
         int i = x.Length;
         Console.WriteLine(i);

string s = x.Trim();
         Console.WriteLine(s);

int p = s.Length;
         Console.WriteLine(p);

执行结果:

4.ToLower()所有大写都转成小写

ToUpper()所有小写都转成大写

string x = Console.ReadLine();
       x = x.ToLower();
       Console.WriteLine(x);
       x = x.ToUpper();
       Console.WriteLine(x);

执行结果:

5.Replace("被替换的","替换为")  --替换字符  (前面是被替换的,后面是替换成的)

string x = Console.ReadLine();
          string p = x.Replace("a", "A");
          Console.WriteLine(p);

执行结果:

6.Remove(从哪一位开始,删除几位)   --删除字符

string x = Console.ReadLine();
            x=x.Remove(5,3);
            Console.WriteLine(x);

执行结果:

7.Split(Char[])    --分隔字符串

string words = "This is a list of words, with: a bit of punctuation" +
                     "\tand a tab character.";
            string[] str = words.Split(new Char[] { ‘ ‘, ‘,‘, ‘.‘, ‘:‘, ‘\t‘ });
            foreach (string s in str)
            {
                if (s.Trim() != "")
                    Console.WriteLine(s);
            }

执行结果:

时间: 2024-08-26 19:16:40

3月5日 String类的相关文章

2016年10月10日--string、Math类、Random随机数、DateTime、异常保护

string string.length; //得到string长度 string.Trim(); //去掉string前后的空格 string.TrimStart(); //去掉string前的空格 string.TrimEnd(); //去掉string后的空格 string.ToLower(); //将string所有大写字母转换为小写 string.ToUpper(); //将string所有小写字母转换为大写 sting.IndexOf(""); //查找第一次出现该字符或者

学习笔记(11月06日) --类

四周一次课(11月6日) 一.类的一般形式 创建类我们一般用class关键字来创建一个类,class后面跟类名字,可以自定义,最后以冒号结尾,如下所示: class ClassName: '''类的说明''' 类的内容 类的内容可以写类的全局变量,方法等 例子: class ren(object):     '''this is a new class'''     name = 'meinv'     sex = 'woman' a = ren() print(type(a)) print(a

3月11日 异常语句 类

一.异常语句 try catch finally try :保护执行里面的代码,若其中有一句有误,直接跳转到catch,不会管下面的内容 try { Console.Write("请输入一个整数"); int a=int.Parse(Console.ReadLine()); Comsole.WriteLine("hello"); } catch//try中发现异常,直接执行,若try中无错则不执行 { Console.WriteLine("输入有误!&qu

2017年11月4日 vs类和结构的区别&哈希表&队列集合&栈集合&函数

类和结构的区别 类: 类是引用类型在堆上分配,类的实例进行赋值只是复制了引用,都指向同一段实际对象分配的内存 类有构造和析构函数 类可以继承和被继承 结构: 结构是值类型在栈上分配(虽然栈的访问速度比较堆要快,但栈的资源有限放),结构的赋值将分配产生一个新的对象. 结构没有构造函数,但可以添加.结构没有析构函数 结构不可以继承自另一个结构或被继承,但和类一样可以继承自接口 //哈希表 //不规定长度 不规定类型 不规定建类型 //Hashtable j = new Hashtable(); //

3月12日 随机数类 Random

Random: Random ran=new Random();//初始化 int a=ran.Next(10); Console.WhiteLine(a); Console.ReadLine(); //随机出验证码,对照输入,判断是否正确: string s = "abcdefghijklmnopqistuvwxyzABCDEFGHIJKLMNOPQISTUVWXYZ0123456789"; Random ran = new Random(); for (; ; ) { string

2014年12月8日-configuration类以及openSession和getCurrentSession的区别

Hibernate的configuration类: configuration类是用来加载hibernate配置文件的,默认的是读取hibernate.cfg.xml配置文件的信息. Configuration cfg = new Configuration().configure(); //Configuration cfg = new AnnotationConfiguration().configure(); //使用annotation的加载配置文件的方法 SessionFactory

2016年4月27日_JAVA学习笔记_JAVA中常见的API(一)String

1.String在JAVA中是一个单独的类,只不过是一种特殊的,专门用来表示字符串的类.之前接触到的创建方式很简单,就是跟C语言中创建变量一样, String aString = "This is a String."; //变量类型为String,变量名为aString,内容为"This is a String.". 在学习API时,接触到了一种特别的创建方式.因为String是一个类,那么就肯定可以用其构造器方法来创建相应的对象. String aString

2015年11月26日 Java基础系列(一)之String与StringBuffer与StringBuilder的区别

序,StringBuffer是线程安全的,StringBuilder是线程不安全的,但是StringBuilder操作速度快,因此在使用时要根据场景合理选择. StringBuffer和StringBuilder都是可变字符串,当对字符串的操作更多是在添加.删除或更换子串的操作,则用StringBuffer或StringBuilder. String是一个类,实现了 Serializable, Comparable<String>, CharSequence. 一.String类 1 创建St

日期类的时间从为什么是从1970年1月1日(格林威治时间)

I suspect that Java was born and raised on a UNIX system.UNIX considers the epoch (when did time begin) to be midnight, January 1, 1970.是说java起源于UNIX系统,而UNIX认为1970年1月1日0点是时间纪元. 但这依然没很好的解释"为什么",出于好奇,继续Google,总算找到了答案: http://en.wikipedia.org/wiki/