1.找出指定字符的位置
using System;
using System.IO;
using System.Text;
namespace A
{
class program
{
public static void Main()
{
string s="ssdfdsdgfdhgfkdhkjhdkjhsd";
int index=s.IndexOf('s');
Console.WriteLine("第1次出现e的位置是{0}",index);
int count=1;
while(index!=-1)
{
count++;
index=s.IndexOf("s",index+1);
if(index==-1)
{
break;
}
Console.WriteLine("第{0}次出现s的位置是{1}",count,index);
}
Console.ReadLine();
}
}
}
2.输入字符串找出指定字符的位置
using System;
using System.IO;
using System.Text;
namespace A
{
class program
{
public static void Main()
{
string Path = @"C:\Users\ASUS\Desktop\1.txt";
string[] contents = File.ReadAllLines(Path, Encoding.Default);
for (int i = 0; i < contents.Length; i++)
{
String[] strNew = contents[i].Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
Console.WriteLine(strNew[0].Length > 10 ? strNew[0].Substring(0, 8) : strNew[0] +"|" + strNew[1]);
}
Console.ReadLine();
}
}
}