字符串 charat_Java | String.charAt(index)| 从字符串中按索引获取字符
字符串 charat
String.charAt() function is a library function of String class, it is used to get/retrieve the specific character from a string. Where, the index starts from 0 and ends to String.lenght-1.
String.charAt()函数是String类的库函数,用于从字符串中获取/检索特定字符。 其中,索引从0开始,到String.lenght-1结束。
For example, if there is string "Hello", its index will starts from 0 and end to 4.
例如,如果存在字符串“ Hello” ,则其索引将从0开始到4结束。
Note: If you try to access the character out of bounds, an exception StringIndexOutOfBoundsException will generate. So, be careful while using index in the string.
注意:如果您尝试超出范围访问字符,则会生成异常StringIndexOutOfBoundsException 。 因此,在字符串中使用索引时要小心。
Example1:
范例1:
In this example, there is string initialized by "Hello world!" and we have to access its 0th and 7th character.
在此示例中,存在由“ Hello world!”初始化的字符串。 并且我们必须访问其第 0和第 7 个字符。
public class Example1
{public static void main (String[] args) throws java.lang.Exception
{String msg = "Hello world!";
System.out.println("Character at 0th index: "+ msg.charAt(0));
System.out.println("Character at 7th index: " + msg.charAt(7));
}
}
Output
输出量
Character at 0th index: H
Character at 7th index: o
Example2:
范例2:
In this example, we have to read a string and print it character by character
在此示例中,我们必须读取字符串并逐个字符打印
import java.util.*;
public class Example2
{public static void main (String[] args) throws java.lang.Exception
{//string Object
String msg = null;
//Scanner class Object
Scanner scan = new Scanner(System.in);
//input a string
System.out.println("Input a string: ");
msg = scan.nextLine();
//printing string character by character
System.out.println("Input string is: ");
for(int loop =0; loop<msg.length(); loop++)
System.out.print(msg.charAt(loop));
}
}
Output
输出量
Input a string: I love programming.
Input string is:
I love programming.
翻译自: https://www.includehelp.com/java/String-charAt-function-to-get-character-by-Index-from-string.aspx
字符串 charat
字符串 charat_Java | String.charAt(index)| 从字符串中按索引获取字符相关推荐
- 学习-Java字符串之String类常用方法之字符串长度
任务描述 本关任务:接收输入值(字符串),将该字符串反转输出,例如接收字符串"abc",输出"cba". 相关知识 为了完成本关任务,你需要掌握:字符串常用方法 ...
- Java字符串分割到map_如何在Java中按空格分割字符串并以键值形式存储在map中?...
您可以尝试此解决方案 import java.util.ArrayList; import java.util.Arrays; import java.util.List; public class ...
- oracle index include,oracle中与索引相关的视图—all_indexes | 学步园
ALL_INDEXES描述了与索引有关的信息,为了使部分字段的内容更准确,我们需要通过analyze或者dbms_stats包来收集与索引有关的统计信息(这部分字段标记为带*). 首先看一下官网给出的 ...
- oracle中的index函数,Oracle中的索引详解(整理)
一. ROWID的概念 存储了row在数据文件中的具体位置:64位 编码的数据,A-Z, a-z, 0-9, +, 和 /, row在数据块中的存储方式 SELECT ROWID, last_name ...
- java s.charat_Java中s.charAt(index)用于提取字符串s中的特定字符操作
charAt(int index)方法是一个能够用来检索特定索引下的字符的String实例的方法. charAt()方法返回指定索引位置的char值.索引范围为0~length()-1. 如: str ...
- java中String类是什么_Java中字符串的学习(一)String类的概述及常见方法使用
转载请注明出处http://www.cnblogs.com/devtrees/p/4347079.html (拓展:Api:编程语言对外给我们提供的应用程序接口.) 一.概述: 我们平时上网发帖,帖子 ...
- Java:字符串类String的功能介绍
在java中,字符串是一个比较常用的类,因为代码中基本上处理的很多数据都是字符串类型的,因此,掌握字符串类的具体用法显得很重要了. 它的主要功能有如下几种:获取.判断.转换.替换.切割.字串的获取.大 ...
- 字符串之String类
主要涉及在程序运行初始化之后不能改变的字符串类String和字符串内容可以动态改变的类StringBuffer,以及用于字符串转换词法分析类StringTokenizer:同时还将介绍字符串和其他数据 ...
- Swift5 字符串(String)操作
Swift5 字符串(String)操作 Swift字符串创建 Swift字符串拼接 Swift字符串追加 Swift字符串格式化 Swift获取字符串长度 Swift判断字符串是否为空 Swift遍 ...
最新文章
- 上周热点回顾(10.1-10.7)
- 简单实现一个Websocket服务
- 一阶电路暂态响应的结果分析。_反激式DCDC变换器的分析、计算与仿真精讲
- python中if语句的实例_对python中if语句的真假判断实例详解
- python之条件、循环语句
- c语言编译生成cpp,C语言的编译过程
- Spring使用ajax异步上传文件
- 关于Initializing Spring root WebApplicationContext解决方法
- [原创]fcitx5五笔98版本----安装
- 微信消息记录导出到电脑
- 期刊论文发表会有证书吗
- 移动网络广告优化(速度优化篇)
- 2018服务商口碑榜Top50(3月)重磅出炉
- 解决2K 显示器的尴尬为MacBook 开启HiDPI(新方法支持M1)
- 微信有趣的“隐身”功能!关掉这几个开关,别人怎么都搜不到你
- 系统架构师考试-案例
- git版本回退的最佳操作
- softer nms论文阅读Bounding Box Regression with Uncertainty for Accurate Object Detection
- C++之命令(Command)模式
- 苹果手机怎么恢复丢失的数据?果粉必看!