0

.txt-file内の文字列をカンマ ( ,)で分割しstring[]、そのすべての項目string[]を別の数式に置き換えようとしています。たとえば、次のようになります。

"Marko Kostic, Faculty of Technical Sciences, University of Novi Sad,
Trg D. Obradovica 6, 21125 Novi Sad, Serbia"

この文字列を単語の間にカンマで分割し、すべての値をリストのように別の行に入れ、すべての値を別の値に変更し"Marko Kostic"たい

<addr-line>Marko Kostic<\addr-line>

問題は、ライターが の最後の値のみstring[]を書き込み、以前の値を消去することです。

助言がありますか?

    using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Text.RegularExpressions;
using Microsoft.Office.Interop;
using Microsoft.Office.Interop.Word;
using System.Diagnostics;
using System.Reflection;
using System.Collections;
using System.Runtime.InteropServices;


namespace AffiliationParser
{
    class Program
    {
        static void Main(string[] args)
        {


            Microsoft.Office.Interop.Word.Application oWord = new Microsoft.Office.Interop.Word.Application();
            object missing = System.Reflection.Missing.Value;
            object isVisible = false;
            using (StreamReader batch = new StreamReader(@"D:\Developing\REF\AffiliationParser\AffiliationParser\AffiliationParser\bin\Debug\Run.bat"))
            {
                string bat;
                while (!batch.EndOfStream)
                {
                    bat = batch.ReadLine();
                    // do your processing with batch command

                    if (bat == "pause")
                    {
                        continue;
                    }
                    string fpath = bat.Substring(bat.IndexOf(" \""));


                    string path = fpath.Replace("\"", "").Replace(" ","");


                    string[] name = Directory.GetFiles(path, "*.txt");

                    string words = name.Min();


                    string word = words.Substring(words.LastIndexOf("\\")).Replace("\\", "");

                    Console.WriteLine("Processing........");
                    Console.WriteLine(word);

                    string Npath = path + @"\Arr" + word;
                    if (File.Exists(Npath))
                    {
                        System.Windows.Forms.MessageBox.Show("The file Arr" + word + " alredy exist in " + path);
                        continue;
                    }

                    else
                    {
                        File.Copy(words, Npath);
                        StreamReader temp = new StreamReader(Npath, Encoding.UTF8);
                        string tempstring = temp.ReadToEnd();
                        string[] temp3 = tempstring.Split(',');

                        temp.Close();

                        foreach (string item in temp3)
                        {
                          string Nitem = item.TrimStart().TrimEnd();





                            //Match MatchCont = Regex.Match(Nitem, @"Afganistan|Albania|Algeria|American\s+Samoa|Andorra|Angola|Anguilla|Antarctica|Antigua\s+and\s+Barbuda|Argentina|Armenia|Aruba|Australia|Austria|Azerbaijan|Bahamas|Bahrain|Bangladesh|Barbados|Belarus|Belgium|Belize|Benin|Bermuda|Bhutan|Bolivia|Bosnia\s+and\s+Herzegovina|Botswana|Bouvet\s+Island|Brazil|British\s+Indian\s+Ocean\s+Territory|Brunei\s+Darussalam|Bulgaria|Burkina\s+Faso|Burundi|Cambodia|Cameroon|Canada|Cape\s+Verde|Cayman\s+Islands|Central\s+African\s+Republic|Chad|Chile|China|Christmas\s+Island|Cocos\s+\(Keeling\)\s+Islands|Colombia|Comoros|Democratic\s+People's\s+Republic\s+of\s+Korea|Democratic\s+Republic\s+of\s+Congo|Cook\s+Islands|Costa\s+Rica|Cote\s+D'Ivoire|Croatia|Cuba|Cyprus|Czech\s+Republic|Republic\s+of\s+Korea|Denmark|Djibouti|Dominica|Dominican\s+Republic|East\s+Timor|Ecuador|Egypt|El\s+Salvador|Equatorial\s+Guinea|Eritrea|Estonia|Ethiopia|Falkland\s+Islands\s+\(Malvinas\)|Faroe\s+Islands|Fiji|Finland|France\s+Metropolitan|France|French\s+Guiana|French\s+Polynesia|French\s+Southern\s+Territories|Gabon|Gambia|Georgia|Germany|Ghana|Gibraltar|Greece|Greenland|Grenadaf|Guadeloupe|Guam|Guatemala|Guinea|Guinea\-Bissau|Guyana|Haiti|Heard\s+Island\s+and\s+McDonald\s+Island|Honduras|Hong\s+Kong|Hungary|Iceland|India|Indonesia|Iran|Iraq|Ireland|Northern\s+Ireland|Isle\s+Of\s+Man|Israel|Italy|Jamaica|Japan|Jordan|Kazakhstan|Kenya|Kiribati|Kuwait|Kyrgyzstan|Lao\s+People'S\s+Democratic\s+Republic|Latvia|Lebanon|Lesotho|Liberia|Libya|Liechtenstein|Lithuania|Luxembourg|Macau|Macedonia|Madagascar|Malawi|Malaysia|Maldives|Mali|Malta|Marshall\s+Islands|Martinique|Mauritania|Mauritius|Mayotte|Mexico|Micronesia|Moldova|Monaco|Mongolia|Montserrat|Morocco|Mozambique|Myanmar|Namibia|Nauru|Nepal|Netherlands\s+Antilles|New\s+Caledonia|New\s+Zealand|Nicaragua|Nigeria|Niger|Niue|Norfolk\s+Island|Northern\s+Mariana\s+Islands|Norway|Oman|Pakistan|Palau|Palestine|Panama|Papua\s+New\s+Guinea|Paraguay|Peru|Philippines|Pitcairn|Poland|Portugal|Puerto\s+Rico|Qatar|Reunion|Romania|Russia|Rwanda|Saint\s+Kitts\s+and\s+Nevis|Saint\s+Lucia|Saint\s+Vincent\s+and\s+The\s+Grenadines|Samoa|San\s+Marino|Sao\s+Tome\s+and\s+Principe|Saudi\s+Arabia|Scotland|Senegal|Serbia|Kosovo|Montenegro|Seychelles|Sierra\s+Leone|Singapore|Slovakia|Slovenia|Solomon\s+Islands|Somalia|South\s+Africa|South\s+Georgia\s+and\s+The\s+South\s+Sandwich\s+Islands|Spain|Sri\s+Lanka|St.\s+Helena|St.\s+Pierre\s+and\s+Miquelon|Sudan|Suriname|Svalbard\s+and\s+Jan\s+Mayen\s+Islands|Swaziland|Sweden|Switzerland|Syria|Taiwan|Tajikistan|Tanzania|Thailand|The\s+Netherlands|Togo|Tokelau|Tonga|Trinidad\s+and\s+Tobago|Tunisia|Turkey|Turkmenistan|Turks\s+and\s+Caicos\s+Islands|Tuvalu|Uganda|Ukraine|United\s+Arab\s+Emirates|UAE|UK|United\s+States\s+Minor\s+Outlying\s+Islands|Uruguay|USA|Uzbekistan|Vanuatu|Vatican\s+City\s+State\s+\(Holy\s+See\)|Venezuela|Vietnam|British\s+Virgin\s+Islands|USA\s+Virgin\s+Islands|Wallis\s+and\s+Futuna\s+Islands|Western\s+Sahara|West\s+Indies|Yemen|Zambia|Zimbabwe|Abkhazia|Afghanistan|Akrotiri\s+and\s+Dhekelia|Aland|Ascension\s+Island|The\s+Bahamas|Brunei|Central\s+Africa|Cocos|Congo|Cote\s+d'lvoire|Czech|Dominican|Falkland\s+Islands|Cambia,\s+The|Grenada|Guemsey|Isle\s+of\s+Man|Jersey|Korea|Laos|Macao|Nagorno\-Karabakh|Netherlands|Northern\s+Cyprus|Pitcaim\s+Islands|Sahrawi\s+Arab\s+Democratic|Saint\-Barthelemy|Saint\s+Helena|Saint\s+Martin|Saint\s+Pierre\s+and\s+Miquelon|Saint\s+Vincent\s+and\s+Grenadines|Samos|Somaliland|South\s+Ossetia|Svalbard|Transnistria|Tristan\s+da\s+Cunha|United\s+Kingdom|Vatican\s+City|Virgin\s+Islands|Wallis\s+and\s+Futuna|Espa�a|Witsch|United\s+States|Prague\s+Czech\s+Republic", RegexOptions.Singleline | RegexOptions.Compiled | RegexOptions.ExplicitCapture | RegexOptions.IgnoreCase);
                            //if (MatchCont.Success==true)
                            //{
                            //    MatchCont.Result(@"<country>" + Nitem + @"<\country>");   
                            //}

                        }
                    }

                }

            }
        }
    }
}
4

2 に答える 2

2

質問にコードを含めるようにしてください。単に回答を配るのはベストプラクティスではありません。そうは言っても、あなたはそのString.Split方法String.Trimとその方法を見たいと思うでしょうFile.AppendText

これを行う簡単な方法:

string[] stuff = data.Split(',');
StreamWriter sW = File.AppendText(pathToFile);

foreach(string parts in stuff)
{       

    sW.WriteLine(parts.Trim());
}

非常に、非常に基本的であり、あなたの側でいくつかの作業なしにあなたに答えを与えません。幸運を!

ここにいくつかの参照があります:File.AppendTextString.Trim

于 2013-03-21T13:36:57.440 に答える
0
string input="a,b,c,d";
string [] parts=input.Split(",",StringSplitOptions.RemoveEmptyEntries);
List<string> output=new List<string>();
foreach(string s in parts)
{
   // do sth you like;
   var newStr="<abc>"+s+"</abc>";
   output.Add(newStr);
}
return output.ToArray();
于 2013-03-21T13:25:40.947 に答える