Java中字符串中子串的查找方法的总结

来源:互联网 发布:java开源库存管理系统 编辑:程序博客网 时间:2024/06/15 20:45

Java中字符串中子串的查找共有四种方法(indexof())

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()用法说明
indexof()

返回 String 对象内第一次出现子字符串的字符位置。

string.indexOf(subString[, startIndex])

参数 
string

必选项。String 对象或文字。

subString 必选项。

要在 String 对象中查找的子字符串。

starIndex 可选项。

该整数值指出在 String 对象内开始查找的索引。如果省略,则从字符串的开始处查找。

说明 
indexOf 方法返回一个整数值,指出 String 对象内子字符串的开始位置。如果没有找到子字符串,则返回-1。

如果 startindex 是负数,则 startindex 被当作零。如果它比最大的字符位置索引还大,则它被当作最大的可能索引。

从左向右执行查找。否则,该方法与 lastIndexOf 相同。

示例 
下面的示例说明了 indexOf 方法的用法。

function IndexDemo(str2){ 
var str1 = "BABEBIBOBUBABEBIBOBU" 
var s = str1.indexOf(str2); 
return(s); 
}

复制代码
public class FirstDemo {   /**     *API中String的常用方法     */   // 查找指定字符串是否存在   public static void main(String[] args) {     String str1 = "abcdefghijklmnabc";     // 从头开始查找是否存在指定的字符     System.out.println(str1.indexOf("c"));     // 从第四个字符位置开始往后继续查找     System.out.println(str1.indexOf("c", 3));     //若指定字符串中没有该字符则系统返回-1     System.out.println(str1.indexOf("x"));   } 
复制代码



Java中的字符串也是一连串的字符。但是与许多其他的计算机语言将字符串作为字符数组处理不同,Java将字符串作为String类型对象来处理。将字符串作为内置的对象处理允许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() 取得字符串的长度
例:
char chars[]={’a',’b’.’c'};
String s=new String(chars);
int len=s.length();

charAt() 截取一个字符
例:
char ch;
ch=”abc”.charAt(1);
返回值为’b’

getChars() 截取多个字符
void getChars(int sourceStart,int sourceEnd,char target[],int targetStart)
sourceStart 指定了子串开始字符的下标
sourceEnd 指定了子串结束后的下一个字符的下标。因此,子串包含从sourceStart到sourceEnd-1的字符。
target 指定接收字符的数组
targetStart target中开始复制子串的下标值
例:
String s=”this is a demo of the getChars method.”;
char buf[]=new char[20];
s.getChars(10,14,buf,0);

getBytes()
  替代getChars()的一种方法是将字符存储在字节数组中,该方法即getBytes()
例:
String s = “Hello!你好!”;
byte[] bytes = s.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()方法比较字符串对象中的字符,==运算符比较两个对象是否引用同一实例。
例:String s1=”Hello”;
String s2=new String(s1);
s1.eauals(s2); //true
s1==s2;//false

compareTo()和compareToIgnoreCase() 比较字符串

indexOf()和lastIndexOf()
indexOf() 查找字符或者子串第一次出现的地方。
lastIndexOf() 查找字符或者子串是后一次出现的地方。



《---------------------------------------------------------------------------------》


Java 获取字符串中第N次出现的字符位置

public static int getCharacterPosition(String string){
 
   //这里是获取"/"符号的位置
    Matcher slashMatcher = Pattern.compile("/").matcher(string);
    int mIdx = 0;
    while(slashMatcher.find()) {
       mIdx++;
       //当"/"符号第三次出现的位置
       if(mIdx == 3){
          break;
       }
    }
    return slashMatcher.start();
 }



《----------------------------------------------------------------------------》


java中用substr()截取到指定字符结束的字符串 

lotn=627937693726723-67237;
lotn=lotn.substring(0,lotn.lastIndexOf("-"));







0 0