How to delete BOM from a string? - MSDN

文章推薦指數: 80 %
投票人數:10人

Microsoft Logo. Gray Pipe ... How I can delete the BOM? Picture 1 and 2. bom1 bom 2 ... Remove(0, BOMMarkUtf8.Length); return p. We’resorry.Thecontentyourequestedhasbeenremoved.You’llbeautoredirectedin1second. ThisforumhasmigratedtoMicrosoftQ&A.VisitMicrosoftQ&Atopostnewquestions. LearnMore Askaquestion Quickaccess Forumshome Browseforumsusers FAQ Searchrelatedthreads RemoveFromMyForums Answeredby: HowtodeleteBOMfromastring? ArchivedForums421-440  >  VisualC# Question 0 Signintovote Hello, HowIcandeletetheBOM? Picture1and2         Thanksfortips. GreetingsMarkus privatestaticstringRemoveBom(stringp) { stringBOMMarkUtf8=Encoding.UTF8.GetString(Encoding.UTF8.GetPreamble()); if(p.StartsWith(BOMMarkUtf8,StringComparison.OrdinalIgnoreCase)) p=p.Remove(0,BOMMarkUtf8.Length); returnp.Replace("\0",""); } privateasyncvoidFileReadOrTimeout(objectsender,RoutedEventArgse) { try { varret=awaitReadDataAsync(@"C:\Users\Freitag\Documents\TEST_M_3.xml",2000); stringconverted=Encoding.UTF8.GetString(ret,0,ret.Length); converted=RemoveBom(converted); MessageBox.Show(converted+"\n\nEnd"); } } privateasyncTaskReadDataAsync(stringpDatei,intpTimeOutMs) { intn=0; intm=(int)Math.Ceiling(pTimeOutMs/100.0); while(!File.Exists(pDatei)) { awaitTask.Delay(100); n++; if(n==m) thrownewFileNotFoundException(); } FileInfofi=newFileInfo(pDatei); StreamstreamXML; byte[]data=newbyte[fi.Length]; using(varfs=fi.OpenRead()) { intread=awaitfs.ReadAsync(data,0,data.Length); streamXML=newMemoryStream(data); vargpx=Deserialize(streamXML); returndata; } } //###################################################### Monday,December16,20195:54PM Answers 1 Signintovote Togetthestring,readerorstream,dependingonfurtherintentions:      stringtext= awaitFile.ReadAllTextAsync( @"SomeUtf8FileWithBOM.xml");      StreamReaderreader=File.OpenText(@"SomeUtf8FileWithBOM.xml");      FileStreamstream=File.OpenRead(@"SomeUtf8FileWithBOM.xml" ); Markedasanswerby MarkusFreitag Tuesday,December24,20194:09PM Tuesday,December17,20197:55AM Allreplies 0 Signintovote I'mguessingyougotSeethefollowingRemoveBomfromhere. https://stackoverflow.com/questions/1317700/strip-byte-order-mark-from-string-in-c-sharp Youmightwanttoreadthroughtheselectedanswerandunderlyingcommentstotheanswer.Pleaseremembertomarktherepliesasanswersiftheyhelpandunmarkedthemiftheyprovidenohelp,thiswillhelpotherswhoarelookingforsolutionstothesameorsimilarproblem.ContactviamyTwitter(KarenPayne)orFacebook(KarenPayne)via myMSDNprofilebutwillnotanswercodingquestiononeither. NuGetBaseConnectionLibraryfordatabaseconnections. StackOverFlow Monday,December16,20198:12PM 1 Signintovote ConsiderafunctionthatcandetectandstripBOM.Forexample,use StreamReader.ReadToEndAsync.YoucanevenspecifytheencodingandBOMdetectionintheconstructor.Orsimplyuse File.OpenTexttocreatethereader.   Ifpossible,designtheDeserialisefunctiontodealdirectlywithfilestreamsorreaders.Thenintermediatebytesandstringsarenotneeded. Tuesday,December17,20196:46AM 0 Signintovote HiViorel, Canyoumakeawholesample. Thanksinadvance. GreetingsMarkus Editedby MarkusFreitag Tuesday,December17,20197:16AM Tuesday,December17,20197:09AM 1 Signintovote Togetthestring,readerorstream,dependingonfurtherintentions:      stringtext= awaitFile.ReadAllTextAsync( @"SomeUtf8FileWithBOM.xml");      StreamReaderreader=File.OpenText(@"SomeUtf8FileWithBOM.xml");      FileStreamstream=File.OpenRead(@"SomeUtf8FileWithBOM.xml" ); Markedasanswerby MarkusFreitag Tuesday,December24,20194:09PM Tuesday,December17,20197:55AM



請為這篇文章評分?