將開發(fā)過程中常用的內(nèi)容記錄起來,下面的資料是關(guān)于C#索引屬性使用范例的代碼。
創(chuàng)新互聯(lián)建站主要從事成都網(wǎng)站設(shè)計、網(wǎng)站建設(shè)、外貿(mào)網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計、企業(yè)做網(wǎng)站、公司建網(wǎng)站等業(yè)務(wù)。立足成都服務(wù)洪洞,十余年網(wǎng)站建設(shè)經(jīng)驗,價格優(yōu)惠、服務(wù)專業(yè),歡迎來電咨詢建站服務(wù):18982081108using System;
public class Document
{
public class WordCollection
{
internal WordCollection(Document d)
{
document = d;
}
private bool GetWord(char[] text, int begin, int wordCount,
out int start, out int length)
{
int end = text.Length;
int count = 0;
int inWord = -1;
start = length = 0;
for (int i = begin; i <= end; ++i)
{
bool isLetter = i < end && Char.IsLetterOrDigit(text[i]);
if (inWord >= 0)
{
if (!isLetter)
{
if (count++ == wordCount)
{
start = inWord;
length = i - inWord;
return true;
}
inWord = -1;
}
}
else
{
if (isLetter)
inWord = i;
}
}
return false;
}
public string this[int index]
{
get
{
int start, length;
if (GetWord(document.TextArray, 0, index, out start,
out length))
return new string(document.TextArray, start, length);
else
throw new IndexOutOfRangeException();
}
set
{
int start, length;
if (GetWord(document.TextArray, 0, index, out start,
out length))
{
if (length == value.Length)
{
Array.Copy(value.ToCharArray(), 0,
document.TextArray, start, length);
}
else
{
char[] newText =
new char[document.TextArray.Length +
value.Length - length];
Array.Copy(document.TextArray, 0, newText,
0, start);
Array.Copy(value.ToCharArray(), 0, newText,
start, value.Length);
Array.Copy(document.TextArray, start + length,
newText, start + value.Length,
document.TextArray.Length - start
- length);
document.TextArray = newText;
}
}
else
throw new IndexOutOfRangeException();
}
}
public int Count
{
get
{
int count = 0, start = 0, length = 0;
while (GetWord(document.TextArray, start + length, 0,
out start, out length))
++count;
return count;
}
}
}
public class CharacterCollection
{
internal CharacterCollection(Document d)
{
document = d;
}
public char this[int index]
{
get
{
return document.TextArray[index];
}
set
{
document.TextArray[index] = value;
}
}
public int Count
{
get
{
return document.TextArray.Length;
}
}
}
public WordCollection Words;
public CharacterCollection Characters;
public Document(string initialText)
{
TextArray = initialText.ToCharArray();
Words = new WordCollection(this);
Characters = new CharacterCollection(this);
}
public string Text
{
get
{
return new string(TextArray);
}
}
}
class Test
{
static void Main()
{
Document d = new Document(
"peter piper picked a peck of pickled peppers. How many pickled peppers did peter piper pick?"
);
for (int i = 0; i < d.Words.Count; ++i)
{
if (d.Words[i] == "peter")
d.Words[i] = "penelope";
}
for (int i = 0; i < d.Characters.Count; ++i)
{
if (d.Characters[i] == 'p')
d.Characters[i] = 'P';
}
Console.WriteLine(d.Text);
}
}
創(chuàng)新互聯(lián)www.cdcxhl.cn,專業(yè)提供香港、美國云服務(wù)器,動態(tài)BGP最優(yōu)骨干路由自動選擇,持續(xù)穩(wěn)定高效的網(wǎng)絡(luò)助力業(yè)務(wù)部署。公司持有工信部辦法的idc、isp許可證, 機房獨有T級流量清洗系統(tǒng)配攻擊溯源,準確進行流量調(diào)度,確保服務(wù)器高可用性。佳節(jié)活動現(xiàn)已開啟,新人活動云服務(wù)器買多久送多久。