当前位置:网站首页>Traversal and splicing of strings
Traversal and splicing of strings
2022-07-24 18:36:00 【The code family】
Traversal of string :
demand : Enter a string on the keyboard , Use the program to traverse the string in the console
Ideas :
① Enter a string on the keyboard , use Scanner Realization
② Traversal string , First, you need to be able to get every character in the string
●public char charAt(int index): Returns the char value , The index of the string is also from 0 At the beginning
③ Traversal string , Secondly, we should be able to get the length of the string
●public int length(): Returns the length of this string
● Length of array : Array name .length
● Length of string : String object .length()
④ The general format for traversing strings
for(int i=0; i<s.length(); i++) {
s. charAt(i); // Is the character value at the specified index
}
import java.util.Scanner;
public class _02_String {
public static void main(String[] args) {
// Enter a string on the keyboard , use Scanner Realization
Scanner sc = new Scanner(System.in);
System.out.println(" Please enter a string :");
String line = sc.nextLine();
// Traversal string , First, you need to be able to get every character in the string
for (int i = 0; i < line.length(); i++) {
System.out.println(line.charAt(i));
}
}
}
String splicing
demand : Define a method , hold int The data in the array is spliced into a string according to the specified format , Call the method , And output the results on the console . for example , The array is int[] arr= {1,2,3};, After executing the method, the output is : [1,2, 3]
Ideas :
① Define a int An array of types , Complete the initialization of array elements with static initialization
② Define a method , Used for holding int The data in the array is spliced into a string according to the specified format . return type String, parameter list int[] arr
③ Traversing arrays in methods , Splice as required
④ Calling method , Receive the result with a variable
⑤ Output results
public class _04_String {
public static void main(String[] args) {
// Define a int An array of types , Complete the initialization of array elements with static initialization
int[] arr = {
1, 2, 3};
// Calling method , Receive the result with a variable
String s = arrayToString(arr);
// Output results
System.out.println("s:" + s);
}
// Define a method , Used for holding int The data in the array is spliced into a string according to the specified format
/* Two clear : return type : String Parameters : int[] arr */
public static String arrayToString(int[] arr) {
String s = "";
// Traversing arrays in methods , Splice as required
s = s + "[";
for (int i = 0; i < arr.length; i++) {
if (i == arr.length - 1) {
s = s + arr[i];
} else {
s = s + arr[i] + ',';
}
}
s = s + "]";
return s;
}
}
边栏推荐
- Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag
- Ionic4 learning notes 11 - popular goods display of an East Project
- Template inheritance and import
- Some buckles
- Pytoch's Journey 2: gradient descent
- Pytorch的旅程一:线性模型
- 根证书的有效期与服务器SSL证书一样长吗?
- Ionic4 learning notes 10 rotation map of an East Project
- Calling startActivity() from outside of an Activity context requires the FLAG_ ACTIVITY_ NEW_ TASK flag
- JMeter -- prometheus+grafana server performance visualization
猜你喜欢

Go小白实现一个简易的go mock server

Cf. bits and pieces (subset pressing DP + pruning)

Vsftpd2.3.4 port penetration 6200 IRC_ 3281_ backdoor

QT—动画框架

【“码”力全开,“章”显实力】2022年第1季Task挑战赛贡献者榜单

Eternal Blue ms17-010exp reappears

The 5th Digital China Construction summit opened in Fuzhou, Fujian

Typora is still the most beautiful and beautiful document editing artifact of yyds in my heart. I believe you will never abandon it

Getting started with MySQL database

Number of times a number appears in an ascending array
随机推荐
Ionic4 learning notes 10 rotation map of an East Project
缺失值处理
【刷题记录】20. 有效的括号
全国职业院校技能大赛网络安全竞赛之数据分析数字取证-A
BOM understanding in odoo
How to solve the problem that yaml in idea is unrecognized or red?
空间三点画圆代码
[Tkinter] common components (II)
redis 数据类型
Ionic4 learning notes 11 - popular goods display of an East Project
初识Pytorch和Pytorch环境配置
微信小程序逆向
Segment tree merge board
Cryptography knowledge - Introduction to encryption -1
字符串的遍历及拼接
Several sorting methods for while and sort
线段树合并板子
Mid year inventory | in 2022, PAAS will be upgraded again
Techempower web framework performance test 21st round results release --asp Net core continue to move forward
Type-C边充边听PD协议芯片