日期:2014-05-17  浏览次数:20698 次

关于字符串索引问题
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace indexer
{
  public class listboxtest

  {
  private int ctr = 0;
  private string[] strings;
   
  public listboxtest(params string[] initialstrings)
  {
  strings = new string[11];
   
  foreach (string s in initialstrings)
  {
  strings[ctr++] = s;
  Console.WriteLine("索引1为:{0}", ctr);
  }
  }
  public void add(string thestring)
  {
  if (ctr >= strings.Length)
  {
  //处理错误索引
  }
  else
  strings[ctr++] = thestring;
  Console.WriteLine("索引为:{0}", ctr);
  }
   
  public string this[int index]
  {

   
  get 
  {
   
  if (index < 0 || index >= strings.Length)
  {
  return strings[6];
  }
  else
  return strings[index];
   
  }
  set
  {
  if (index >= strings.Length)
  {
  //处理错误
  }
  else
  {
  strings[index] = value;
  // if (ctr < index + 1)
  // ctr = index + 1;
  }
  }
  }
  private int findstring(string searchstring)
  {
  int t = 3;
  for (int i = 0; i < strings.Length; i++)
   
  if (strings[i].StartsWith(searchstring))
  return i; 
   
  return t;
  }
  public string this[string index]
  {
  get
  {
  if (index.Length == 0)
  { 
  //处理错误
  }
  return this[findstring(index)];
  }
  set
  {
  if (findstring(index) < 0)
  { 
  //处理错误
  }
  else
  strings[findstring(index)] = value;
  }
  }
  public int getnumentries()
  {
  return ctr;
  }
  }
  class Program
  {
  static void Main(string[] args)
  {
  listboxtest lbt=new listboxtest("hello","world");
  lbt.add("who");
  lbt.add("is");
  lbt.add("douglas");
  lbt.add("adams");
  string subst = "universe";
  lbt[1] = subst;
  lbt["dd"] = "go