1、将字符串反转
String 是一种不可变字符串,一对字符串进行改变操作,则创建一个全新的对象,然后把引用指向这个新的对象。
package enumeration;
public class HomeWork {
public static void main(String[] args) {
String str = "abcdefg";
str = Me.reverse(str,0,3);
System.out.println(str);
}
}
class Me {
public static String reverse(String str, int start, int end) {
//对输入的参数做一个验证
//写出正确的情况,然后取反。
if (!(str != null && start > 0 && end <= str.length() && start < end)){
System.out.println("输入有误");
throw new RuntimeException("参数不正确");
}
char[] chars = str.toCharArray();
//改变成数组
char temp = ' ';
for (int i = start ,j = end; i < j; i++, j--) {
temp = chars[i];
chars[i] = chars[j ];
chars[j] = temp;
}
//以上是对字符串数组进行修改。
String s = new String(chars);
//String 是一种不可变字符串,一对字符串进行改变操作,则创建一个全新的对象,然后把引用指向这个新的对象。
//将chars字符串数组转换成String
// return new String(chars);
return s;
}
}
2、输入信息
package enumeration;
import java.util.Scanner;
public class HomeWork01 {
public static void main(String[] args) {
//
String name = "jack";
String password = "123456";
String mail = "1152@qq.com";
try {
apply(name,password,mail);
System.out.println("注册成功");
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
public static void apply(String name ,String password ,String mail){
if (!(name.length()==2 || name.length() == 3 || name.length() ==4) ){
throw new RuntimeException("用户名的长度为2或3或4");
}
if (!(password.length() == 6 && isDegital(password) )){
throw new RuntimeException("输入格式有误");
}
int i = mail.indexOf(".");
int j = mail.indexOf("@");
if (!(i > 0 && j < i)){
throw new RuntimeException("输入格式有误");
}
System.out.println("信息输入正确,如下:");
System.out.println("name = " +name +
"password = " + password +
"mail = " + mail);
}
public static boolean isDegital(String password){
//将password转换成字符串数组
char[] chars = password.toCharArray();
for (int i = 0; i < chars.length ; i++) {
//用来判断是否是一个数字,解释:看当前的数组的ASCII是否小于0和9的ascii码,要是
//超出范围表示不是一个数字。
if (chars[i] < '0' && chars[i] > '9' ){
return false;
}
}
return true;
}
}
3、分割字符串,改变输出的格式
package enumeration;
public class M {
public static void main(String[] args) {
String name = "wang xiao sai";
printName(name);
}
public static void printName(String str){
if (str == null){
System.out.println("str 不能为null");
return;
}
String[] names = str.split(" ");//进行分割,分割之后保存为字符数组。
if(names.length != 3){
System.out.println("结束啦");
return;
}
String format = String.format("%s %s .%c",names[2],names[0],names[1].toUpperCase().charAt(0));//最后一句话进行解读:将最后的名字先转成大写字母,然后取第一个字符。
System.out.println(format);
}
}
4、查找多少个大小写字符数字
package enumeration;
public class G {
public static void main(String[] args) {
String str = "uucahsuilcha123";
find(str);
}
public static void find(String str){
int smallCount = 0;
int maxCount = 0;
int numCount = 0;
if (str == null){
throw new RuntimeException("字符串不能为null");
}
char[] chars = str.toCharArray();
for (int i = 0; i < chars.length; i++) {
if (chars[i] > '0' && chars[i] < '9'){
numCount++;
}
if (chars[i] > 'a' && chars[i] < 'z'){
smallCount++;
}
if (chars[i] > 'A' && chars[i] < 'Z'){
maxCount++;
}
}
System.out.println("numCount" + numCount + "\tsmallCount" + smallCount + "\tmaxCount" + maxCount);
}
}
FTTFFT