indexOf(String.indexOf 方法)

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, @"1"))
{
     Response.Write("存在");
}
else
{
     Response.Write("不存在");
}
复制代码

凝视:
 在正则中匹配一个单词边界
写了一个方法

复制代码
//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, @"" + tar + @""))
         return true;
     return false;
}
复制代码

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

 

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

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

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

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

[java] view plaincopy
  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方法有三个參数,第一个參数指定被替换子串的起始位置,第二个參数指定被替换子串的终止位置,第三个參数指定新子串
原文地址:https://www.cnblogs.com/gcczhongduan/p/4295429.html