黑马程序员技术交流社区
标题:
今天学了String
[打印本页]
作者:
夏至落叶
时间:
2014-11-19 22:19
标题:
今天学了String
今天学习了String,对于第一次接触API的我i感觉这一天学到了很多方法,但是最后感觉白天学的都忘得差不多了,晚自习就开始敲代码,把讲到的String方法都敲一遍,顺便总结。
把总结打包了,里面有我得总结和总结敲的小代码。还有老师上课说的例题.
学习java就是要敲代码。特别是API,不会就敲个几遍。都是很基础的,高手勿喷哈
//String
//构造函数直接看API,就不总结了。
/* 判断功能:
* boolean equals(Object obj):判断字符串的内容是否相同,区分大小写。
* boolean equalsIgnoreCase(String str):判断字符串的内容是否相同,不区分大小写。
* boolean contains(String str):判断字符串对象是否包含给定的字符串。
* boolean startsWith(String str):判断字符串对象是否以给定的字符串开始。
* boolean endsWith(String str):判断字符串对象是否以给定的字符串结束。
* boolean isEmpty():判断字符串对象是否为空。数据是否为空。
*/
public class StringDemo {
public static void main(String[] args) {
String s = "helloword";
String s2 = "helloword";
String s3 = "Helloword";
String s4 = "ow";
String s5 = "";
System.out.println(s.equals(s2));
System.out.println(s.equalsIgnoreCase(s3));
System.out.println(s.contains(s4));
System.out.println(s.startsWith("h"));
System.out.println(s.endsWith("rd"));
System.out.println(s5.isEmpty());
}
/* * 字符串的获取功能:
* int length():获取字符串的长度
* char charAt(int index):返回字符串中给定索引处的字符
* int indexOf(int ch):返回指定字符在此字符串中第一次出现的索引
* int indexOf(String str):返回指定字符串在此字符串中第一次出现的索引
* int indexOf(int ch,int fromIndex):返回在此字符串中第一次出现指定字符的索引,从指定的索引开始搜索。
* int indexOf(String str,int fromIndex):返回在此字符串中第一次出现指定字符串的索引,从指定的索引开始搜索。
* String substring(int start):截取字符串。返回从指定位置开始截取后的字符串。
* String substring(int start,int end)截取字符串。返回从指定位置开始到指定位置结束截取后的字符串。
*/
public class StringDemo2 {
public static void main(String[] args) {
String str = "helloword";
int s = str.length();
char s1 = str.charAt(3);
int s2 = str.indexOf('w');
int s3 = str.indexOf("low");
int s4 = str.indexOf('o',3);
int s5 = str.indexOf("or", 1);
String s6 = str.substring(3);
String s7 = str.substring(1, 5);
System.out.println(s);
System.out.println(s1);
System.out.println(s2);
System.out.println(s3);
System.out.println(s4);
System.out.println(s5);
System.out.println(s6);
System.out.println(s7);
}
//字符串的转换功能:
/*
* byte[] getBytes():把字符串转换成字节数组。
* char[] toCharArray():把字符串转换成字符数组。
* static String copyValueOf(char[] chs):把字符数组转换成字符串。
* static String valueOf(char[] chs):把字符数组转换成字符串。
* static String valueOf(int i)基本类型:把int(基本类型)转换成字符串。
* String toLowerCase():把字符串变成小写
* String toUpperCase():把字符串变成大写
* String concat(String str):拼接字符串。
*/
public class StringDemo3 {
public static void main(String[] args) {
String str = "abcdefg";
byte [] s = str.getBytes();
for (int i = 0; i < s.length; i++) {
System.out.print(s[i]+" ");
}
System.out.println("");
char[] s1 = str.toCharArray();
for (int i = 0; i < s.length; i++) {
System.out.print(s1[i]);
}
System.out.println("");
char [] s3 = { 'a', 'b', 'c', '中', '国'};
//System.out.println(s);
String s4 = String.copyValueOf(s3);
System.out.println(s4);
String s5 = String.valueOf(158);
String s6 = str.toUpperCase();
String s7 = str.toLowerCase();
String s8 = str.concat(s5);
System.out.println(s6);
System.out.println(s7);
System.out.println(s8);
}
}
/*
* 替换功能
* String replace(char oldChar,char newChar):用新的字符去替换指定的旧字符
* String replace(String oldString,String newString):用新的字符串去替换指定的旧字符串
*
* 切割功能
* String[] split(String regex)
*
* 去除字符串两端空格
* String trim()
*
* 按字典顺序比较两个字符串
* int compareTo(String str)
*/
public class StringDemo4 {
public static void main(String[] args) {
String str = "hello";
String s7 = "abllo";
String s6 = " hello java ";
String s9 = "abcdefghadfjas";
String s = str.replace('l', 'p');
String s1 = str.replaceAll("lo", "pk");
String[] s4 = s9.split("f");
String s5 = s6.trim();
int x = str.compareTo(s7);
System.out.println(s);
System.out.println(s1);
for (int i = 0; i < s4.length; i++) {
System.out.println(s4[i]);
}
System.out.println(s5);
System.out.println(x);
}
}
课堂老师说的练习题:
import java.util.Scanner;
/*
* 输入字母首字母大写后面字母小写的功能
*
*
*/
public class StringTest {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("输入字母");
String s = sc.next();
//获取字符串第一个字母并大写
String s1 = s.substring(0,1).toUpperCase();
//获取字符串除第一个字母并小写
String s2 = s.substring(1).toLowerCase();
//连接字符串
String s3 = s1.concat(s2);
System.out.println(s3);
System.out.println("********************");
//链式写法
String s4 = s.substring(0,1).toUpperCase().concat(s.substring(1).toLowerCase());
System.out.println(s4);
}
}
import java.util.Scanner;
/*
* 输入字符串
* 分别算出字符串中大写字母,小写字母,数字的个数
*
*/
public class Test {
public static void main(String[] args) {
//定义3个变量,分别是大写字母个数,小写字母个数,数字个数
int bigcount = 0;
int smallcount = 0;
int number = 0;
//键盘录入功能
Scanner sc = new Scanner(System.in);
System.out.println("输入字符串");
String s = sc.nextLine();
for (int i = 0; i <s.length(); i++) {
//把字符串转化为字符数组
//进行遍历
char ch = s.charAt(i);
//判断数组中各个数字类型并累加
if (ch>=65&&ch<=90) {
bigcount++;
} else if(ch>=97&&ch<=122){
smallcount++;
}
else if(ch>=48&&ch<=57){
number++;
}
}
System.out.println("大写字母个数:"+bigcount);
System.out.println("小写字母个数:"+smallcount);
System.out.println("数字个数:"+number);
}
}
/*
* /*
* 获取一个字符串中指定子串出的次数。比如说“hanbasdnbafllgnbahjnbakqqqqlnbaxi” 在这个字符串中,多有个nba?
*
*
*/
public class Test2 {
public static void main(String[] args) {
String s = "hanbasdnbafllgnbahjnbakqqqqlnbaxi";
String x = "nba";
int count = fun(s,x);
System.out.println(count);
}
public static int fun(String maxstring,String minstring){
int count = 0;
//找到小字符串在大字符串第一次出现的位置
int index = maxstring.indexOf(minstring);
//当字符串中没有指定子串返回-1,下面则直接退出
while (index!=-1){
count++;
//把小字符串前的字符删掉重新组成新字符
maxstring = maxstring.substring(index+minstring.length());
//再找出小字符串的位置
index = maxstring.indexOf(minstring);
}
return count;
}
}
复制代码
作者:
天天小志
时间:
2014-11-19 22:27
先来占个位子,200多行,真猛!
作者:
这是我的地盘
时间:
2014-11-19 22:28
做的很好,程序的学习过程就是从敲代码开始的,多敲多练,总会有收获的,赞一个!
作者:
wxnzb502
时间:
2014-11-19 22:32
记录的不错
作者:
我就是姚舜禹
时间:
2014-11-19 22:33
做的很好,程序的学习过程就是从敲代码开始的,多敲多练,总会有收获的,赞一个!
作者:
NCry
时间:
2014-11-19 22:35
200多行。 这么多行就应该赞啊!!!!
作者:
随风sky
时间:
2014-11-19 22:41
真给力!加油,哥们!
作者:
迷失的小Z
时间:
2014-11-19 22:44
每一天都敲200行,你离牛人也不远了……
作者:
zbt
时间:
2014-11-19 22:48
没抢到沙发
作者:
夏至落叶
时间:
2014-11-19 22:49
中间不少是注释,没有200行。玩java就是敲代码。就这样上面的代码我还没记住。明天还得继续敲
作者:
a986875894
时间:
2014-11-19 22:53
本帖最后由 a986875894 于 2015-12-10 21:08 编辑
..................
作者:
王小忠
时间:
2014-11-20 00:40
厉害~~200多行,以后多多交流哈
作者:
梦浮冀北
时间:
2014-11-20 09:18
不错,继续加油
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2