< java.lang >-- String字符串
java中用String类进行描述。对字符串进行了对象的封装。这样的好处是可以对字符串这种常见数据进行方便的操作。对象封装后,可以定义N多属性和行为。
如何定义字符串对象呢?String s = "abc";只要是双引号引起的数据都是字符串对象。
特点:字符串一旦被初始化,就不可以被改变,存放在方法区中的常量池中。
------------------------------------------------------
String s1 = "abc"; // s1指向的内存中只有一个对象abc。
String s2 = new String("abc"); // s2指向的内容中有两个对象abc、new 。
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字符串的更多相关文章
- java.lang.String 字符串操作
1.获取文件名 //获取文件名,即就是去掉文件的后缀 /** * mypic.jpg * 获取文件名 * 1. 先找到"."的位置 * 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 ...
- 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 ...
- [Ljava.lang.String和java.lang.String区别
在做项目时报了一个got class [Ljava.lang.String的提示,当时看到[Ljava.lang.String这个时,感觉有点怪怪的,第一次遇到这种情况.最后在网上查了下才明白.是数组 ...
- java.lang.String
1.String 是一个类,广泛应用于 Java 程序中,相当于一系列的字符串.在 Java 语言中 strings are objects.创建一个 strings 最直接的方式是 String g ...
- 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 ...
- java.lang.String 类的所有方法
java.lang.String 类的所有方法 方法摘要 char charAt(int index) 返回指定索引处的 char 值. int codePointAt(int index) 返回指定 ...
- 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] ...
- groovy --不注意的小错误(java.lang.String.positive() is applicable)
sql 语句拼接报错: No signature of method: java.lang.String.positive() is applicable for argument types: () ...
随机推荐
- 织梦dedecms调用栏目的SEO标题、描述、关键字的方法
调用SEO标题:<title>{dede:field.title/}_{dede:field.seotitle /}-{dede:global.cfg_webname/}</titl ...
- angular $apply()以及$digest()讲解1
一些知名的批评和缺陷.他们都涉及到$digest loop(更新周期)中一个很常见的问题:如何在Angular之外更新$scope? 在哪调用 $apply? 更佳的做法是确保你是在$digest l ...
- linux 文本处理
tr,awk,sed 一:tr 1.大小写转换 cat file | tr [a-z] [A-Z] > new_file(大写 --> 小写) cat file | tr [A-Z] [a ...
- ★★★.NET 在meta标签中使用表达式设置页面的关键字
在aspx文件中 给meta标签的属性复制是不能直接使用 表达式的 错误的写法: <meta name="keywords" content="<%=news ...
- set 赋值(转载)
名著<C#设计模式>第9章“观察者模式”涉及了标准的事件处理流程,作者在探讨属性值变更时给出一个如下示例代码(P73-74): pulbic abstract class TpeakFun ...
- FusionCharts X轴显示方式
本文摘自ITYPE 网站的wangxiao5530 原文网址为:http://wangxiao5530.iteye.com/blog/1454200 1)Wrap Mode (换行模式) <ch ...
- WordPress 主题开发 - (七) 让主题更安全 待翻译
We're just about ready to start building our theme's template files. Before we do this, however, it' ...
- 怎么做QQ、微信等消息气泡
主XML: <?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android ...
- android 特效UI实现
弧形菜单 https://github.com/daCapricorn/ArcMenu
- [php]php数组函数的相关
1.8.26---list与extract的作用区别extractextract -- 从数组中将变量导入到当前的符号表 说明int extract ( array var_array [, int ...