// 转载加编辑 -- 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 字符串操作小结的更多相关文章

  1. android TextView字体设置最少占多少行. 及其 Java String 字符串操作 . .

    ①  字体设置: 修改代码 :  GridViewActivity.java priceTv为 TextView priceTv.setMaxLines(3); //当多与7个字fu的时候 , 其余字 ...

  2. java String字符串操作 字符串加密等

    子串加密 1,设计思想 (1)输入一个字符串 (2)通过toCharArray()的方法将字符串转换成字符数组 (3)新建一个字符数组用来存储修改后的字符数组 2,程序流程图 3,源代码 packag ...

  3. JAVA作业—字符串操作

    ------------恢复内容开始------------ ------------恢复内容开始------------ ------------恢复内容开始------------ ------- ...

  4. Java的字符串操作

    目录 Java的字符串操作 一.不同字符串操作的对比 1.1 C++中const修饰指针 const在星号的左边,是被指向的常量不可变 const在星号的右边,是指针的指向不可变 二. Java字符串 ...

  5. Java String字符串/==和equals区别,str。toCharAt(),getBytes,indexOf过滤存在字符,trim()/String与StringBuffer多线程安全/StringBuilder单线程—— 14.0

    课程概要 String 字符串 String字符串常用方法 StringBuffer StringBuilder String字符串: 1.实例化String对象 直接赋值  String str=& ...

  6. Java的字符串操作一些简单的思考

    Java的字符串操作 1 .1不可变的String String对象事不可变的,String类中的每一个看起来会修改String值的方法,实际上都是创建了一个全新的String对象,以包含修改后的字符 ...

  7. java String字符串

      五.java数据类型之String(字符串) CreateTime--2017年7月21日16:17:45 Author:Marydon (一)数据格式 (二)初始化 // 方式一 String ...

  8. Java String字符串深入详解

    Java中字符串对象创建有两种形式,一种为字面量形式,如String str = "hello";,另一种就是使用new这种标准的构造对象的方法,如String str = new ...

  9. java类库字符串操作

    在java类库中,java给我们提供了字符串几个特别的操作,分别是String,Stringbuffer,Stringbuilder等.下面就对这几个类做一个简单的介绍.首先,我们先了解一下Strin ...

随机推荐

  1. CORS实现跨域Ajax

    客户端 #!/usr/bin/env python import tornado.ioloop import tornado.web class MainHandler(tornado.web.Req ...

  2. 关于stickybroadcast

    stickybroadcast顾名思义,粘性广播,从字面上我们可以联想到service的返回值中也有个一stick,在service中stick作用是当返回了之后服务被杀死,会重启服务. 但是这里的s ...

  3. String/StringBuilder 类 用对象数组实现登录注册功能

    一.需求说明:实现用户注册.登陆功能: 程序中使用一个长度为3的对象数组,存储用户的登录名和密码: 例如如下格式: 登录名    密码      生日           爱好 zhangsan 11 ...

  4. Luogu 3267 [JLOI2016/SHOI2016]侦察守卫

    以后要记得复习鸭 BZOJ 4557 大佬的博客 状态十分好想,设$f_{x, i}$表示以覆盖完$x$为根的子树后还能向上覆盖$i$层的最小代价,$g_{x, i}$表示以$x$为根的子树下深度为$ ...

  5. Luogu 2254 [NOI2005]瑰丽华尔兹

    简单dp,设$f_{i,j,k}$表示第i个时间段,钢琴处在(j,k)位置移动距离的最大值,那么有转移 $f_{i, j, k} = max(f_{i - 1, j, k}) ,  f_{i, j, ...

  6. python3-password在输入密码时隐藏密码

    # Auther: Aaron Fan #这个脚本请在命令行去执行才可以试出效果,pycharm这里无法测试这个脚本,切记!import getpass _username = "Aaron ...

  7. Entity Framework Tutorial Basics(3):Entity Framework Architecture

    Entity Framework Architecture The following figure shows the overall architecture of the Entity Fram ...

  8. Jsp入门第二天

    1. JSP 指令: JSP指令(directive)是为JSP引擎而设计的. 它们并不直接产生任何可见输出, 而只是告诉引擎如何处理JSP页面中的其余部分. 2. 在目前的JSP 2.0中,定义了p ...

  9. C#多线程的简单例子

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  10. Umbraco中如何找到home node

    在一个Umbraco项目中,我们经常会出现需要找到这个项目的home node的情况, 那么如何来找到项目的home node呢 方法如下: 1. 在View中 @inherits Umbraco.W ...