当前位置:网站首页>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;
}
}
边栏推荐
- Vsftpd2.3.4-端口渗透 6200 irc_3281_backdoor
- 16. What is the difference between target and currenttarget?
- Cf. bits and pieces (subset pressing DP + pruning)
- Variable and immutable data types
- Inoic4 learning notes 2
- 奶头乐理论介绍及个人感悟
- Maximum sum and promotion of continuous subarrays (2)
- How to solve the problem that yaml in idea is unrecognized or red?
- 13. What is the difference between onkeydown, up and onkeypress?
- 全国职业院校技能大赛网络安全竞赛——Apache安全配置详解
猜你喜欢

How to render millions of 2D objects smoothly with webgpu?

今日睡眠质量记录79分

Sword finger offer 21. adjust the array order so that odd numbers precede even numbers

MySQL - bufferpool related information

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

04-分布式资源管理系统YARN

【微信小程序开发】自定义tabBar案例(定制消息99+小红心)

ETL开发工具Kettle下载安装环境搭建及使用教程

Oracle EBS form common objects and their relationships

Network security port 80 - PHP CGI parameter injection Execution Vulnerability
随机推荐
Calling startActivity() from outside of an Activity context requires the FLAG_ ACTIVITY_ NEW_ TASK flag
QT - animation frame
ORM student management system
8. = = and = = =?
Getaverse,走向Web3的远方桥梁
无关的表进行关联查询及null=null条件
Template inheritance and import
Data analysis of network security competition of national vocational college skills competition digital forensics-a
Wechat applet reverse
[opencv] - thresholding
core dump
空间三点画圆代码
middleware
Date function format conversion
Getting started with MySQL database
Pycharm configuring opencv Library
About the writing method of interface 1 chain interpretation 2. Method execution (finally) must be executed
Ionic4 learning notes 4 -- add a tab page
3. Variable declaration promotion?
2020年中职组“网络空间安全”赛项浙江省竞赛任务书及答案(Flag)