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

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. python 环境安装

    wget http://www.python.org/ftp/python/2.7.3/Python-2.7.3.tgz  tar zxf Python-2.7.3.tgz  cd Python-2. ...

  2. oracle with as 用法

    http://blog.itpub.net/28371090/viewspace-1190141/

  3. PHP面试试题

    1,用PHP打印出前一天的时间,格式是2006-5-10 22:21:21echo date("Y:m:d H:i:s",strtotime("-1 day") ...

  4. bug-android之app:mergeDebugResources

    bug描述:Error:Execution failed for task ':app:mergeDebugResources'. > Crunching Cruncher seekbar_th ...

  5. 巧用linux服务器的/dev/shm/,如果合理使用,可以避开磁盘IO不给力,提高网站访问速度。

    巧用linux服务器的/dev/shm/ 巧用linux服务器的/dev/shm/,如果合理使用,可以避开磁盘IO不给力,提高网站访问速度. 首先让我们认识一下,什么是tmpfs和/dev/shm/? ...

  6. css 图形,非常完美

    http://www.360doc.com/content/12/0327/13/8674_198243134.shtml

  7. NGUI 图片变灰

    效果图 1.先准备好一个变灰shader.代码如下 Shader "Custom/Gray" { Properties { _MainTex ("Base (RGB), ...

  8. iOS 关于UIWindow的理解

    Every iOS app has a window that handles the presentation of the app’s user interface. Although the w ...

  9. vc++创建文件目录

    #include "stdafx.h" #include <iostream> #include <fstream> #include <string ...

  10. Win7下同时使用有线和无线时的优先级设置

    终于找到这个问题的解决方案了!!!!我是通过方法1改跃点数实现的,方法2无效. http://linshengling.blog.163.com/blog/static/114651912012102 ...