indexOf(String.indexOf 方法)

34 篇文章 0 订阅

indexOf(String.indexOf 方法)

字符串的IndexOf()方法搜索在该字符串上是否出现了作为参数传递的字符串,如果找到字符串,则返回字符的起始位置 (0表示第一个字符,1表示第二个字符依此类推)如果说没有找到则返回 -1 
返回 String 对象内第一次出现子字符串的字符位置。
[code=csharp]public indexOf(value:String, [startIndex:Number]) : Number[/code]
搜索字符串,并返回在调用字符串内 startIndex 位置上或之后找到的 value 的第一个匹配项的位置。此索引从零开始,这意味着字符串中的第一个字符被视为位于索引 0 而不是索引 1 处。如果未找到 value,该方法会返回 -1。
参数value:String - 一个字符串;要搜索的子字符串。
startIndex:Number [可选] - 一个整数,指定搜索的开始索引。
返回Number - 指定子字符串的第一个匹配项的位置,或 -1。
--------------------------------------------------------------------------------------------------------------------------------------------------
indexOf 方法 
返回 String 对象内第一次出现子字符串的字符位置。

strObj.indexOf(subString[, startIndex])

参数 
strObj 
必选项。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);
}

实例:
我获得一个字符串a为 "1,18,33" 
如果写成 a indexOf("1") 好象查不出来 更重要的是 18和1前面都有个1所以成立的条件不准确 请问应该怎么写啊

indexOf这样用

复制代码
string test = "1,18,33";
if (test.IndexOf("1") > -1)
{
     Response.Write("存在");
}
else
{
     Response.Write("不存在");
}
复制代码

但是如果说只有1符合要求,而18中的1不符合要求,那不能用IndexOf来做,这样

复制代码
using System.Text.RegularExpressions;
string test = "1,18,33";
if (Regex .IsMatch(test, @"\b1\b"))
{
     Response.Write("存在");
}
else
{
     Response.Write("不存在");
}
复制代码

注释:
\b 在正则中匹配一个单词边界
写了一个方法

复制代码
//src 源字符串
//tar 待比较字符串
private bool CheckString(string src, string tar)
{
     string temp = Regex.Replace(tar, @"[.$^{\[(|)*+?\\]", "");
     if (temp.Length < tar.Length)
         return false;
     if (Regex.IsMatch(src, @"\b" + tar + @"\b"))
         return true;
     return false;
}
复制代码

String是不变类,用String修改字符串会新建一个String对象,如果频繁的修改,将会产生很多的String对象,开销很大.因此java提供了一个StringBuffer类,这个类在修改字符串方面的效率比String高了很多。

 

在java中有3个类来负责字符的操作。

  1.Character 是进行单个字符操作的,

  2.String 对一串字符进行操作。不可变类。

  3.StringBuffer 也是对一串字符进行操作,但是可变类。

[java]  view plain copy
  1. public class UsingStringBuffer {  
  2.     /** 
  3.      * 查找匹配字符串 
  4.      */  
  5.     public static void testFindStr() {  
  6.         StringBuffer sb = new StringBuffer();  
  7.         sb.append("This is a StringBuffer");  
  8.         // 返回子字符串在字符串中最先出现的位置,如果不存在,返回负数  
  9.         System.out.println("sb.indexOf(\"is\")=" + sb.indexOf("is"));  
  10.         // 给indexOf方法设置参数,指定匹配的起始位置  
  11.         System.out.println("sb.indexOf(\"is\")=" + sb.indexOf("is"3));  
  12.         // 返回子字符串在字符串中最后出现的位置,如果不存在,返回负数  
  13.         System.out.println("sb.lastIndexOf(\"is\") = " + sb.lastIndexOf("is"));  
  14.         // 给lastIndexOf方法设置参数,指定匹配的结束位置  
  15.         System.out.println("sb.lastIndexOf(\"is\", 1) = "  
  16.                 + sb.lastIndexOf("is"1));  
  17.     }  
  18.   
  19.     /** 
  20.      * 截取字符串 
  21.      */  
  22.     public static void testSubStr() {  
  23.         StringBuffer sb = new StringBuffer();  
  24.         sb.append("This is a StringBuffer");  
  25.         // 默认的终止位置为字符串的末尾  
  26.         System.out.print("sb.substring(4)=" + sb.substring(4));  
  27.         // substring方法截取字符串,可以指定截取的起始位置和终止位置  
  28.         System.out.print("sb.substring(4,9)=" + sb.substring(49));  
  29.     }  
  30.   
  31.     /** 
  32.      * 获取字符串中某个位置的字符 
  33.      */  
  34.     public static void testCharAtStr() {  
  35.         StringBuffer sb = new StringBuffer("This is a StringBuffer");  
  36.         System.out.println(sb.charAt(sb.length() - 1));  
  37.     }  
  38.   
  39.     /** 
  40.      * 添加各种类型的数据到字符串的尾部 
  41.      */  
  42.     public static void testAppend() {  
  43.         StringBuffer sb = new StringBuffer("This is a StringBuffer!");  
  44.         sb.append(1.23f);  
  45.         System.out.println(sb.toString());  
  46.     }  
  47.   
  48.     /** 
  49.      * 删除字符串中的数据 
  50.      */  
  51.     public static void testDelete() {  
  52.         StringBuffer sb = new StringBuffer("This is a StringBuffer!");  
  53.         sb.delete(05);  
  54.         sb.deleteCharAt(sb.length() - 1);  
  55.         System.out.println(sb.toString());  
  56.     }  
  57.   
  58.     /** 
  59.      * 向字符串中插入各种类型的数据 
  60.      */  
  61.     public static void testInsert() {  
  62.         StringBuffer sb = new StringBuffer("This is a StringBuffer!");  
  63.         // 能够在指定位置插入字符、字符数组、字符串以及各种数字和布尔值  
  64.         sb.insert(2'W');  
  65.         sb.insert(3new char[] { 'A''B''C' });  
  66.         sb.insert(8"abc");  
  67.         sb.insert(23);  
  68.         sb.insert(32.3f);  
  69.         sb.insert(63.75d);  
  70.         sb.insert(5, 9843L);  
  71.         sb.insert(2true);  
  72.         System.out.println("testInsert: " + sb.toString());  
  73.     }  
  74.   
  75.     /** 
  76.      * 替换字符串中的某些字符 
  77.      */  
  78.     public static void testReplace() {  
  79.         StringBuffer sb = new StringBuffer("This is a StringBuffer!");  
  80.         // 将字符串中某段字符替换成另一个字符串  
  81.         sb.replace(10, sb.length(), "Integer");  
  82.         System.out.println("testReplace: " + sb.toString());  
  83.     }  
  84.   
  85.     /** 
  86.      * 将字符串倒序 
  87.      */  
  88.     public static void reverseStr() {  
  89.         StringBuffer sb = new StringBuffer("This is a StringBuffer!");  
  90.         System.out.println(sb.reverse()); // reverse方法将字符串倒序  
  91.     }  
  92. }  
小结: 
StringBuffer不是不变类,在修改字符串内容时,不会创建新的对象,因此,它比String类更适合修改字符串。 
StringBuffer类没有提供同String一样的toCharArray方法 
StringBuffer类的replace方法同String类的replace方法不同,它的replace方法有三个参数,第一个参数指定被替换子串的起始位置,第二个参数指定被替换子串的终止位置,第三个参数指定新子串
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值