摘要:学习JAVA语言,字符串是不可缺少的与部分,字符串声明和赋值;获取字符串长度;字符串连接;字符串比较;字符串截取,使用substring()方法;字符串查找,使用indexOf()方法;字符串替换,replace(oldChar, newChar);字符串转换字符数组。掌握这些,我我们在JAVA语言的路上就会越走越宽。
字符串声明和赋值
String name = new String("京唐C");
或者
String name = "京唐C";
或者
String name=""; name = "京唐C";
获取字符串长度
使用length()方法
int lenth = name.length();
字符串连接
使用concat()方法
String user="觅音博客";
直接使用字符串字面量调用concat()方法
String string = "京唐C".concat("觅音博客");
或者使用“+”连接
System.out.println(name+user);
使用“+”连接时,如果存在各种数据参与运算,按照运算符从左到右进行运算,Java会根据“+”运算符两边的操作数据类型决定是进行算术运算还是字符创连接运算
第一行代码,先计算1.1+1=2.1,之后连接字符,结果为2.1京唐C
第二行代码,先连接字符京唐C+1.1,之后再连接字符1,结果为京唐C1.11
System.out.println(1.1+1+"京唐C");
字符串比较
使用equals()方法,区分大小写
System.out.println("京唐C".equals("京唐c"));
使用equalsIgnoreCase()方法,忽略大小写
System.out.println("京唐C".equalsIgnoreCase("京唐c"));
字符串截取,使用substring()方法
String blog = "京唐C觅音博客"; String sub1 = string.substring(2); String sub2 = blog.substring(2, 4); System.out.println(sub1); System.out.println(sub2);
字符串查找,使用indexOf()方法
String string2 = "I love Java"; String string3 = "o"; int serindex1 = string2.indexOf(string3); //从String2开始位置查找string3字符串 int serindex2 = string2.indexOf(string3,5); //从String2字符串第二个位置查找string3字符串 System.out.println(serindex1); //从0开始 System.out.println(serindex2); //查询不到显示-1
字符串替换,replace(oldChar, newChar)
char oldChar = 'I'; char newChar = 'U'; string2.replace(oldChar, newChar); System.out.print(string2.replace(oldChar, newChar));
字符串转换字符数组
String hellostring = "hello"; char[] helloArray = hellostring.toCharArray();
本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注职坐标编程语言JAVA频道!
您输入的评论内容中包含违禁敏感词
我知道了
请输入正确的手机号码
请输入正确的验证码
您今天的短信下发次数太多了,明天再试试吧!
我们会在第一时间安排职业规划师联系您!
您也可以联系我们的职业规划师咨询:
版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
沪公网安备 31011502005948号