string 类型的使用方法

                     string a = "hello world!";
                       string b = a.Substring(6);   //获取下标为6以后的全部字符。
                       string c = a.Substring(6, 2); //获取下标为6以后的2个字符。
                       Console.WriteLine(b);
                       Console.WriteLine(c);
                       string d = a.Insert(0, "pkm ");     //在下标为0之前插入"pkm "
                       Console.WriteLine(d);
                       string e = d.Insert(d.Length - 1, " good"); //在下标为长度减1(最后一个)前插入" good"
                       Console.WriteLine(e);
                       string f = a.Remove(6);   //删除下标从6开始的后面的全部字符
                       Console.WriteLine(f);
                       string g = a.Remove(6, 2);   //删除下标从6开始的后面的2个字符
                       Console.WriteLine(g);
                       string h = a.Replace('!', '*');   //将!替换为 *
                       Console.WriteLine(h);
                       string i = a.Replace("hello", "HELLO");   //将hello 替换成 HELLO
                       Console.WriteLine(i);
                       //转义字符@   下面的   j==k
                       string j = "C:\temp\temp1\temp2";
                       Console.WriteLine(j);
                       string k = @"C:temptemp1temp2";
                       Console.WriteLine(k);
                       //StringBuilder是可变的字符串(string是不可变的,修改长度要重新开辟空间)
                       //定义使用方法如下:
                       StringBuilder l = new StringBuilder("hello", 50);//
                       l.Append(" world");
                       Console.WriteLine(l);
                       l.AppendFormat("{0} end", "!");
                       Console.WriteLine(l);
                       l.AppendLine(" one line");
                       Console.WriteLine(l);
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
String a="hello world"; //在java中有一个常量池,当创建String 类型的引用变量给它赋值时,java会到它的常量池中找"hello world"是不是在常量池中已存在。如果已经存在则返回这个常量池中的"hello world"的地址(在java中叫引用)给变量a 。注意a并不是一个对象,而是一个引用类型的变量。它里面存的实际上是一个地址值,而这个值是指向一个字符串对象的。在程序中凡是以"hello world"这种常量似的形式给出的都被放在常量池中。 String b=new String("hello world"); //这种用new关键字定义的字符串,是在堆中分配空间的。而分配空间就是由new去完成的,由new去决定分配多大空间,并对空间初始化为字符串"hello world" 返回其在堆上的地址。 通过上面的原理,可以做如下实验: String a="hello world"; String b="hello world"; String c=new String("hello world"); String d=new String("hello world"); if(a==b) System.out.println("a==b"); else System.out.println("a!=b"); if(c==d) System.out.println("c==d"); else System.out.println("c!=d"); //输出结果: a==b c!=d 为什么会出现上面的情况呢? String a="hello world"; String b="hello world"; 通过上面的讲解可以知道,a和b都是指向常量池的同一个常量字符串"hello world"的,因此它们返回的地址是相同的。a和b都是引用类型,相当于c语言里面的指针。java里面没有指针的概念,但是实际上引用变量里面放的确实是地址值,只是java为了安全不允许我们对想c语言中的那样对指针进行操作(如++ 、--)等。这样就有效的防止了指针在内存中的游离。 而对于 String c=new String("hello world"); String d=new String("hello world"); 来说是不相等的,他们是有new在堆中开辟了两块内存空间,返回的地址当然是不相等的了。如果我们要比较这两个字符串的内容怎么办呢?可以用下面的语句: if(c.equals(d)) System.out.println("c==d"); else System.out.println("c!=d"); //输出 c==d
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值