โปรแกรมที่ 1
static void Main()
{
string astring = "Now is the time";
int pos;
string word;
ArrayList words = new ArrayList();
pos = astring.IndexOf(" ");
while (pos > 0)
{
word = astring.Substring(0, pos);
words.Add(word);
astring = astring.Substring(pos + 1, astring.Length - (pos + 1));
pos = astring.IndexOf(" ");
if (pos == -1)
{
word = astring.Substring(0, astring.Length);
words.Add(word);
}
Code (C#)
โปรแกรมที่ 2
static void Main()
{
string astring = "now is the time for all good people ";
ArrayList words = new ArrayList();
words = SplitWords(astring);
foreach (string word in words)
Console.Write(word + " ");
Console.Read();
}
static ArrayList SplitWords(string astring)
{
string[] ws = new string[astring.Length - 1];
ArrayList words = new ArrayList();
int pos;
string word;
pos = astring.IndexOf(" ");
while (pos > 0)
{
word = astring.Substring(0, pos);
words.Add(word);
astring = astring.Substring(pos + 1, astring.Length - (pos + 1));
if (pos == -1)
{
word = astring.Substring(0, astring.Length);
words.Add(word);
}
Code (C#)
โปรแกรมที่ 3
static void Main() {
string data = "Mike,McMillan,3000 W. Scenic,North Little Rock,AR,72118";
string[] sdata;
char[] delimter = new char[] {','};
sdata = data.Split(delimter, data.Length);
foreach (string word in sdata)
Console.Write(word + " ");
string joined;
joined = string.Join(",", sdata);
Console.Write(joined);
}
Code (C#)
โปรแกรมที่ 4
static void Main() {
int size = 100;
Timing timeSB = new Timing();
Timing timeST = new Timing();
Console.WriteLine();
for(int i = 0; i <= 3; i++) {
timeSB.startTime();
BuildSB.(size);
timeSB.stopTime();
timeST.startTime();
BuilString(size);
timeST.stopTime();
Console.WriteLine
("Time (in milliseconds) to build StringBuilder"
+ "object for " & size & " elements : +
timeSB.Result.TotalMilliseconds);
Console.WriteLine
(("Time (in milliseconds) to build String object"
+ " for " & size & " elements : "+
timeSB.Result.TotalMilliseconds);
Console.WriteLine();
size *= 10;
}
}
static void BuildSB(int size) {
StringBuilder sbObject = new StringBuilder();
for(int i = 0; i <= size; i++)
sbObject.Append("a");
}
static void BuildString(int size) {
string stringObject = "";
for(int i = 0; i <= size; i++)
stringObject & = "a";
}
Tag : .NET, C#
Date :
2013-08-23 00:58:45
By :
กวาง
View :
1851
Reply :
2
No. 1
Guest
Code (C#)
โปรแกมที่ 5..
static void Main() {
Regex reg = new Regex("the");
String st1 = "the guick brow fox jumped over the lazy dog";
Match matchSet;
int matchPos;
matchSet = reg.Match(st1);
if (matchSet.Success) {
matchPos = matchSet.Index;
Console.WriteLine("found match at position:" + matchPos);
Console.Read();
}