Stringa="Hello World";Stringb="My name is ";Stringx=b;// 存储到了相同地址Stringc=b+"yxc";// String可以通过加号拼接Stringd="My age is "+18;// int会被隐式转化成字符串"18"Stringstr=String.format("My age is %d",18);// 格式化字符串,类似于C++中的sprintfStringmoney_str="123.45";doublemoney=Double.parseDouble(money_str);// String转double
只读变量,不能修改,例如:
12
Stringa="Hello ";a+="World";// 会构造一个新的字符串
常用API
length():返回字符串长度
split(String regex):分割字符串
equals():判断两字符串是否相等,注意不能使用==
substring(int beginIndex, int endIndex):返回[beinInext, endIndex)中的子串