欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程资源 > 编程问答 >内容正文

编程问答

android String.charAt的使用

发布时间:2024/4/15 编程问答 57 豆豆
生活随笔 收集整理的这篇文章主要介绍了 android String.charAt的使用 小编觉得挺不错的,现在分享给大家,帮大家做个参考.


今天,简单讲讲String里的 

charAt()的使用。


描述

java.lang.String.charAt() 方法返回指定索引处的char值。索引范围是从0到length() - 1。对于数组索引,序列的第一个char值是在索引为0,索引1,依此类推,

声明

以下是声明java.lang.String.charAt()方法

public char charAt(int index)

参数

  • index -- 这是该指数的char值.

返回值

此方法返回这个字符串的指定索引处的char值。第一个char值的索引为0.

异常

  • IndexOutOfBoundsException -- 如果index参数为负或不小于该字符串的长度.

实例

下面的示例演示使用的java.lang.String.charAt()方法.

package com.yiibai;import java.lang.*;public class StringDemo {public static void main(String[] args) {String str = "This is yiibai";// prints character at 1st locationSystem.out.println(str.charAt(0));// prints character at 5th location i.e white-space characterSystem.out.println(str.charAt(4));// prints character at 18th location System.out.println(str.charAt(17));} }


让我们编译和运行上面的程序,这将产生以下结果。它也会打印空白字符。

Tp


这个实在太简单了,其实使用String.subString(i,i+1)也可以实现同样的效果。


android String.charAt的使用就讲完了。


就这么简单。

总结

以上是生活随笔为你收集整理的android String.charAt的使用的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。