Java String 字符串操作小结
// 转载加编辑 -- 21 Apr 2014
1. Java字符串中子串的查找
Java中字符串中子串的查找共有四种方法,如下:
1、int indexOf(String str) :返回第一次出现的指定子字符串在此字符串中的索引。
2、int indexOf(String str, int startIndex):从指定的索引处开始,返回第一次出现的指定子字符串在此字符串中的索引。
3、int lastIndexOf(String str) :返回在此字符串中最右边出现的指定子字符串的索引。
4、int lastIndexOf(String str, int startIndex) :从指定的索引处开始向后搜索,返回在此字符串中最后一次出现的指定子字符串的索引。
说明
indexOf 方法返回一个整数值,指出 String 对象内子字符串的开始位置。如果没有找到子字符串,则返回-1。
如果 startindex 是负数,则 startindex 被当作零。如果它比最大的字符位置索引还大,则它被当作最大的可能索引。
从左向右执行查找。否则,该方法与 lastIndexOf 相同。
2. Java String 字符串分割及日期转换
//字符串分割:
String splitStr = new String("abc,def,ghi,gkl");
String [] newsplitstr = splitStr.split(",");
for(int i=0;i<newsplitstr.length;i++)
{
System.out.println(newsplitstr[i]);
}
String [] newsplitstr2 = splitStr.split(",",2);//限定拆分次数
for(int i=0;i<newsplitstr2.length;i++)
{
System.out.println(newsplitstr2[i]);
}
//格式化字符串:
//日期格式化
//%te 一个月中的某一天(1-31)
//%tb 月份
//%tB 月份的全称
//%tA 星期几全称
//%ta 星期几
//%tc
//%tY 4位年份(2009)
//%tj 一年中的第几天(001-366)
//%tm 月份
//%td 一个月中的第几天(01-31)
//%ty 2位年份
Date date = new Date();
String dates = String.format("%te", date);
String datedays = String.format("%tj", date);
System.out.println("日期:"+dates);
System.out.println("一年中的第几天:"+datedays);
//时间格式化:
//%tH 2位24小时数:(00-23)
//%tI 2位12小时数:(01-12)
//%tk 2位24小时数:(0-23)
//%tl 2位12小时数:(1-12)
//%tM 2位数字的分钟(00-59)
//%tS 2位数字的秒(00-60)
//%tL 8位毫秒(000-999)
//%tN 9位毫秒()
//%tp 上下午,
//%tz
//%tZ
//%ts 1970-01-01 00:00:00至今经过的秒
//%tQ 1970-01-01 00:00:00至今经过的毫秒
String hour = String.format("%tH", date);
String minute = String.format("%tM", date);
String second = String.format("%tS", date);
String msecond = String.format("%tQ", date);
System.out.println("现在是"+hour+"时"+minute+"分"+second+"秒");
System.out.println("现在是毫秒:"+msecond);
//日期时间组合:
//%tF 年-月-日
//%tD 月/日/年
//%tc 全部日期和时间信息
//%tr 时分秒上下午
//%tT 时分秒
//%tR 时分
String time = String.format("%tc", date);
String form = String.format("%tF", date);
String sfm = String.format("%tT", date);
System.out.println("全部时间信息:"+time);
System.out.println("年-月-日格式:"+form);
System.out.println("时分秒:"+ sfm);
System.out.println("date.toString():" + date.toString());//date.toLocaleString()
System.out.println("date.toLocaleString():" + date.toLocaleString());
//使用正则表达式
//字符串生成器:StringBuilder
//builder.append();insert(int offset arg);delete(int start,int end);
StringBuilder bf = new StringBuilder("Hello");
bf.insert(5, "word");
System.out.println(bf.toString());
3. Java字符串常用方法
substring()
它有两种形式,第一种是:String substring(int startIndex)
第二种是:String substring(int startIndex,int endIndex)
concat()
连接两个字符串
replace()
替换。 它有两种形式,第一种形式用一个字符在调用字符串中所有出现某个字符的地方进行替换,形式如下:
String replace(char original,char replacement)
例如:String s=”Hello”.replace(’l',’w');
第二种形式是用一个字符序列替换另一个字符序列,形式如下:
String replace(CharSequence original,CharSequence replacement)
trim()
去掉起始和结尾的空格
valueOf()
转换为字符串
toLowerCase()
转换为小写
toUpperCase()
转换为大写
length()
取得字符串的长度
charAt()
截取一个字符
getChars()
截取多个字符
void getChars(int sourceStart,int sourceEnd,char target[],int targetStart)
sourceStart 指定了子串开始字符的下标
sourceEnd 指定了子串结束后的下一个字符的下标。因此,子串包含从sourceStart到sourceEnd-1的字符。
target 指定接收字符的数组
targetStart target中开始复制子串的下标值
getBytes()
替代getChars()的一种方法是将字符存储在字节数组中,该方法即getBytes()
toCharArray()
例:
String s = “Hello!你好!”;
char[] ss = s.toCharArray();
equals() 和 equalsIgnoreCase()
比较两个字符串
regionMatches()
用于比较一个字符串特定区域与另一特定区域,它有一个重载的形式允许在比较中忽略大小写
boolean regionMatches (int startIndex,String str2,int str2StartIndex,int numChars)
boolean regionMatches (boolean ignoreCase,int startIndex,String str2,int str2StartIndex,int numChars)
startsWith() 和 endsWith()
startsWith()方法决定是否以特定字符串开始,endWith()方法决定是否以特定字符串结束
equals() 和 ==
equals()方法比较字符串对象中的字符,==运算符比较两个对象是否引用同一实例。
compareTo() 和 compareToIgnoreCase()
比较字符串
indexOf() 和 lastIndexOf()
indexOf() 查找字符或者子串第一次出现的地方。
lastIndexOf() 查找字符或者子串是后一次出现的地方。
StringBuffer构造函数
StringBuffer定义了三个构造函数:
StringBuffer()
StringBuffer(int size)
StringBuffer(String str)
StringBuffer(CharSequence chars)
下面是StringBuffer相关的函数:
length()和capacity()
一个StringBuffer当前长度可通过length()方法得到,而整个可分配空间通过capacity()方法得到。
ensureCapacity()
设置缓冲区的大小
void ensureCapacity(int capacity)
setLength()
设置缓冲区的长度
void setLength(int len)
charAt() 和 setCharAt()
char charAt(int where)
void setCharAt(int where,char ch)
getChars()
void getChars(int sourceStart,int sourceEnd,char target[],int targetStart)
append()
可把任何类型数据的字符串表示连接到调用的StringBuffer对象的末尾。
例:int a=42;
StringBuffer sb=new StringBuffer(40);
String s=sb.append(”a=”).append(a).append(”!”).toString();
insert()
insert字符串
StringBuffer insert(int index,String str)
StringBuffer insert(int index,char ch)
StringBuffer insert(int index,Object obj)
index指定将字符串insert到StringBuffer对象中的位置的下标。
reverse()
颠倒StringBuffer对象中的字符
StringBuffer reverse()
delete() 和 deleteCharAt()
删除字符
StringBuffer delete(int startIndex,int endIndex)
StringBuffer deleteCharAt(int loc)
replace()
替换
StringBuffer replace(int startIndex,int endIndex,String str)
substring()
截取子串
String substring(int startIndex)
String substring(int startIndex,int endIndex)
Java String 字符串操作小结的更多相关文章
- android TextView字体设置最少占多少行. 及其 Java String 字符串操作 . .
① 字体设置: 修改代码 : GridViewActivity.java priceTv为 TextView priceTv.setMaxLines(3); //当多与7个字fu的时候 , 其余字 ...
- java String字符串操作 字符串加密等
子串加密 1,设计思想 (1)输入一个字符串 (2)通过toCharArray()的方法将字符串转换成字符数组 (3)新建一个字符数组用来存储修改后的字符数组 2,程序流程图 3,源代码 packag ...
- JAVA作业—字符串操作
------------恢复内容开始------------ ------------恢复内容开始------------ ------------恢复内容开始------------ ------- ...
- Java的字符串操作
目录 Java的字符串操作 一.不同字符串操作的对比 1.1 C++中const修饰指针 const在星号的左边,是被指向的常量不可变 const在星号的右边,是指针的指向不可变 二. Java字符串 ...
- Java String字符串/==和equals区别,str。toCharAt(),getBytes,indexOf过滤存在字符,trim()/String与StringBuffer多线程安全/StringBuilder单线程—— 14.0
课程概要 String 字符串 String字符串常用方法 StringBuffer StringBuilder String字符串: 1.实例化String对象 直接赋值 String str=& ...
- Java的字符串操作一些简单的思考
Java的字符串操作 1 .1不可变的String String对象事不可变的,String类中的每一个看起来会修改String值的方法,实际上都是创建了一个全新的String对象,以包含修改后的字符 ...
- java String字符串
五.java数据类型之String(字符串) CreateTime--2017年7月21日16:17:45 Author:Marydon (一)数据格式 (二)初始化 // 方式一 String ...
- Java String字符串深入详解
Java中字符串对象创建有两种形式,一种为字面量形式,如String str = "hello";,另一种就是使用new这种标准的构造对象的方法,如String str = new ...
- java类库字符串操作
在java类库中,java给我们提供了字符串几个特别的操作,分别是String,Stringbuffer,Stringbuilder等.下面就对这几个类做一个简单的介绍.首先,我们先了解一下Strin ...
随机推荐
- day17-jdbc 2.jdbc介绍
SQL是一种非过程性语言,只能写一条嘛,你写多条不行嘛.每个数据库都有自己的存储过程.你可以做编程,你可以写多条SQL语句把它放到一起.这就是存储过程.然后用的时候一调它就执行这个逻辑结构了.因为多条 ...
- web网页 页面布局的几种方式(转)
web网页 页面布局的几种方式 转载 2017年06月16日 12:19:40 2218 网页基本布局方式: (1)流式布局 Fluid 流布局与固定宽度布局基本不同点 就在于对网站尺寸的侧量单位不同 ...
- css知多少(6)——选择器的优先级(转)
css知多少(6)——选择器的优先级 1. 引言 上一节<css知多少(5)——选择器>最后提到,选择器类型过多将导致一些问题,是什么问题呢?咱们直接举例子说明. 上图中,css中的两 ...
- Tensorflow练习
# coding: utf-8 import tensorflow as tf from tensorflow.examples.tutorials.mnist import input_data # ...
- 【转】pecl,pear的不同
PEAR是PHP扩展与应用库(the PHP Extension and Application Repository)的缩写.它是一个PHP扩展及应用的一个代码仓库,基于php代码的,安装目录在/u ...
- vue 之 Nodejs介绍
Nodejs英文网:https://nodejs.org/en/ 中文网:http://nodejs.cn/ 我们会发现这样一句话: 翻译成中文如下: Node.js 是一个基于 Chrome V8 ...
- unix 下 shell 遍历指定范围内的日期
UNIX下遍历日期,date 没有 -d 的参数,所以需要自己处理. 下面使用时差的方法进行计算,遍历的日期是降序的 #!/usr/bin/ksh . $HOME/.profile timelag= ...
- 关于 windows mobile 进程操作的方法
#region Process class /// <summary> /// Summary description for Process. /// </summary> ...
- redis系列:哨兵
1 简介 Sentinel(哨兵)是Redis 的高可用性解决方案:通过哨兵可以创建一个当主服务器出现故障时自动将从服务器升级为主服务器的一个分布式系统.解决了主从复制出现故障时需要人为干预的问题. ...
- [raspberry pi3] aarch64 mongodb 编译和安装
raspberry pi3官方支持是32bit的系统,使用mongodb的时候有2G数据库大小的限制,32bit的系统上数据大点基本上就可以认为不能用了,所以要装64bit的opensuse. 安装了 ...