小编Har*_*ond的帖子

写入文件但文件为空

我正在将输出写入此文件,但它在Organized.txt中显示为空.如果我从sr2.WriteLine更改最后一个foreach循环并且只用WriteLine写入控制台,那么输出会在控制台上正确显示,那么为什么它在文本文件中没有正确显示?

 class Program
    {
        public static void Main()
        {

            string[] arr1 = new string[200];


            System.IO.StreamWriter sr2 = new System.IO.StreamWriter("OrganizedVersion.txt");




                    // Dictionary, key is number from the list and the associated value is the number of times the key is found
                    Dictionary<string, int> occurrences = new Dictionary<string, int>();
                    // Loop test data
                    foreach (string value in File.ReadLines("newWorkSheet.txt"))
                    {
                        if (occurrences.ContainsKey(value)) // Check if we have found this key before
                        {
                            // Key exists. Add number of occurrences for this key by …
Run Code Online (Sandbox Code Playgroud)

c#

2
推荐指数
1
解决办法
1371
查看次数

C#正则表达式匹配行中的精确字符串但忽略大小写

我目前拥有的正则表达式代码将寻找与case完全匹配的代码,因此我必须做出哪些更改才能忽略这种情况?

public static bool ExactMatch(string input, string match)
{
    return Regex.IsMatch(input, string.Format(@"\b{0}\b", Regex.Escape(match)));
}
Run Code Online (Sandbox Code Playgroud)

c# regex

2
推荐指数
2
解决办法
5395
查看次数

计算Array中的出现次数

我正在计算数组中每个元素的出现但我得到错误"值不能为空"这对我没有意义,因为arr1完全填充没有空值,除了最后5个为null的元素.

这是我的代码.我是第一次使用字典,所以我可能在某处有一些逻辑错误.我正在阅读文本文件.

string[] arr1 = new string[200];
StreamReader sr = new StreamReader("newWorkSheet.txt");
string Templine1 = "";
int counter = 0;
while (Templine1 != null)
{
    Templine1 = sr.ReadLine();
    arr1[counter] = Templine1;
    counter += 1;
}
sr.Close();

// Dictionary, key is number from the list and the associated value is the number of times the key is found
Dictionary<string, int> occurrences = new Dictionary<string, int>();
// Loop test data
foreach (string value in arr1)
{
    if (occurrences.ContainsKey(value)) // Check if we …
Run Code Online (Sandbox Code Playgroud)

.net c# dictionary

0
推荐指数
1
解决办法
8057
查看次数

为什么这个文件上没有换行?

我编写了一些代码来比较2个文件并将它们的公共行写入第3个文件.由于某种原因,虽然包含公共线的第3个文件具有在1行上写入的所有公共线.这应该是每个公共行1个新行..我甚至尝试添加Console.WriteLine('\n'); 添加一个新行来分隔公共线,但这没有帮助.关于什么是错的任何想法?

    //This program will read  files and compares to see if they have a line in common
    //if there is a line in common then it writes than common line to a new file
  static void Main(string[] args)
    {

        int counter = 0;
        string line;
        string sline;
        string[] words;
        string[] samacc = new string[280];


        //first file to compare
        System.IO.StreamReader sfile =
           new System.IO.StreamReader("C:\\Desktop\\autoit\\New folder\\userlist.txt");
        while ((sline = sfile.ReadLine()) != null)
        {
            samacc[counter] = sline;
            Console.WriteLine();

            counter++;
        }

        sfile.Close();

        //file …
Run Code Online (Sandbox Code Playgroud)

c# file-io

0
推荐指数
1
解决办法
181
查看次数

RegEx for C#中的名称

我有一个名单:

Jacqueline Beaurivage   (loh Da road); 
Bon Van Daht (fary goal lim)
Bon Jobi (ting wei)
Ting Wan (dehtee road);
Run Code Online (Sandbox Code Playgroud)

如何使用C#字符串搜索方法将名称存储到变量中?

基本上我不想要"("之后的任何内容.什么是最有效和最简单的方法?

c# string

0
推荐指数
1
解决办法
109
查看次数

比较两个名单

我试图在C#中创建一个简短的Windows Presentation Foundation应用程序来比较不同格式的两个列表,并输出它们共有的用户.

现在我通过文本框从用户那里获取每个列表.

现在我对如何比较两个不同的文本框并输出常用名称感到有点困惑.如果数据是某种数组,我可以使用:

var name = string.Split('(')[0].Trim()
Run Code Online (Sandbox Code Playgroud)

但由于我决定使用文本框,我不确定如何继续这个.例如,考虑以下两个输入列表和预期输出:

第一份清单:

Jacqueline Beaurivage (loh Da road); 
Bon Van Daht (fary goal lim)
Bon Jobi (ting wei)
Ting Wan (dehtee road);
Run Code Online (Sandbox Code Playgroud)

第二份清单:

Jacqueline Beaurivage
Bon Van Daht
Run Code Online (Sandbox Code Playgroud)

预期产量:

Jacqueline Beaurivage
Bon Van Daht
Run Code Online (Sandbox Code Playgroud)

c# wpf

0
推荐指数
1
解决办法
169
查看次数

标签 统计

c# ×6

.net ×1

dictionary ×1

file-io ×1

regex ×1

string ×1

wpf ×1