当前位置:网站首页>String class
String class
2022-07-29 07:40:00 【lilshork】
Catalog
What is? String class
String Class represents string ,Java All string literals in the program ( Such as "abc" ) Are implemented as examples of this class . Strings are constants ; Their values cannot be changed after creation .
frequently-used String Method
①str.length() Gets the length of the string
②str.charAt() Get a single character
③str.substring(1,2) Intercepting string
④str.indexOf(" One ") Get the index position of the specified character
example
Design a login program , Enter... Through the keyboard , The login name is “ Zhang San ”, The password for 123456, There are a total of three login opportunities .
public class Login {
public static void main(String[] args) {
String username = " Zhang San ";
String password = "123456";
Scanner scanner = new Scanner(System.in);
int count = 0;
for (int i = 0; i < 3; i++) {
System.out.println(" Please enter a user name :");
String zhanghao = scanner.next();
System.out.println(" Please input a password :");
String mima = scanner.next();
if(zhanghao.equals(username)&&mima.equals(password)){
System.out.println(" Login successful !");
break;
}else{
System.out.println(" Login failed ! Please re-enter ! You still have "+(2-i)+" Second chance !");
count++;
}
}
if (count==3){
System.out.println(" Too many failures , Please try again later !");
}
}
}
analysis : First of all, we can see that we need to use keyboard input , And there are a total of three login opportunities , If the user name and password are correct, there is no need to , So you can use a loop , When the number of times is known, you can think of using for loop , If correct, then break Out of the loop , meanwhile , You can use String Class equals Compare keyboard input with “ Zhang San ” and 123456 equally .
Enter a string on the keyboard , Traverse and output the string
public class StringTest {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println(" Please enter the string :");
String a = scanner.next();
for (int i = 0; i < a.length(); i++) {
System.out.print(a.charAt(i));
}
}
}analysis : To traverse a string , We can think of taking a single character of a string , Export them , So you can use charAt(), Use the loop to output them one by one , The number of cycles is the length of the string , So you can think of it length.
Receive a phone number from the keyboard as a string , Block the middle four digits of the mobile phone number
public class StringTest02 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println(" Please enter your mobile number ");
String number = scanner.next();
String before = number.substring(0,3);
String after = number.substring(7);
System.out.println(before+"****"+after);
}
}analysis : Because it is necessary to block the middle four digits of the mobile phone number , So we can think of intercepting strings , Get the top three mobile phone numbers , The last four got , Intermediate use * Instead of just , So we need substring().
边栏推荐
- Leetcode buckle classic problem -- 4. Find the median of two positively ordered arrays
- Amaze UI 图标查询
- PAT甲级 1146 拓扑顺序
- Use custom annotations to verify the size of the list
- The difference between static library and dynamic library of program
- Pat class a 1150 traveling salesman problem
- Blue Bridge Cup group a selection XOR
- Android面试题 | 怎么写一个又好又快的日志库?
- 工业互联网行至深水区,落地的路要怎么走?
- Matlab simulation of LDPC minimum sum decoding based on high-order six ring free
猜你喜欢

关于大龄读博的几点回答?

Segger's hardware anomaly analysis

Amazon cloud assistant applet is coming!

The new generation of public chain attacks the "Impossible Triangle"

207. Curriculum

IonIcons图标大全

【深度学习】数据准备-pytorch自定义图像分割类数据集加载

LANDSCAPE

Cs61abc sharing session (VI) detailed explanation of program input and output - standard input and output, file, device, EOF, command line parameters

10 practical uses of NFT
随机推荐
3-global exception handling
Amazon cloud assistant applet is coming!
【暑期每日一题】洛谷 P1601 A+B Problem(高精)
@RequestMapping 用法详解
EF core reading text type is slow_ EF core is slow to read large string fields
[WPF] realize language switching through dynamic / static resources
MySQL 45 | 08 is the transaction isolated or not?
Multi thread shopping
【无标题】格式保存
基于高阶无六环的LDPC最小和译码matlab仿真
信用卡购物积分
LANDSCAPE
写点dp
UPC little C's King Canyon
Use custom annotations to verify the size of the list
性能更佳、使用更简单的懒加载IntersectionObserverEntry(观察者)
Monitor the bottom button of page scrolling position positioning (including the solution that page initialization positioning does not take effect on mouse sliding)
What are the answers about older bloggers?
Strongly connected component
cs61abc分享会(六)程序的输入输出详解 - 标准输入输出,文件,设备,EOF,命令行参数