关于Java中字符串最基础的东东

强制性转换在前篇中已经写到,将浮点型转换成整型,例如
double x=9.997;
int nx=(int) x;
这样的话nx的值变回为9;如果想对浮点类型进行四舍五入的计算,得到整数,那么我们就使用Math.round的方法,例如下面:
double x=9.997;
int nx=(int) Math.round(x);
这样nx的变量便是10了;
字符串,从概念上来讲,Java的字符串就是Unicode字符序列,每个用双括号括起来的字符串都是String类的一个实例:
String=”“;//这是一个空的字符串
String=”helzilo”;
(1).字串。在String中的substring方法中从一个较大的可以字符串提取到一个字串。
String greeting=”hello”;
String s=greeting.substring(0, 3);
这样是创建了一个“hel”组成的字符串。
(2)、拼接
String expletive =”Expletive”;
String PG13=”deleted”;
String massage=”expletive+PG13”;
上述代码将Expletivedeleted赋值给了massage;
(3)、修改字符串
String greeting=”hello”;
greeting=greeting.substring(0, 3)+”p!”;
这样将greeting的当前值修改为help!
(4)检测字符串是否相等
用equals方法检测两个字符串是否相等。s.equals(t) 如果s与t相等,则返回ture,否则返回fslse,
字符串常量
“hello”.equals(greeting);
而想要检测两个字符串相等而不区分大小写,可以用equalsIgnoreCase的方法,例如
“hello”.equalsIgnoreCase(“Hello”);
(5)、空串与Null串
空串 if(str.lenth()==0)或者if(str.equals(“”))
Null串 if(str==null)
(6)、代码点和代码单元
String greeting=”hello”;
int n=greeting.lenth();// is 5
得到实际的长度
int cpCount=greeting.codePointCount(0,greeting.lenth());

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值