C#控制台基础 找到一个字符串中一个字符的所有索引值

来源:互联网 发布:泉州会计网络继续教育 编辑:程序博客网 时间:2024/05/01 21:50
镇场诗:慈心积善融学习,技术誓为大家学。善心速造多好事,前人栽树后乘凉。我今于此写经验,愿见文者得启发。
——————————————————————————————————————————————————————————


using System;using System.Collections.Generic;using System.Diagnostics;using System.Linq;using System.Text;using System.Threading.Tasks;namespace ConsoleApplication7{    class Program    {        static void Main(string[] args)        {            var sw = new Stopwatch();            sw.Start();            string str = "1aqw5a7a9a";            //a的索引值是1,5,7,9            //建立一个存储a的索引值的数组            int[] index = new int[str.Length];            int j = 0;            index[j++] = str.IndexOf('a');            for (int i = str.IndexOf('a')+1; i <= str.LastIndexOf('a'); i++)            {                                i = str.IndexOf('a', i);                index[j++] = i;            }                 sw.Stop();            Console.WriteLine("该方法用时:"+sw.Elapsed);            foreach (var item in index)            {                Console.WriteLine(item);            }            Console.ReadKey();        }    }}






然后在网络上,有个算法,网址:http://zhidao.baidu.com/link?url=Zyw54OTIS8FH4tOUDG8VUSC2SiqkL7b1unCp_8wVHcBr6hGGRpw7YjYksg2JCfsZzX9wzHplWwLlMC-d22qcya

using System;using System.Collections;using System.Collections.Generic;using System.Diagnostics;using System.Linq;using System.Text;using System.Threading.Tasks;namespace ConsoleApplication7{    class Program    {        static void Main(string[] args)        {            var sw = new Stopwatch();            sw.Start();            ArrayList lt = new System.Collections.ArrayList();            string str = "sfskjfskfakfjaga";            int index = 0;            foreach (Char ch in str)            {                if (ch == 's')                {                    lt.Add(index);                }                index++;            }            sw.Stop();            Console.WriteLine("该方法用时:"+sw.Elapsed);            Console.ReadKey();        }    }}




呐,看到了木,嘿嘿。算法的不同,处理的速度不同。


参考了网络上的方法,我自己更改了代码,

using System;using System.Collections;using System.Collections.Generic;using System.Diagnostics;using System.Linq;using System.Text;using System.Threading.Tasks;namespace ConsoleApplication7{    class Program    {        static void Main(string[] args)        {            var sw = new Stopwatch();            sw.Start();            string str = "1aqw5a7a9a";            //a的索引值是1,5,7,9            var index = new ArrayList();            //建立一个存储a的索引值的链表            index.Add( str.IndexOf('a'));            for (int i = str.IndexOf('a') + 1; i <= str.LastIndexOf('a'); i++)            {                i = str.IndexOf('a', i);                index.Add(i);            }            sw.Stop();            Console.WriteLine("该方法用时:"+sw.Elapsed);            foreach (var item in index)            {                Console.WriteLine(item);            }            Console.ReadKey();        }    }}



使用链表更加优秀!

果然呀,出来写代码,必须见多识广。


——————————————————————————————————————————————————————————
感恩帮助过我的人。博客的精髓在技术部分,更在镇场一诗。
我是一个新手,代码还有许多不完善的地方,请您看代码的时候多多思考。
C#是一个优秀的语言,VS是一个优秀的编译软件,二者值得学习。如果您有一些不会的知识,咱们可以相互讨论。
如果您认为代码可以有改进的地方,有错误的地方,请留下评论,我会处理的。
注:如果我的博文无意中侵犯了您的权益,请告知。看到您的告知后,我将及时作出处理。
0 0
原创粉丝点击