一、得到字符串对象的有关信息

1.通过调用length()方法得到String的长度.

String str=”This is a String”;

int len =str.length();

2.StringBuffer类的capacity()方法与String类的 length()的方法类似,但是她测试是分配给StringBuffer的内存空间的大小,而不是当前被使用了的内存空间。

3.如果想确定字符串中指定字符或子字符串在给定字符串的位置,可以用 indexOf()和lastIndexOf()方法。

String str=”This is a String”;

Int index1 =str.indexOf(“i”);   //index=2

Int index2=str.indexOf(‘i’,index+1);   //index2=5

Int index3=str.lastIndexOf(“I”);   //index3=15

Int index4=str.indexOf(“String”);  //index4=10

二、String 对象的比较和操作

1.String 对象的比较

String类的equals()方法用来确定两个字符串是否相等。

String str=”This is a String”;

Boolean result=str.equals(“This is another String “);

//result=false

2.String对象的访问

A、方法charAt()用以得到指定位置的字符。

String str=”This is a String”;

char chr=str.charAt(3); //chr=”i”

B、方法getChars()用以得到字符串的一部分字符串

public void getChars(int srcBegin,int srcEnd,char[]dst,int dstBegin)

String str=”This is a String”;

Char chr =new char[10];

Str.getChars(5,12,chr,0);  //chr=”is a St”

C、subString()是提取字符串的另一种方法,它可以指定从何处开始提取字符串以及何处结束。

3.操作字符串

A、replace()方法可以将字符串中的一个字符替换为另一个字符。

String str=”This is a String”;

String str1=str.replace(‘T’,'t’); //str1=”this is a String”

B、concat()方法可以把两个字符串合并为一个字符串。

String str=”This is a String”;

String str1=str.concat(“Test”); //str1=”This is a String Test”

C、toUpperCase()和toLowerCase()方法分别实现字符串大小写的转换。

String str=”THIS IS A STRING”;

String str1=str.toLowerCase(); //str1=”this is a string”;

D、trim()方法可以将字符串中开头和结尾处的空格去掉.

String str=”This is a String   “;

String str1=str.trim();   // str1=”This is a String”

E、String 类提供静态方法valueOf(),它可以将任何类型的数据对象转换为一个字符串。如

System.out.println(String,ValueOf(math,PI));

关于JAVA的String类的一些方法的更多相关文章

  1. java.lang.String 类的所有方法

    java.lang.String 类的所有方法 方法摘要 char charAt(int index) 返回指定索引处的 char 值. int codePointAt(int index) 返回指定 ...

  2. Java中String类的format方法使用总结

    可参考: http://www.cnblogs.com/fsjohnhuang/p/4094777.html http://kgd1120.iteye.com/blog/1293633 String类 ...

  3. 关于Java中String类的hashCode方法

    首先来看一下String中hashCode方法的实现源码 public int hashCode() { int h = hash; if (h == 0 && value.lengt ...

  4. JAVA中String类的intern()方法的作用

    一般我们变成很少使用到 intern这个方法,今天我就来解释一下这个方法是干什么的,做什么用的 首先请大家看一个例子: public static void main(String[] args) t ...

  5. 103、Java中String类之compareTo()方法

    01.代码如下: package TIANPAN; /** * 此处为文档注释 * * @author 田攀 微信382477247 */ public class TestDemo { public ...

  6. 098、Java中String类之charAt()方法

    01.代码如下: package TIANPAN; /** * 此处为文档注释 * * @author 田攀 微信382477247 */ public class TestDemo { public ...

  7. JAVA中string类的split方法

    split([separator,[limit]])第一个参数为分隔符,可以是一个正则表达式,第二个参数为返回结果数组的长度

  8. Java中String类的方法及说明

    String : 字符串类型 一.      String sc_sub = new String(c,3,2);    //      String sb_copy = new String(sb) ...

  9. 深入分析Java的String类的方法与特点

    字符串是任何编程语言都必须支持的变量类型,有些编程语言是直接提供了原生的变量类型,有些编程语言则使用语法特性以 SDK 的形式提供支持.在Java编程平台中,对字符串的支持使用了后者的形式,就是通过在 ...

随机推荐

  1. HDU 5033 Building

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5033 解题报告:在一条x轴上有n个建筑物,每个建筑物有一个高度h,然后现在有q次查询,查询的内容是假设 ...

  2. Web服务精讲–搭个 Web 服务器(二)

    导读 曾几何时,你所选择的 Python Web 框架会限制你所可选择的 Web 服务器,反之亦然.如果某个框架及服务器设计用来协同工作的,那么一切正常. 在第一部分中,我提出了一个问题:“如何在你刚 ...

  3. 仿jQuery的siblings效果的js原生代码

    仿jQuery的siblings效果的js原生代码 <previousSibling> 属性返回选定节点的上一个同级节点(在相同树层级中的前一个节点). <nextSibling&g ...

  4. div 加滚动条

    div 加滚动条的方法: <div style="position:absolute; height:400px; overflow:auto"></div> ...

  5. 1.5---字符串压缩(CC150)

    import java.util.*; public class Zipper { public String zipString(String str) { // write code here i ...

  6. discuz内置常用CSS代码分析

    CSS多IE下兼容HACK写法 所有 IE浏览器适用:.ie_all .foo { ... } IE6 专用:.ie6 .foo { ... } IE7 专用:.ie7 .foo { ... } IE ...

  7. Python sorted函数对列表排序

    http://jingyan.baidu.com/article/f3ad7d0ffe8e1409c2345b48.html http://www.cnblogs.com/100thMountain/ ...

  8. ubuntu 16.04 apt-get error: in the drive /media/cdrom and press

    If you have an internet connection, you can safely comment out the line starting with deb cdrom: ... ...

  9. C++构造函数初始化顺序

    [本文链接] http://www.cnblogs.com/hellogiser/p/constructor-order.html 1.构造函数.析构函数与拷贝构造函数介绍 构造函数 构造函数不能有返 ...

  10. ios delegate 使用注意 assign,weak

    今天一个同事写代码,把一个delegate对象设定成了assign类型属性,没有用weak,就是delegate对象释放后,不会把delegate指针自动设定为nil,把对象的delegate设定成了 ...