if you have an array of 10 items, Ubound
will return 10 and Length
will return 11.
you can use .GetUpperBound(0)
or .Lenght-1
proof
using System;
using Microsoft.VisualBasic;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
var ar = new string[11];
Console.WriteLine(ar.GetUpperBound(0));
Console.WriteLine(ar.Length);
Console.WriteLine(Microsoft.VisualBasic.Information.UBound(ar));
Console.ReadKey();
}
}
}
so with the help of this answer
this is what you would need, I think
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string s = "123412341234";
string Token = "2";
var Values = new string[] {"a","b", "c" };
int i = 0;
int MarkerPos;
do
{
s = ReplaceFirst(s, Token, Values[i]);
MarkerPos = s.IndexOf(Token);
i++;
} while(MarkerPos != -1 && i <= Values.GetUpperBound(0));
Console.WriteLine(s);
Console.ReadKey();
}
static string ReplaceFirst(string text, string search, string replace)
{
int pos = text.IndexOf(search);
if (pos < 0)
{
return text;
}
return text.Substring(0, pos) + replace + text.Substring(pos + search.Length);
}
}
}