Re: [問題] 自行建立LastIndexOf功能函數

看板C_Sharp (C#)作者時間20年前 (2004/11/26 11:01), 編輯推噓1(100)
留言1則, 1人參與, 最新討論串3/8 (看更多)
※ 引述《Remin.bbs@ptt.cc (Remin)》之銘言: > using System; > class call //輸入字串中的任一字元 輸出該字元最後出現的位元 > { > public static void Main() > { > String str1="abbaaa"; > char[] aa=str1.ToCharArray(); > Console.Write("請輸入指定字母: "); > String str2=Console.ReadLine(); > char bb=char.Parse(str2); > for(int i=0;i<6;i++) > { > if((int)aa[i]==(int)bb) > Console.WriteLine(i); > } > } > } > 最後程式執行,出現 "0 3 4 5" ,但是我只想要"5" > 請問各位先進,該如何修正呢? Thanks^^ net的string本身就有內建LastIndexOf的函數,所以你只要用 string str1 = "abbaaa"; Console.WriteLine(str1.LastIndexOf('a')); 如果您是想自己實做看看的話,可以用底下方法,同您的程式,改掉for for ( int i=aa.Length-1; i>=0; --i ) if ( aa[i] == b ) { Console.WriteLine( i ); break; } 但是如果你的字串很長,而且你要搜的對象又剛好在前面,那這個程式的 效就不是挺好的,建議把實作改成RE,之前有說過RE怎麼用,找找看吧 :) -- ※ Origin: 元智大學 風之塔 <bbs.yzu.edu.tw> ※ From : 220-139-139-157.dynamic.hinet.net ※ X-Info: Re: [問題] 自行建立LastIndexOf功能函數 ※ X-Sign: 10QD6JIm1HKHZE5qW2wI (04/11/26 10:52:34 )

140.109.40.39 11/26, , 1F
非常感謝!! :)
140.109.40.39 11/26, 1F
文章代碼(AID): #11ffnk00 (C_Sharp)
討論串 (同標題文章)
文章代碼(AID): #11ffnk00 (C_Sharp)