C#自学笔记_009_Real(字符串函数练习)
using System
;
using System
.Collections
.Generic
;
using System
.IO
;
using System
.Linq
;
using System
.Text
;
using System
.Threading
.Tasks
;
using System
.Net
.Http
.Headers
;
namespace Csharp009stringtraining
{
class Program
{
static void Main(string[] args
)
{
string path
= @"E:\C#learningfile\Csharp009stringtraining\trainingfile.txt";
string[] contents
= File
.ReadAllLines(path
);
for (int i
= 0; i
< contents
.Length
; i
++)
{
string[] strNew
= contents
[i
].Split(new char[] { ' ' }, StringSplitOptions
.RemoveEmptyEntries
);
Console
.WriteLine((strNew
[0].Length
> 8 ? strNew
[0].Substring(0, 8) + "......" : strNew
[0]) + "|" + strNew
[1]);
}
string str
= "abcdefg";
char[] chs
= str
.ToCharArray();
for (int i
= 0; i
< chs
.Length
/ 2; i
++)
{
char temp
;
temp
= chs
[i
];
chs
[i
] = chs
[chs
.Length
- 1 - i
];
chs
[chs
.Length
- 1 - i
] = temp
;
}
str
= new string(chs
);
Console
.WriteLine(str
);
string str
= "Hello c sharp";
string[] strNew
= str
.Split(new char[] { ' ' }, StringSplitOptions
.RemoveEmptyEntries
);
for (int i
= 0; i
< strNew
.Length
/ 2; i
++)
{
string temp
;
temp
= strNew
[i
];
strNew
[i
] = strNew
[strNew
.Length
- 1 - i
];
strNew
[strNew
.Length
- 1 - i
] = temp
;
}
str
= string.Join(" ", strNew
);
Console
.WriteLine(str
);
string email
= "abcedf@outlook.com";
int index
= email
.IndexOf('@');
string userName
= email
.Substring(0, index
);
string yuMing
= email
.Substring(index
+ 1);
Console
.WriteLine(userName
);
Console
.WriteLine(yuMing
);
string[] strNew
= email
.Split(new char[] { '@' }, StringSplitOptions
.RemoveEmptyEntries
);
Console
.WriteLine(strNew
[0]);
Console
.WriteLine(strNew
[1]);
string str
= "abcdefhuigiyehodueenjekwe";
int index
= str
.IndexOf('e');
Console
.WriteLine("第1次出现e的位置是{0}", index
);
int count
= 1;
while (index
!= -1)
{
count
++;
index
= str
.IndexOf('e', index
+ 1);
if (index
== -1)
{
break;
}
Console
.WriteLine("第{0}次出现e的位置是{1}", count
, index
);
}
Console
.ReadKey();
}
}
}
txt文件
转载请注明原文地址: https://lol.8miu.com/read-37764.html