字符串 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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/380501.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

移动硬盘linux逻辑分区的区别,主磁盘分区、扩展磁盘分区、逻辑分区的区别

硬盘分区有三种&#xff0c;主磁盘分区、扩展磁盘分区、逻辑分区。一个硬盘可以有一个主分区&#xff0c;一个扩展分区&#xff0c;也可以只有一个主分区没有扩展分区。逻辑分区可以若干。主分区是硬盘的启动分区&#xff0c;他是独立的&#xff0c;也是硬盘的第一个分区&#…

hdu1247

一道水题&#xff0c;居然改错改了一个多小时 题目的意思是其实就是找出一个单词&#xff0c;前半部是一个出现过的单词&#xff0c;后半部也是&#xff0c;记住&#xff0c;要严格满足这个条件 所以&#xff0c;其实也就是先查找一个单词的是否有前缀&#xff0c;再用这个单词…

FPU数据寄存器

FPU有8个独立的、可寻址的80位数据寄存器R0-R7&#xff0c;如下图所示&#xff0c;这些寄存器合称为寄存器栈。FPU 状态字中名为 TOP 的一个 3 位字段给出了当前处于栈顶的寄存器编号。例如&#xff0c;在下图中&#xff0c;TOP 等于二进制数 011&#xff0c;这表示现在栈顶为 …

计算当月的天数(sql)

declare dt datetime set dtgetdate() --set dt2006-8-8 SELECT 32-Day(dt32-Day(dt)) 转载于:https://www.cnblogs.com/super-yc/archive/2006/11/04/549646.html

使用Java中的equals()和compareTo()方法比较字符串

Given strings and we have to compare them using equals() and compareTo() method. 给定字符串&#xff0c;我们必须使用equals()和compareTo()方法进行比较。 Java string equals() method Java字符串equals()方法 Java string equals() method compares the content of …

poj 2528_2

代码&#xff1a; #include<iostream> #include<fstream>using namespace std;struct e{int l,r;bool isa; };e tree[80001]; int n;struct f{int num,s,l; };f b[20001];int c[10001][2];int cmp(const void *a,const void *b){f *s(f*)a;f *t(f*)b;return s->…

linux 命令修改网络,linux网络命令学习

用命令修改的均为临时生效&#xff0c;重启后失效&#xff0c;要想永久生效需更改配置文件设置主机名vi etc/sysconfig/network查看主机名hostname设置IP地址ifconfig 网卡名 IP地址 netmask 子网掩码ifconfig etho 10.0.0.1 netmask 255.0.0.0查看某快网卡的状况ifconfig 网卡…

素数求和问题

描述 现在给你N个数(0< N<1000) ,现在要求你写出一个程序&#xff0c;找出这N个数中的所有素数&#xff0c;并求和。 输入 第一行给出整数M(0< M<10)代表多少组测试数据 每组测试数据第一行给你N&#xff0c;代表该组测试数据的数量。 接下来的N个数为要测试的…

ITIL(信息技术基础构架库)!

新找到一个网址&#xff1a;www.itsm.info&#xff0c;英文的&#xff01; 转载于:https://www.cnblogs.com/tankhorse/archive/2006/11/09/555512.html

常用汇编浮点操作指令

如果不知道ST&#xff08;0&#xff09;&#xff0c;可以看这篇文章&#xff1a;FPU数据寄存器 浮点数载入指令&#xff1a; fld src ;将浮点数src压入ST&#xff08;0&#xff09; fild src ;将整数src压入ST&#xff08;0&#xff09; ;f&#xff1a;float&#xff08;浮点…

是雏还是鹰mdash;mdash;编程规范之代码注释

注释能使代码更加容易理解&#xff0c;更加容易跟踪。出色的注释就像一幅好的设计蓝图&#xff0c;能够引导阅读者通过你的应用程序的曲折之处&#xff0c;能够说明预期的运行结果和可能出现的异常情况。注释可以为后来的维护者带来极大的方便&#xff0c;无论是类说明注释&…

Java PropertyPermission newPermissionCollection()方法与示例

PropertyPermission类newPermissionCollection()方法 (PropertyPermission Class newPermissionCollection() method) newPermissionCollection() method is available in java.util package. newPermissionCollection()方法在java.util包中可用。 newPermissionCollection() m…

C#多线程学习笔记(三)

刚刚把前两天的笔记整理完了&#xff0c;发现做笔记可以加深印象。要坚持做下去&#xff0c;可以学到一些细节的东西。a.今天学到一个非常试用的lock语法:lock(expression) statement_block expression代表你希望跟踪的对象&#xff0c;通常是对象引用。一般地&#xff0c;如果…

linux ida 图形界面,linux – IDA在屏幕内不起作用

我试图在屏幕会话中运行idal64(IDA pro),但是我收到此错误&#xff1a;TVision error: Can not load libcurses.soWithout libcurses can work only with xterm/linuxAborted (core dumped)我安装了’libncurses5-dev’,’libncursesw5-dev’,’lib32ncurses5-dev’和’libx32n…

C和汇编混合编程----printf

今天终于用c和汇编成功调试出第一个程序了&#xff0c;程序很简单&#xff0c;我太菜了&#xff0c;花了几天的时间&#xff0c;才调试好&#xff0c;来记录一下&#xff0c;以防忘记了 先上程序&#xff1a; #include "stdio.h" int main() {char *str"begin\…

(诡异事件)iframe标签后面的alert不执行

今天做项目的过程中&#xff0c;发现一个非常奇怪的事情。iframe标签后面的js 不执行&#xff1f; 把代码贴出来&#xff0c;看看有没有大牛碰到这种情况。谢谢。 <html xmlns"http://www.w3.org/1999/xhtml"><head> <title>Untitled Page</…

kotlin 判断数字_Kotlin程序检查给定数字是正数,负数还是零

kotlin 判断数字A positive number is a number which is greater than 0, a negative number is a number which is less than 0, else the number is Zero. 正数是大于0的数字&#xff0c;负数是小于0的数字&#xff0c;否则为零。 Given a number num, we have to check wh…

gz格式linux怎么打开,linux 下载解压gz文件怎么打开

一般用WINRAR下载地址&#xff1a;http://www.onlinedown.net/soft/5.htm如何下载安装winrar并打开“压缩文件”如何下载、安装winrar&#xff0c;并打开“压缩文件”网上有很多文件都是压缩格式&#xff0c;经过压缩后的文件由于其体积较小&#xff0c;因而比较适合网络传输&a…

c和汇编混合编程----main的反汇编

想看一下main函数的反汇编程序&#xff0c;分析一下&#xff1a; int main() {return 0; } 反汇编&#xff1a; 1: int main() 2: { 00401010 55 push ebp &#xff1b;将ebp压入栈 00401011 8B EC mov ebp,esp &am…

转 商业软件编程很无聊?

这周读到三篇博客帖子。把它们串在一块儿读&#xff0c;对我们的职业发展非常有教育意义。 一篇是Thoughtworks前员工Ravi Mohan写的&#xff0c;《但是马老大&#xff0c;商业编程就是无聊》。Martin Fowler在一篇帖子里说&#xff0c;编写企业软件不光是捣腾数据。并不是只有…