D12
来源:互联网 发布:有关于网络的卡通形象 编辑:程序博客网 时间:2024/05/18 17:42
1,String s = new String(“hello”)和String s = “hello”;的区别?
有。前者会创建2个对象,后者创建1个对象。
==:比较引用类型比较的是地址值是否相同
* equals:比较引用类型默认也是比较地址值是否相同,而String类重写了equals()方法,比较的是内容是否相同。
*/
2,写出结果!
public static void main(String[] args) {String s1 = new String("hello");String s2 = new String("hello");System.out.println(s1 == s2);// falseSystem.out.println(s1.equals(s2));// trueString s3 = new String("hello");String s4 = "hello";System.out.println(s3 == s4);// falseSystem.out.println(s3.equals(s4));// trueString s5 = "hello";String s6 = "hello";System.out.println(s5 == s6);// trueSystem.out.println(s5.equals(s6));// true}
3,看程序写结果
* 字符串如果是变量相加,先开空间,在拼接。
* 字符串如果是常量相加,是先加,然后在常量池找,如果有就直接返回,否则,就创建。
public static void main(String[] args) {String s1 = "hello";String s2 = "world";String s3 = "helloworld";System.out.println(s3 == s1 + s2);// falseSystem.out.println(s3.equals((s1 + s2)));// trueSystem.out.println(s3 == "hello" + "world");// true 应该是trueSystem.out.println(s3.equals("hello" + "world"));// true// 通过反编译看源码,我们知道这里已经做好了处理。// System.out.println(s3 == "helloworld");// System.out.println(s3.equals("helloworld"));}
* String s = ""; 内容为空
* String s = null; 对象为空
5,遍历获取字符串中的每一个字符
for (int x = 0; x < s.length(); x++) {
// char ch = s.charAt(x);
// System.out.println(ch);
// 仅仅是输出,我就直接输出了
System.out.println(s.charAt(x));
}
6,统计一个字符串中大写字母字符,小写字母字符,数字字符出现的次数。(不考虑其他字符)
public static void main(String[] args) {//定义一个字符串String s = "Hello123World";//定义三个统计变量int bigCount = 0;int smallCount = 0;int numberCount = 0;//遍历字符串,得到每一个字符。for(int x=0; x<s.length(); x++){char ch = s.charAt(x);//判断该字符到底是属于那种类型的if(ch>='a' && ch<='z'){smallCount++;}else if(ch>='A' && ch<='Z'){bigCount++;}else if(ch>='0' && ch<='9'){numberCount++;}}//输出结果。System.out.println("大写字母"+bigCount+"个");System.out.println("小写字母"+smallCount+"个");System.out.println("数字"+numberCount+"个");}
7,把一个字符串的首字母转成大写,其余为小写。(只考虑英文大小写字母字符)
public static void main(String[] args) {// 定义一个字符串String s = "helloWORLD";// 先获取第一个字符String s1 = s.substring(0, 1);// 获取除了第一个字符以外的字符String s2 = s.substring(1);// 把A转成大写String s3 = s1.toUpperCase();// 把B转成小写String s4 = s2.toLowerCase();// C拼接DString s5 = s3.concat(s4);System.out.println(s5);// 优化后的代码// 链式编程String result = s.substring(0, 1).toUpperCase().concat(s.substring(1).toLowerCase());System.out.println(result);}
8,把数组中的数据按照指定个格式拼接成一个字符串
public static void main(String[] args) {// 前提是数组已经存在int[] arr = { 1, 2, 3 };// 定义一个字符串对象,只不过内容为空String s = "";// 先把字符串拼接一个"["s += "[";// 遍历int数组,得到每一个元素for (int x = 0; x < arr.length; x++) {// 先判断该元素是否为最后一个if (x == arr.length - 1) {// 就直接拼接元素和"]"s += arr[x];s += "]";} else {// 就拼接元素和逗号以及空格s += arr[x];s += ", ";}}// 输出拼接后的字符串System.out.println("最终的字符串是:" + s);}
9,字符串反转
public static String myReverse(String s) {// 定义一个新字符串String result = "";// 把字符串转成字符数组char[] chs = s.toCharArray();// 倒着遍历字符串,得到每一个字符for (int x = chs.length - 1; x >= 0; x--) {// 用新字符串把每一个字符拼接起来result += chs[x];}return result;}
10,统计大串中小串出现的次数
public static int getCount(String maxString, String minString) {// 定义一个统计变量,初始化值是0int count = 0;// 先在大串中查找一次小串第一次出现的位置int index = maxString.indexOf(minString);// 索引不是-1,说明存在,统计变量++while (index != -1) {count++;// 把刚才的索引+小串的长度作为开始位置截取上一次的大串,返回一个新的字符串,并把该字符串的值重新赋值给大串int startIndex = index + minString.length();maxString = maxString.substring(startIndex);// 继续查index = maxString.indexOf(minString);}return count;}或者
public static int getCount(String maxString, String minString) {int index;while((index=maxString.indexOf(minString))!=-1){count++;maxString = maxString.substring(index + minString.length());}return count;}
0 0
- d12
- D12
- D12
- D12函数
- USB D12开发板
- D12一些注意事项
- D12 Hive基础及Flume
- D12-Nginx-利用Referer防盗链
- 《Python基础教程》学习笔记之[D12]充电时刻(一)
- PC与单片机USB(D12)数据通讯过程描述
- 【转】【学】USB接口芯片D12开发的经验
- Your D12 chip's ID is: 0x1012. ID is correct!
- Excel地址转换 Excel是最常用的办公软件。每个单元格都有唯一的地址表示。 比如:第12行第4列表示为:“D12”,第5行第255列表示为“IU5”。
- ubantu 常见问题
- 第一回:优秀的产品经理需要具备哪些能力——老吴说产品
- 一个简单的遍历选定目录的PowerShell脚本
- 一位大学生的坎坷辛苦求学路
- android TextView加下划线的方法
- D12
- ios开发笔记之TextView
- 5 Examples of Different Types of CAPTCHAs
- Google 发布 Bottom Navigation design guidelines
- c++实现split函数
- 2016年阿里实习生面试经历及问题总结(2016-03-15)
- Intel Edison 换完Debian 后开机自动设置 Wifi
- SQl语句中使用占位符的优点
- web测试总结3