java中用String类进行描述。对字符串进行了对象的封装。这样的好处是可以对字符串这种常见数据进行方便的操作。对象封装后,可以定义N多属性和行为。

如何定义字符串对象呢?String s = "abc";只要是双引号引起的数据都是字符串对象。

特点字符串一旦被初始化,就不可以被改变,存放在方法区中的常量池中。

------------------------------------------------------

String s1 = "abc"; // s1指向的内存中只有一个对象abc

String s2 = new String("abc"); // s2指向的内容中有两个对象abcnew

 

System.out.println(s1==s2);//false

System.out.println(s1.equals(s2));//true ,字符串中equals比较的是字符串内容是否相同。

-------------------------------------------------------

字符串的方法:

 

1:构造方法:将字节数组或者字符数组转成字符串。

String s1 = new String();//创建了一个空内容的字符串。

String s2 = null;//s2没有任何对象指向,是一个null常量值。

String s3 = "";//s3指向一个具体的字符串对象,只不过这个字符串中没有内容。

//一般在定义字符串时,不用new

String s4 = new String("abc");

String s5 = "abc"; 一般用此写法

new String(char[]);//将字符数组转成字符串。

new String(char[],offset,count);//将字符数组中的一部分转成字符串。

2:一般方法:

按照面向对象的思想:

2.1 获取:

2.1.1:获取字符串的长度。length();

2.1.2:指定位置的字符。char charAt(int index);

2.1.3:获取指定字符的位置。如果不存在返回-1,所以可以通过返回值-1来判断某一个字符不存在的情况。

int indexOf(int ch);//返回第一次找到的字符角标

int indexOf(int ch,int fromIndex); //返回从指定位置开始第一次找到的角标

int indexOf(String str); //返回第一次找到的字符串角标

int indexOf(String str,int fromIndex);

int lastIndexOf(int ch);

int lastIndexOf(int ch,int fromIndex);

int lastIndexOf(String str);

int lastIndexOf(String str,int fromIndex);

2.1.4:获取子串。

String substring(int start);//从start位开始,到length()-1为止.

String substring(int start,int end);//从start开始到end为止。//包含start位,不包含end位。

substring(0,str.length());//获取整串

2.2 判断:

2.2.1:字符串中包含指定的字符串吗?

boolean contains(String substring);

2.2.2:字符串是否以指定字符串开头啊?

boolean startsWith(string);

2.2.3:字符串是否以指定字符串结尾啊?

boolean endsWith(string);

2.2.4:判断字符串是否相同

boolean equals(string);//覆盖了Object中的方法,判断字符串内容是否相同。

2.2.5:判断字符串内容是否相同,忽略大小写。

boolean equalsIgnoreCase(string) ;

2.3 转换:

2.3.1:通过构造函数可以将字符数组或者字节数组转成字符串。

2.3.2:可以通过字符串中的静态方法,将字符数组转成字符串。

static String copyValueOf(char[] );

static String copyValueOf(char[],int offset,int count);

static String valueOf(char[]);

static String valueOf(char[],int offset,int count);

2.3.3:将基本数据类型或者对象转成字符串。

static String valueOf(char);

static String valueOf(boolean);

static String valueOf(double);

static String valueOf(float);

static String valueOf(int);

static String valueOf(long);

static String valueOf(Object);

2.3.4:将字符串转成大小写。

String toLowerCase();

String toUpperCase();

2.3.5:将字符串转成数组。

char[] toCharArray();//转成字符数组。

byte[] getBytes();//可以加入编码表。转成字节数组。

2.3.6:将字符串转成字符串数组。切割方法。

String[] split(分割的规则-字符串);

2.3.7:将字符串进行内容替换。注意:修改后变成新字符串,并不是将原字符串直接修改。

String replace(oldChar,newChar);

String replace(oldstring,newstring);

2.3.8: String concat(string); //对字符串进行追加。

String trim();//去除字符串两端的空格

int compareTo();//如果参数字符串等于此字符串,则返回值 0;如果此字符串按字典顺序小于字符串参数,则返回一个小于 0 的值;如果此字符串按字典顺序大于字符串参数,则返回一个大于 0 的值。

< java.lang >-- String字符串的更多相关文章

  1. java.lang.String 字符串操作

    1.获取文件名 //获取文件名,即就是去掉文件的后缀 /** * mypic.jpg * 获取文件名 * 1. 先找到"."的位置 * 2. 从第一个字符开始截取到".& ...

  2. 【spring boot】spring boot 前台GET请求,传递时间类型的字符串,后台无法解析,报错:Failed to convert from type [java.lang.String] to type [java.util.Date]

    spring boot 前台GET请求,传递时间类型的字符串,后台无法解析,报错:Failed to convert from type [java.lang.String] to type [jav ...

  3. Cannot convert value of type [java.lang.String] to required type [java.util.Date] for property 'xxx': no matching editors or conversion strategy found

    今天在完成项目的时候遇到了下面的异常信息: 04-Aug-2014 15:49:27.894 SEVERE [http-apr-8080-exec-5] org.apache.catalina.cor ...

  4. [Ljava.lang.String和java.lang.String区别

    在做项目时报了一个got class [Ljava.lang.String的提示,当时看到[Ljava.lang.String这个时,感觉有点怪怪的,第一次遇到这种情况.最后在网上查了下才明白.是数组 ...

  5. java.lang.String

    1.String 是一个类,广泛应用于 Java 程序中,相当于一系列的字符串.在 Java 语言中 strings are objects.创建一个 strings 最直接的方式是 String g ...

  6. mybatis There is no getter for property named 'xx' in 'class java.lang.String

    转载自://http://www.cnblogs.com/anee/p/3324140.html 用mybatis查询时,传入一个字符串传参数,且进行判断时,会报 There is no getter ...

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

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

  8. spring mvc出现 Failed to convert property value of type 'java.lang.String' to required type 'java.util.Date' for property 'endtime'

    在使用spring mvc中,绑定页面传递时间字符串数据给Date类型是出错: Failed to convert property value of type [java.lang.String] ...

  9. groovy --不注意的小错误(java.lang.String.positive() is applicable)

    sql 语句拼接报错: No signature of method: java.lang.String.positive() is applicable for argument types: () ...

随机推荐

  1. IE CSS HACK

    IE 属性值 HACK .test { background:blue; /*所有浏览器*/ background:yellow\9; /*所有IE浏览器*/ background:green\0; ...

  2. css 字间距离_css 字体字符间距设置

    介绍下css 字间距,使用css来控制字与字之间距离,也叫css字间距方法. 使用到的css样式属性单词text-indent抬头距离,letter-spacing字与字间距. Css字间距.div ...

  3. Oracle 查询字段在什么表

    -- 查询字段在什么表 select * from all_tab_cols t where t.column_name='ABC'; -- 查询字段在什么表并且 判断是否是主键 select * f ...

  4. poj2070

    #include <stdio.h> int main(){ double a,b,c; int s; while(~scanf("%lf%lf%lf",&a, ...

  5. Java c3po

    1.准备通用类 (引用:c3p0-0.9.1.2.jar) package nankang.test; import java.sql.Connection; import com.mchange.v ...

  6. myeclipse关闭properties文件自动转义

    1.如图,源代码是这样: 2.保存后重新打开变成这样: 3.解决方法如下:

  7. 针对BootStrap中tabs控件的美化和完善

    BootStrap中的tabs控件以其简单易用而很受广大开发者的欢迎.但是,它的样式比较单一,如何才能在其原有的基础上做出更加美观的效果呢,我一直在考虑这个问题.另外,Bootstrap中的tabs必 ...

  8. LINUX下网站压力测试工具webbench

    wget http://blog.s135.com/soft/linux/webbench/webbench-1.5.tar.gz tar zxvf webbench-1.5.tar.gz cd we ...

  9. Oracle存储过程知识汇总

    基本语法篇: CREATE OR REPLACE PROCEDURE 存储过程名 //CREATE OR REPLACE PROCEDURE 是一个SQL语句通知Oracle数据库去创建一个叫做ske ...

  10. C#中List〈string〉和string[]数组之间的相互转换

    1,从System.String[]转到List<System.String> System.String[] str={"str","string" ...