1、现在有字符串:String s1=" SSSCCsdfgsdAX@KaTeX parse error: Expected 'EOF', got '#' at position 1: #̲67 "; String s…#67"; 完成以下操作:【默认的是对 s1操作】
(1):保存在字符数组中;
(2):取得第三个元素值;
(3):打印输出该字符串的每个字符;
(4):截取"sd"该子串;
(5):返回"67"的下标位置
(6):返回"gsd"的最后一个下标位置
(7):判断两个字符串是否相等【两种方法操作】
(8):将两个字符串进行连接为一个字符串,s2在前面,s1在后面
(9):去掉s1前后空格
(10):将s1只要是大写字符全部转为小写
(11):判断最后面的字串是不是“67”
(12):判断s2最前面的字串是不是“sss”
public class StringDemo {
public static void main(String[] args) {
String s1=" SSSCCsdfgsdAX@$#67 ";
String s2="sssdCCsdfGSDAX@$#67";
//(1):保存在字符数组中;
System.out.println("(1):保存在字符数组中;");
char[] s11 = s1.toCharArray();
for (int i = 0; i < s11.length; i++) {
System.out.print(s11[i]+" ");
}
System.out.println();
for (char i:s11){
System.out.print(i+" ");
}
System.out.println();
//(2):取得第三个元素值;
System.out.println("(2):取得第三个元素值;");
System.out.println(s1.charAt(2));
//(3):打印输出该字符串的每个字符;
System.out.println("(3):打印输出该字符串的每个字符;");
for (int i = 0; i < s1.length(); i++) {
System.out.print(s1.charAt(i)+" ");
}
System.out.println();
//(4):截取"sd"该子串;String s1=" SSSCCsdfgsdAX@$#67 "
System.out.println("(4):截取\"sd\"该子串;");
System.out.print(s1.substring(6,8)+" ");
System.out.println();
//(5):返回"67"的下标位置
System.out.println("(5):返回\"67\"的下标位置");
System.out.println(s1.indexOf("67"));
// (6):返回"gsd"的最后一个下标位置
System.out.println("(6):返回\"gsd\"的最后一个下标位置");
System.out.println(s1.lastIndexOf("gsd"));
//(7):判断两个字符串是否相等【两种方法操作】
String s3=" SSSCCsdfgsdAX@$#67 ";
System.out.println("(7):判断两个字符串是否相等【两种方法操作】");
System.out.println("方法一:直接 equals()");
System.out.println(s1.equals(s2));
System.out.println(s1.equals(s3));
System.out.println("方法二: 循环遍历对比法");
System.out.println(CompareString.compareString(s1,s2));
System.out.println(CompareString.compareString(s1,s3));
System.out.println("方法三:==");
System.out.println(s1==s2);
System.out.println(s1==s3);
System.out.println("方法四:compareTo");
if (s1.compareTo(s2)==0){
System.out.println(true);
}else {
System.out.println(false);
}if (s1.compareTo(s3)==0){
System.out.println(true);
}else {
System.out.println(false);
}
System.out.println("方法五:contains");
if (s1.contains(s2) && s2.contains(s1)){
System.out.println(true);
}else {
System.out.println(false);
}if (s1.contains(s3) && s3.contains(s1)){
System.out.println(true);
}else {
System.out.println(false);
}
//(8):将两个字符串进行连接为一个字符串,s2在前面,s1在后面
System.out.println("(8):将两个字符串进行连接为一个字符串,s2在前面,s1在后面");
String s4 = s1.concat(s2);
String s5 = s1+s2;
System.out.println(s4);
System.out.println(s5);
//(9):去掉s1前后空格
System.out.println("(9):去掉s1前后空格");
System.out.println(s1.trim());
//(10):将s1只要是大写字符全部转为小写
System.out.println("(10):将s1只要是大写字符全部转为小写");
System.out.println(s1.toLowerCase());
//(11):判断最后面的字串是不是“67”
System.out.println("(11):判断最后面的字串是不是“67”");
System.out.println(s1.endsWith("67"));
//(12):判断s2最前面的字串是不是“sss”
System.out.println("(12):判断s2最前面的字串是不是“sss”");
System.out.println(s2.startsWith("sss"));
}
}
public class CompareString {
public static boolean compareString(String a, String b) {
if (a.length() == b.length()) {
for (int i = 0; i < a.length(); i++) {
if (a.charAt(i)!=b.charAt(i)){
return false;
}else {
return true;
}
}
}
return false;
}
}
2、字符串解析,现有一字符串,“卡巴斯基#杀毒软件#免费版#俄罗斯#”,解析出每个元素。
public class AnalysisString {
public static void main(String[] args) {
String s1 = "卡巴斯基#杀毒软件#免费版#俄罗斯#";
String[] s2 = s1.split("#");
for (int i = 0; i < s2.length; i++) {
System.out.println("这是第"+(i+1)+"个 "+s2[i]);
}
}
}
3、“那车水马龙的人世间,那样地来 那样地去,太匆忙”,第二次出现"那"的位置。
public class Second {
public static void main(String[] args) {
String s1 = "那车水马龙的人世间,那样地来 那样地去,太匆忙";
int k =0;
for (int i = 0; i < s1.length(); i++) {
if (s1.charAt(i) == '那'){
k++;
if (k == 2){
System.out.println("第二个那出现的位置为:第"+(i+1)+"个汉字,在字符串中的下标为"+i);
break;
}
}
}
}
}
4、有一身份证号,判断此为男还是女,基于此方法,写一个算法,判断一个身份证号为男还是女。
public class JudgeID {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入您的身份证号码:");
String id = sc.nextLine();
int x = id.length()-2;
System.out.print("您的身份证号是:");
for (int i = 0; i < id.length(); i++) {
System.out.print(id.charAt(i));
}
if (id.charAt(x)%2==0){
System.out.println(" 欢迎您,女士!");
}else {
System.out.println(" 欢迎您,先生!");
}
}
}
5、如下字符串,01#张三#20-02#李四#30-03#王五#40。。。。。。,解析每个人分数多少。样式如下:
01 张三 20
02 李四 30
03 王五 40。并且计算总分。
public class Jiexi {
public static void main(String[] args) {
String s1 = "01#张三#20-02#李四#30-03#王五#40";
int sum=0;
String[] s11 = s1.split("-");
for (int i = 0; i < s11.length; i++) {
String a= String.valueOf(s11[i].charAt(s11[i].length()-1));
String b = String.valueOf(s11[i].charAt(s11[i].length()-2));
int x =Integer.parseInt(a);
int y =Integer.parseInt(b);
System.out.println(x);
System.out.println(y);
sum+=x+y*10;
System.out.println(s11[i].replace("#"," "));
}
System.out.println("他们的总成绩为:"+sum);
}
}
6、输入一个字符串,将字符串中的所有敏感词替换成**。
敏感词列表:色情,马勒戈壁,暴力,操,日。
public class Replace01 {
public static void main(String[] args) {
Scanner sr = new Scanner(System.in);
System.out.println("请输入你想要说的话:");
String speak = sr.nextLine();
if (speak.contains("色情")||speak.contains("马勒戈壁")||speak.contains("暴力") || speak.contains("操")|| speak.contains("日")){
String speak2 = speak.replace("色情","**");
String speak3 = speak2.replace("马勒戈壁","**");
String speak4 = speak3.replace("暴力","**");
String speak5 = speak4.replace("操","**");
String speak6 = speak5.replace("日","**");
System.out.println(speak6);
}
}
}
7、输入一个字符串,判断有多少个字母?多少个数字字符?多少个汉字字符?
public class Judge {
public static void main(String[] args) {
Scanner sr = new Scanner(System.in);
int x = 0;
int y = 0;
int z = 0;
System.out.println("请输入一个字符串:");
String talk = sr.nextLine();
System.out.println("您输入的字符串为:");
for (int i = 0; i < talk.length(); i++) {
System.out.print(talk.charAt(i));
}
System.out.println();
for (int i = 0; i < talk.length(); i++) {
if (talk.charAt(i)>='A'&& talk.charAt(i)<='Z'||talk.charAt(i)>='a'&& talk.charAt(i)<='z'){
x++;
}else if (talk.charAt(i)>='0'&& talk.charAt(i)<='9'){
y++;
}else {
z++;
}
}
System.out.println("其中有"+x+"个字母,有"+y+"个数字字符,有"+z+"个汉字字符");
}
}
8、歌词:
朋友啊朋友,你可曾想起了我?
如果你正享受幸福,请你忘记我。
朋友啊朋友,你可曾记起了我?
如果你正承受不幸,请你告诉我。
朋友啊朋友。。。
请问“朋友”出现了多少次。
public class FindFriend {
public static int str1(String str ,String sub){
int s = 0;
for(int i = str.indexOf(sub,0);i!=-1;i=str.indexOf(sub,i+sub.length())){
s++;
}
return s;
}
public static void main(String[] args) {
String friendsong = "朋友啊朋友,你可曾想起了我?如果你正享受幸福,请你忘记我。朋友啊朋友,你可曾记起了我?如果你正承受不幸,请你告诉我。朋友啊朋友。。。";
String s1 = friendsong.replace("朋友"," ");
int conts = friendsong.length()-s1.length();
System.out.println("其中有"+conts+"个朋友");
System.out.println("方法二;");
System.out.println("其中有"+str1(friendsong,"朋友")+"个朋友");
}
}
9、将字符串“我爱中华人民共和国”,变成一个倒叙的字符串。
public class Reversal {
public static void main(String[] args) {
String s1 = "!国和共民人华中爱我";
char[] s11 = s1.toCharArray();
for (int i = 0; i < s11.length/2; i++) {
char temp = s11[i];
s11[i] = s11[s11.length-i-1];
s11[s11.length-i-1] = temp;
}
for(char x :s11){
System.out.print(x);
}
System.out.println();
System.out.println("方法二:");
System.out.println(new StringBuffer(s1).reverse().toString());
}
}
10、学生类:学号,姓名,成绩。
重写equals方法:如果学号相同,返回true。
重写toString方法:返回学生的信息,格式“学号-1001;姓名-张三;成绩-90”。
public class Student {
private long id;
private String name;
private double score;
public Student() {
}
public Student(long id, String name, double score) {
this.id = id;
this.name = name;
this.score = score;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getScore() {
return score;
}
public void setScore(double score) {
this.score = score;
}
@Override
public String toString() {
return "Student{" +
"id=" + id +
", name='" + name + '\'' +
", score=" + score +
'}';
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Student)) return false;
Student student = (Student) o;
return id == student.id;
}
@Override
public int hashCode() {
return Objects.hash(id);
}
}
public class Test {
public static void main(String[] args) {
Student s1 = new Student(11111,"张三",98);
Student s2 = new Student(22222,"李四",95);
Student s3 = new Student(11111,"王五",88);
Student s4 = new Student(33333,"汪峰",90);
Student s5 = new Student(55555,"哈哈",95);
System.out.println(s1.equals(s2));
System.out.println(s1.equals(s3));
System.out.println(s1.toString());
System.out.println(s2.toString());
System.out.println(s3.toString());
System.out.println(s4.toString());
System.out.println(s5.toString());
}
}