热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

过滤并将字符串加载到列表框中-Filterandloadstringtoalistbox

Ihaveatxtfilecontains:我有一个txt文件包含:##testthisisatest127.0.0.1test69.com127.0.0.1

I have a txt file contains:

我有一个txt文件包含:

#
#test
this is a test
127.0.0.1    test69.com
127.0.0.1    http://test69.com
127.0.0.1    www.test69.com
127.0.0.1    man.test
127.0.0.1    http://man.test
127.0.0.1    www.man.test
127.0.0.1    www.another.test

How can I load all the lines in to a listbox but just keep only 3 lines in listbox like :

如何将所有行加载到列表框中,但只在列表框中保留3行,如:

test69.com
man.test
another.test

Example: My code works well but I can't avoid that my listbox loaded www.test69.com and http://test69.com but I don't want them, I just want something like test69.com appears

示例:我的代码运行良好,但我无法避免我的列表框加载www.test69.com和http://test69.com但我不想要它们,我只想要像test69.com这样的东西出现

string str = "";
        StreamReader sr = new StreamReader("C:\\Users\\GhoSter\\Desktop\\test.txt");
        while ((str = sr.ReadLine()) != null)
        {
            if (Check(str) == true)
            {
                string localip = str.Substring(0,9);
                string load = str.Replace(localip + "   ", "");
                myList.Add(load);
            }
        }
        this.listBox1.DataSource = myList;
    }

    bool Check(string s)
    {
        bool flag = false;
        if (s == "" || s == null)
            flag = false;
        else
        {
            string first = s.Substring(0, 1);
            switch (first)
            {
                case "0":
                case "1":
                case "2":
                case "3":
                case "4":
                case "5":
                case "6":
                case "7":
                case "8":
                case "9":
                    flag = true;
                    break;
                default:
                    flag = false;
                    break;
            }
        }
        return flag;
    }

4 个解决方案

#1


1  

I guess you want distinct names without "www" & ip addres from all lines in a text file

我想你想要文本文件中所有行没有“www”&ip addres的不同名称

note :- if wrong please comment about requirement still clear.This is just to filter the string.Rest you do for the Listbox as per your logic.

注意: - 如果有错,请评论有关要求仍然清楚。这只是为了过滤字符串。按照你的逻辑对列表框做的。

Try this code

试试这个代码

 string[] lines = File.ReadAllLines(AppDomain.CurrentDomain.BaseDirectory + @"\fil.txt");

        int ctr = 0;

        foreach (var item in lines)
        {
            string tmp = item;

            tmp = tmp.Replace("127.0.0.1", "");
            tmp = tmp.Replace("http://", "");
            tmp = tmp.Replace("www.", "");
            tmp = tmp.Trim();

            if (ctr 

#2


2  

You could use the Uri class to parse and validate it and to remove the protocol. Then use Enumerable.Distinct to remove duplicates:

您可以使用Uri类来解析和验证它并删除协议。然后使用Enumerable.Distinct删除重复项:

var listOfDistinctUrls = File.ReadLines(@"PathToTextFile");
    .Where(l => !string.IsNullOrWhiteSpace(l))
    .Select(l => {
        string token = l.Split().Last();
        Uri uri;
        if (Uri.TryCreate(token, UriKind.RelativeOrAbsolute, out uri))
        {
            string fileName = Path.GetFileName(uri.IsAbsoluteUri ? uri.Host + uri.PathAndQuery : uri.ToString());
            int wwwIndex = fileName.IndexOf("www.", 0, StringComparison.OrdinalIgnoreCase);
            return wwwIndex >= 0 ? fileName.Substring(wwwIndex + 4) : fileName;
        }
        else
            return null;
    })
    .Where(u => !string.IsNullOrWhiteSpace(u))
    .Distinct()
    .ToList();

Result:

test69.com
man.test

#3


1  

here how you can do this

在这里你如何做到这一点

string[] lines =File.ReadAllLines(yourpathFile);  
//in your listbox 
listBox1.Items.AddRange(lines.Take(6).ToArray()); 

#4


1  

Try this:

List arr = new List();
StreamReader r = File.OpenText( Server.MapPath("b.txt"));
while (!r.EndOfStream)
{
   Match match = Regex.Match(r.ReadLine(), @"(www.|http://)([\w.]+)$");
   if (match.Success)
   {
     arr.Add(Regex.Replace(match.Value, @"(http://|www.)", ""));
   }
}
ListBox1.DataSource = arr.Distinct();
ListBox1.DataBind();

Output:

enter image description here


推荐阅读
  • 本文分享了一个关于在C#中使用异步代码的问题,作者在控制台中运行时代码正常工作,但在Windows窗体中却无法正常工作。作者尝试搜索局域网上的主机,但在窗体中计数器没有减少。文章提供了相关的代码和解决思路。 ... [详细]
  • C# 7.0 新特性:基于Tuple的“多”返回值方法
    本文介绍了C# 7.0中基于Tuple的“多”返回值方法的使用。通过对C# 6.0及更早版本的做法进行回顾,提出了问题:如何使一个方法可返回多个返回值。然后详细介绍了C# 7.0中使用Tuple的写法,并给出了示例代码。最后,总结了该新特性的优点。 ... [详细]
  • Go Cobra命令行工具入门教程
    本文介绍了Go语言实现的命令行工具Cobra的基本概念、安装方法和入门实践。Cobra被广泛应用于各种项目中,如Kubernetes、Hugo和Github CLI等。通过使用Cobra,我们可以快速创建命令行工具,适用于写测试脚本和各种服务的Admin CLI。文章还通过一个简单的demo演示了Cobra的使用方法。 ... [详细]
  • 使用圣杯布局模式实现网站首页的内容布局
    本文介绍了使用圣杯布局模式实现网站首页的内容布局的方法,包括HTML部分代码和实例。同时还提供了公司新闻、最新产品、关于我们、联系我们等页面的布局示例。商品展示区包括了车里子和农家生态土鸡蛋等产品的价格信息。 ... [详细]
  • 本文详细介绍了使用C#实现Word模版打印的方案。包括添加COM引用、新建Word操作类、开启Word进程、加载模版文件等步骤。通过该方案可以实现C#对Word文档的打印功能。 ... [详细]
  • 利用空间换时间减少时间复杂度以及以C语言字符串处理为例减少空间复杂度
    在处理字符串的过程当中,通常情况下都会逐个遍历整个字符串数组,在多个字符串的处理中,处理不同,时间复杂度不同,这里通过利用空间换时间等不同方法,以字符串处理为例来讨论几种情况:1: ... [详细]
  • 正则表达式及其范例
    为什么80%的码农都做不了架构师?一、前言部分控制台输入的字符串,编译成java字符串之后才送进内存,比如控制台打\, ... [详细]
  • 巧用arguments在Javascript的函数中有个名为arguments的类数组对象。它看起来是那么的诡异而且名不经传,但众多的Javascript库都使用着它强大的功能。所 ... [详细]
  • 8.2location对象location对象既是window对象的属性,也是document对象的属性.window.location和document.location引用的是同一个对象. ... [详细]
  • 本文介绍了一个Java猜拳小游戏的代码,通过使用Scanner类获取用户输入的拳的数字,并随机生成计算机的拳,然后判断胜负。该游戏可以选择剪刀、石头、布三种拳,通过比较两者的拳来决定胜负。 ... [详细]
  • 本文介绍了在iOS开发中使用UITextField实现字符限制的方法,包括利用代理方法和使用BNTextField-Limit库的实现策略。通过这些方法,开发者可以方便地限制UITextField的字符个数和输入规则。 ... [详细]
  • IOS开发之短信发送与拨打电话的方法详解
    本文详细介绍了在IOS开发中实现短信发送和拨打电话的两种方式,一种是使用系统底层发送,虽然无法自定义短信内容和返回原应用,但是简单方便;另一种是使用第三方框架发送,需要导入MessageUI头文件,并遵守MFMessageComposeViewControllerDelegate协议,可以实现自定义短信内容和返回原应用的功能。 ... [详细]
  • Ihaveaworkfolderdirectory.我有一个工作文件夹目录。holderDir.glob(*)>holder[ProjectOne, ... [详细]
  • 本文介绍了使用readlink命令获取文件的完整路径的简单方法,并提供了一个示例命令来打印文件的完整路径。共有28种解决方案可供选择。 ... [详细]
  • 题目描述:一个DNA序列由ACGT四个字母的排列组合组成。G和C的比例(定义为GC-Ratio)是序列中G和C两个字母的总的出现次数除以总的字母数目(也就是序列长度)。在基因工程中,这个 ... [详细]
author-avatar
素材火
优质网页素材http://www.sucaihuo.com/
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有