Java——字符串类实验
来源:互联网 发布:access数据库 excel 编辑:程序博客网 时间:2024/06/11 15:53
【任务一】:
实验要求:
能够根据业务的要求设计和定义类;能够根据业务的要求提取出和业务相关的属性和方法;能够使用定义的属性和
方法完成相应的业务要求;能够使用对象和对象之间的关系完成业务要求;能够将实际的问题描述转换成 Java程序。
实验任务:编写一个 Java 程序,将用户输入的句子当中每一个单词的第一个字母大写, 而单词中的其余字母小写。
要求:
1. 创建类 CaseConverter。
2. CaseConverter 具有方法 String convert(String value),用于返回转换大小写后的结果。
3. 写一个测试类来测试该程序,首先要求用户输入一个句子, 然后提取每一个单词,并将单词的首字母转换成大写,
其余字符小写。 最后输出新字符串。
提示:
1. 需要使用 Scanner的 nextLine()方法来获取一个句子。
2. 使用 StringBuffer类来创建替换过大小写的新字符串。
注意:
1. 单词的分割符除了空格之外,还可能有 Tab或者( ,;.等标点符号)。
2. 注意判断首字符是不是字母。
3. 单词中的其他字母可能为大写。
例如:当用户输入”Thisis a samPLe sentencE to demostrATE the TasK 2.”
实验步骤:
新建两个类——CaseConverter(实现功能)、TestCaseConverter(测试功能)
实验代码:
< CaseConverter.java>
public class CaseConverter {public static String[] words = new String[20];public static String convert(String value) {StringBuffer upAfter = new StringBuffer(value.length());char aCharacter;//将tab , ; .等标点符号替换成空格value = value.replaceAll("[\\pP‘’“”]", " ");words = value.split(" ");for (int i = 0;i < words.length; i++){for(int j = 0;j < words[i].length(); j++) {aCharacter = words[i].charAt(j);if (j==0) {//判断是不是字母if(Character.isLetter(aCharacter)) {upAfter.append(Character.toUpperCase(aCharacter));}else {upAfter.append(words[i].charAt(j));}}else {//如果该字符是字母切是大写字母,则转小写,否则直接连接if(Character.isLetter(aCharacter) && Character.isUpperCase(aCharacter)) { upAfter.append(Character.toLowerCase(aCharacter)); }else {upAfter.append(words[i].charAt(j));}}}upAfter.append(" ");}return new String(upAfter);}}
< TestCaseConverter.java>
import java.util.Scanner;public class TestStringEditor {public static void main(String[] args) {// TODO Auto-generated method stubScanner scan = new Scanner(System.in);System.out.print("Input your string:");String inputString = scan.nextLine();scan.close();System.out.println("Your string after change:" + CaseConverter.convert(inputString));}}
实验笔记:
1. 提取一句英语句子(字符串)中的单词,使用了String split()方法。
在java.lang包中有stringObj.split([separator,[limit]]) 方法,将一个字符串分割为子字符串,然后将结果作为字符串数组返回。
stringObj 必选项。要被分解的 String 对象或文字。该对象不会被 split 方法修改。
separator 可选项。字符串或 正则表达式 对象,它标识了分隔字符串时使用的是一个还是多个字符。如果忽略该选项,返回包含整
个字符串的单一元素数组。
limit 可选项。该值用来限制返回数组中的元素个数
举个栗子:
String.split("\\.")用“.”作为分隔
String.split("\\|")用“|”作为分隔
String.split(" ", 2); 用“ ”作为分割,并且返回2个数组
由于在这个实验中,单词的分割符除了空格之外,还可能有 Tab或者( ,;.等标点符号),
所以先把所有的标点符号全部替换成空格,然后再通过split(" ")提取出单词。
在 JDK 6 以下的版本中可以这样:(用空格*正则替换字符串中的全椒、半角标点符号)
value = value.replaceAll("[\\pP‘’“”]", " ");
2. 在遍历(二维)字符串数组的过程中,使用了length(),charAt()方法。
获取长度 *.length();//这与数组中的获取长度不同,*.length;
charAt()的范围是0到length() - 1。
3. 判断是否为字母,以及字母转换成大写或者小写。
Character.isLetter(aCharacter) 判断一个字符是否为字母;
Character.isUpperCase(aCharacter) 判断一个字符是否为大写字母;
Character.toLowerCase(aCharacter) 将一个字符转换成小写。
4. 连接方法append()的使用。
StringBuffer append( Object ob) 将ob连接到接受者字符串的末尾。
更多关于字符串类的使用,参考文献:http://www.runoob.com/java/java-character.html
5. 返回的时候为什么要返回new String(),而不是直接返回upAfter这个StringBuffer类的字符串?
粗略的说就是:new String()字符串池中,生成了一个新的String对象。有对象,才有返回对象。
as both of your function has return type String
, creating a new String()
is just a overhead. it like wrapping
a string to again to a string and create a new string in pool which in real has no advantage.
But one major difference in primitive type and String object is that class String always create new string.
String str = "my string";
if "my string" already exists in String pool. then it will use the same string instead of creating new.
This is the reason why when,
String str1= "my string";String str2 ="my string";str1==str2? --> will return true
The result of above will be true, because same String object from pool is used.
but when you do,
String str = new String("new string");
Always, a new String object is created, irrespective of a same one already exists in pool or not.
so comparing:
String str1 = new String("new string");String str2 = new String("new string");str1==str2 --> will return false
6. 测试类中,调用的Scanner()方法,Scan.close()的意义,以及nextLine()。
Scanner in = new Scanner(System.in); // System.in is an InputStream
int number = in.nextInt(); //Returns the next token asan int. 接收一个整数
String string = in.next(); //Finds and returns the next complete token from this scanner and returns it as a string; a
token is usually ended by whitespace such as a blank or line break.接收一个以空格或句号结束的字符串。
String nextLine(); //Returns the rest of the current line, excluding any line separator at the end.接收一整行的字符串。
出处:http://www.cs.utexas.edu/users/ndale/Scanner.html
8. 调用其他类的方法,此方法一定要是static的吗?
这真是个愚蠢的问题!
static修饰的方法属于类不属于实例(对象),所以其他类必须通过类名才能使用这个静态方法。
否则,如果一个方法不是static,它属于对象,那么其他类则需要通过实例化一个此类的对象才能调用这个对象的方法。
参考文献:http://www.wikihow.com/Call-a-Method-in-Java
将方法比作财产,(默认是public)
公共财产——一个组的所有人都可以用;受保护财产——个人和子女可用;私有财产——只有本人可用。
public method——一个包的所有类都可以使用(其他包可import),protected method——此类和子类可使用,private method——只有此类可使用。
void 方法不需要返回值,void也可以用int ,String, char等替换,表示返回值的类型。
利用带返回值的方法直接给变量赋值。
When calling a method that returns something, you can use what it returns. For example,
if a someMethod()
returns an integer, then you can set an integer to what it returns with "int a = someMethod();
"
结束!!
someMethod()
returns an integer, then you can set an integer to what it returns with "int a = someMethod();
" - Java——字符串类实验
- C++实验五—字符串
- 汇编实验——查找匹配字符串
- C语言实验——输出字符串
- C++实验6——字符串操作
- C语言实验——输出字符串
- C语言实验——输出字符串
- C语言实验——输出字符串
- C++实验6—字符串操作
- Java作业——权限综合实验 + 数组、字符串高级应用
- 回顾大学--大一JAVA实验--实验四 字符与字符串
- java实验2.(1)字符串简单操作
- C语言实验——拍皮球(java语言实验)
- 字符串插入块链实现——数据结构上机实验
- 王爽《汇编语言》实验10.1——显示字符串
- 汇编实验——用表格形式显示字符串
- C语言实验——输出字符串(sdut oj)
- oj1974: C语言实验——输出字符串
- 关于跨域问题
- jQuery 遍历
- Apache Kafka:下一代分布式消息系统
- 不需要面对面签订的电子合同有效吗
- 如何远程访问公司的内部办公系统?
- Java——字符串类实验
- Linux内核配置和make menuconfig
- 单播、多播、广播、组播、泛播概念区分
- 廖雪峰python中filter生成素数
- 正则表达式(RegExp)
- JDK9新API:Thread.onSpinWait()
- CVE-2016-5195
- ubuntu下软件安装
- Recyclerview-的简单用法