Question:

整理String类的Length()、charAt()、 getChars()、replace()、 toUpperCase()、 toLowerCase()、trim()、toCharArray()使用说明。

Answer:

Length():public int length()//求字符串长度,返回字符串长度

String s=”dwfsdfwfsadf”;

System.out.println(s.length());

charAt():public char charAt(int index)//返回字符串中指定位置的字符

String s=”Hello”;

System.out.println(s.charAt(3));

注意:字符串中第一个字符的下标是 0。如果参数 index 不在 0 与 string.length 之间,
该方法将返回一个空字符串。

getChars():public void getChars(int srcBegin,int srcEnd,char[] dst,int dstBegin)//将字符从此字符串复制到目标字符数组。

要复制的第一个字符在索引 srcBegin 处;

要复制的最后一个字符在索引 srcEnd-1 处(因此要复制的字符总数是 srcEnd-srcBegin)。

要复制到 dst 子数组的字符从索引 dstBegin 处开始,并结束于索引.

例如:

String str = "abcdefghikl";
     Char[] ch = new char[8];
     str.getChars(2,5,ch,0);
     就是从str的第二个字母开始一直复制到第五个,一共是3个字符,从ch的第一个开始接受。

replace():public String replace(char oldChar,char newChar)//替换字符串

返回一个新的字符串,它是通过用 newChar 替换此字符串中出现的所有 oldChar 而生成的。

如 果 oldChar 在此 String 对象表示的字符序列中没有出现,则返回对此 String 对象的引用。
   否则,创建一个新的 String 对象,用来表示与此 String 对象表示的字符序列相等的字符序列,
    除了每个出现的 oldChar 都被一个 newChar 替换之外。

注:也就是说调用该函数的字符串是不被改变的,只是返回了一个新的对象,这个新对象可能是对原

字符串的引用,也可能是一个替换之后的新的字符串(和原字符串不同的,地址内容都不同)

toUpperCase():public String toUpperCase()//将字符串全部转换成大写

例如:

System.out.println(new String(“hello”).toUpperCase());

toLowerCse():public String toLowerCase()//将字符串全部转换成小写

例如:

System.out.println(new String(“HELLO”).toLowerCase());

trim():public String trim()//去除字符串两边的空格

String x=”   ax  c   ”;

System.out.println(x.trim());//只去除字符串两边的空格,中间的空格是不受影响的。

toCharArray(): public char[] toCharArray()// 将字符串转换为一个字符数组

例如:

String x=”abcd”

char myChar[]=x.toCharArray();

System.out.println(“myChar[1]”+myChar[1]);

05_整理String类的Length()、charAt()、 getChars()、replace()、 toUpperCase()、 toLowerCase()、trim()、toCharArray()使用说明的更多相关文章

  1. String.equals()方法、整理String类的Length()、charAt()、 getChars()、replace()、 toUpperCase()、 toLowerCase()、trim()、toCharArray()

    equals 是比较的两个字符串是否一样 length() 返回字符串的长度 charAt (int index) 返回index所指定的字符 getChars(int srcBegin,int sr ...

  2. 整理string类常见方法的使用说明

    整理String类的Length().charAt().getChars().replace().toUpperCase().toLowerCase().trim().toCharArray()使用说 ...

  3. string类自定义字符串替换函数replace

    #include <iostream> #include <string> using namespace std; /* *  函数功能:将string字符串中的某些字符替换 ...

  4. String类中一些的方法的应用

    一.整理string类 1.Length():获取字串长度: 2.charAt():获取指定位置的字符: 3.getChars():获取从指定位置起的子串复制到字符数组中:(它有四个参数) 4.rep ...

  5. 关于JAVA的String类的一些方法

    一.得到字符串对象的有关信息 1.通过调用length()方法得到String的长度. String str=”This is a String”; int len =str.length(); 2. ...

  6. Java基础String类

    String是一个对象 String不属于8种基本数据类型(byte, char, short, int, float, long, double, boolean),String是对象,所以其默认值 ...

  7. [Java初探04]__字符串(String类)相关

    前言 接下来将暂时将重心偏移向实际操作,不在将大量时间花费在详细的知识点整理上,将会简略知识总结笔记的记录,加强实际练习的时间,实例练习篇也不再同步进行,我会将部分我觉得重要的源码更新在每节知识点后面 ...

  8. 动手动脑 4 String 类

    动手动脑1: 请运行以下示例代码StringPool.java,查看其输出结果.如何解释这样的输出结果?从中你能总结出什么?       在Java中,内容相同的字串常量(“Hello”)只保存一份以 ...

  9. Java学习笔记 02 String类、StringBuilder类、字符串格式化和正则表达式

    一.String类一般字符串 声明字符串 >>String str 创建字符串 >>String(char a[])方法用于将一个字符数组创建为String对象 >> ...

随机推荐

  1. stella mccartney falabella foldover tote a few eye observed

    Lately, the particular Heyuan City Courtroom retrial, in order to commit the criminal offense of cou ...

  2. 函数的caller属性

    今天我在这里通过一个例子介绍一下函数自身的call属性. 例: function whoCallMe(){ alert("My caller is" + whoCallMe.cal ...

  3. 不安装oracle,使用plsql连接oracle

    通常企业开发时,数据库是不会在我们本地安装的(废话),所以使用plsql时,没必要的话,我们尽量不去安装oracle,太大了: 接下来说一下本人plsql,不安装oracle的使用步骤: 1.个人本地 ...

  4. qt做触摸屏演示程序

    界面效果图: 参考资料: http://blog.csdn.net/orz415678659/article/details/9136575     这个最重要.. https://www.oschi ...

  5. iShare.js分享插件

    iShare.js是一个小巧的分享插件,纯JS编写,不依赖任何第三方库,使用简便. 为啥写这个插件? 因为在搭建个人blog时(还没有搭建好(¯﹃¯)),对目前国内比较受欢迎的分享插件都不太满意,主要 ...

  6. 安装ant问题

    1  设置path, 在windwos 7下要重启 2  重启后,cmd----ant -version  Unable to locate tools.jar. Expected to find i ...

  7. android layout_weight讲解

    Layout_weight是线性布局,也就是LinearLayout里面用到的,下面通过实验来看这个Layout_weight的特性. 1.当控件的属性android:layout_width=&qu ...

  8. jquery插件链接

    1.jquery版本 http://www.jq22.com/jquery-info122 2.jquery属性 http://www.runoob.com/jsref/prop-radio-valu ...

  9. android手动修改density(dpi)的方法

    Android系统中会根据屏幕分辨率范围,制定默认的density,既320(xhdpi),那么我们也可以手动修改density. 修改的方式在system.prop中修改ro.sf.lcd_dens ...

  10. 用户新加入Group

    Linux中一个user可以加入多个分组 以下以用户holmes为例,原始用户组为holmes,新加入用户组users 首先进入root模式 [holmes@KirCentOS share]$ su ...