|
|
长情的台灯 · 陕西高校专升本招生普通本科及职教本科征集志愿 ...· 6 月前 · |
|
|
会搭讪的大葱 · 生态环境部召开1月例行新闻发布会_中华人民共 ...· 1 年前 · |
|
|
坚韧的凉面 · 浙江省教育收费政策(2022年)· 1 年前 · |
|
|
发怒的楼房 · 天理学术|张志明&鲁统部教授团队在异双核团簇 ...· 1 年前 · |
|
|
玩足球的筷子 · “暮云”:小孩子两岁4个月,现在很喜欢看手机 ...· 1 年前 · |
The Java String charAt() method returns the character at the specified index. The index value should lie between 0 and length() – 1. Let us check more points about the String charAt() method in Java.
public char charAt(int index)
The following examples demonstrate how to use charAt() method:
To show the working of the charAt() method
public
static
void
main(String args[])
String s =
"Welcome! to Geeksforgeeks Planet"
;
char
ch = s.charAt(
3
);
System.out.println(ch);
ch = s.charAt(
0
);
System.out.println(ch);
Output
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 4
at java.lang.String.charAt(String.java:658)
at Gfg.main(File.java:9)
// Java Program to implement
// Print Characters Presented at
// Odd Positions and Even Positions
import
java.io.*;
// Driver Class
class
GFG {
// main function
public
static
void
main(String[] args)
String str =
"GeeksforGeeks"
;
// Odd Positions
System.out.println(
"Odd Positions"
);
for
(
int
i =
0
; i < str.length(); i +=
2
) {
System.out.print(str.charAt(i));
System.out.println(
"\n"
);
// Even Positions
System.out.println(
"Even Positions"
);
for
(
int
i =
1
; i < str.length(); i +=
2
) {
System.out.print(str.charAt(i));
for
(
int
i =
0
; i < str.length(); i++) {
// Counting e in string
if
(str.charAt(i) ==
'e'
)
count++;
// printing occurrence
System.out.println(
"Count the occurrence of e : "
+ count);
|
|
坚韧的凉面 · 浙江省教育收费政策(2022年) 1 年前 |