日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > java >内容正文

java

字符串 charat_Java | String.charAt(index)| 从字符串中按索引获取字符

發(fā)布時(shí)間:2023/12/1 java 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 字符串 charat_Java | String.charAt(index)| 从字符串中按索引获取字符 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

字符串 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()函數(shù)是String類的庫(kù)函數(shù),用于從字符串中獲取/檢索特定字符。 其中,索引從0開始,到String.lenght-1結(jié)束。

For example, if there is string "Hello", its index will starts from 0 and end to 4.

例如,如果存在字符串“ Hello” ,則其索引將從0開始到4結(jié)束。

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.

注意:如果您嘗試超出范圍訪問(wèn)字符,則會(huì)生成異常StringIndexOutOfBoundsException 。 因此,在字符串中使用索引時(shí)要小心。

Example1:

范例1:

In this example, there is string initialized by "Hello world!" and we have to access its 0th and 7th character.

在此示例中,存在由“ Hello world!”初始化的字符串。 并且我們必須訪問(wèn)其 0和 7 個(gè)字符。

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: HCharacter at 7th index: o .minHeight{min-height: 250px;}@media (min-width: 1025px){.minHeight{min-height: 90px;}} .minHeight{min-height: 250px;}@media (min-width: 1025px){.minHeight{min-height: 90px;}}

Example2:

范例2:

In this example, we have to read a string and print it character by character

在此示例中,我們必須讀取字符串并逐個(gè)字符打印

import java.util.*;public class Example2 {public static void main (String[] args) throws java.lang.Exception{//string ObjectString msg = null;//Scanner class ObjectScanner scan = new Scanner(System.in);//input a stringSystem.out.println("Input a string: ");msg = scan.nextLine();//printing string character by characterSystem.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

總結(jié)

以上是生活随笔為你收集整理的字符串 charat_Java | String.charAt(index)| 从字符串中按索引获取字符的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。