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. pushlet

    自己准备做一个小游戏,租个云服务,然后挂在网上,可以跟同学一起玩,不过首先布置的是,这个游戏是否能实现,多人在线网页游戏,考虑到是否能够实时查询,在网上借鉴了下聊天原理,http长连接,搜索到push ...

  2. HTML中的select只读

    因为Select下拉框只支持disabled属性,不支持readOnly属性,而在提交时,disabled的控件,又是不提交值的.现提供以下几种解决方案: 1.在html中使用以下代码,在select ...

  3. centos设置静态ip地址

    1.修改网卡配置 编辑:vi /etc/sysconfig/network-scripts/ifcfg-eth0 DEVICE=eth0 #描述网卡对应的设备别名,例如ifcfg-eth0的文件中它为 ...

  4. 对于java.lang.NoSuchMethodError: antlr.collections.AST.getLine()I错误解决

    在J2EE框架下开发web网站,这种问题经常遇到,只要我们网上搜一下,就可以看到很多版本的,我整理一下: 第一种可能性解决:看看我的项目:主要 是里面的Structs 1.3 (structs 2)和 ...

  5. ERROR 1872 (HY000): Slave failed to initialize relay log info structure from the repository

    salve复制线程停止,尝试start slave 时报ERROR 1872错误mysql> system perror 1872 MySQL error code 1872 (ER_SLAVE ...

  6. GreenDao数据库框架的配置与增删改查

    并非原创,原创地址http://blog.csdn.net/njweiyukun/article/details/51893092 配置-------------------------------- ...

  7. ie6下js更新元素display:block后,仍然不显示的hack办法

    $hotGames.html(html).removeClass("hide").show();//代码执行到这里,在ie6下仍然无法正常显示 //只有执行了下边的两行代码后,才正 ...

  8. hash表C语言实现

    算法参考<算法导论>第11章散列表.采用链地址法解决冲突. #include <stdio.h> #include <stdlib.h> #include < ...

  9. Your build settings specify a provisioning profile with the UUID, no provisioning profile

    在Archive项目时,出现了“Your build settings specify a provisioning profile with the UUID “”, however, no suc ...

  10. esxi 6 虚拟机安装复制

    打开VMware vSphere Client 连接到esxi服务器,选择配置-存储器, 右击存储器标识,选择浏览存储数据, 首先新建一个文件夹,用来存放系统镜像,这里新建了iso文件夹, 选择iso ...