带条件阅读文字

时间:2019-06-05 06:45:45

标签: c# conditional-statements

sample1.txt

ID名称年龄
    1阿米拉31
    2贾亚28
    3萨汉24
    4 Ishara 21

这是我的sample1.text文件,具有3列。我想读取文件,只写年龄大于25岁的人。

请帮忙,以最好的方式做到这一点。 谢谢

2 个答案:

答案 0 :(得分:0)

使用string.Split方法将元素添加到数组或列表中。

对于选项卡,它看起来像<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Welcome!</title> <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.8.1/css/all.css" integrity="sha384-50oBUHEmvpQ+1lW4y57PTFmhCaXp0ML5d60M1M7uH2+nqUivzIebhndOJK28anvf" crossorigin="anonymous"> <link rel="stylesheet" href="/css/bootstrap.min.css"> <link rel="stylesheet" href="/css/app.css"> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fullcalendar/core@4.1.0/main.min.css"> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fullcalendar/daygrid@4.1.0/main.min.css"> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fullcalendar/timegrid@4.1.0/main.min.css"> <script src="/js/jquery.min.js"></script> </head> <body> <nav class="navbar navbar-expand-lg bg-"> <a class="navbar-brand" href="/">Share</a> <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarColor01" aria-controls="navbarColor01" aria-expanded="false" aria-label="Toggle navigation"> <span class="navbar-toggler-icon"></span> </button> <div class="collapse navbar-collapse" id="navbarColor01"> <ul class="navbar-nav mr-auto"> <li class="nav-item active"> <a class="nav-link" href="/account/dashboard"><i class="fas fa-chart-line fa-2x"></i> Tableau de bord</a> </li> <li class="nav-item "> <a class="nav-link" href="/dashboard/showMines"><i class="fas fa-hands-helping fa-2x"></i> offres d'atelier <span class="badge-pill badge-danger dash">3</span> </a> </li> <li class="nav-item "> <a class="nav-link" href="/dashbord/books"><i class="fas fa-graduation-cap fa-2x "></i> cours pris<span class="badge-pill badge-danger dash">2</span></a> </li> <li class="nav-item "> <a class="nav-link" href="/dashboard/courses"><i class="fas fa-chalkboard-teacher fa-2x"></i> cours donnés</a> </li> <li class="nav-item "> <a class="nav-link" href="/dashboard/calendar"><i class="fas fa-calendar-alt fa-2x"></i> mon agenda</a> </li> </ul> <ul class="navbar-nav ml-auto align-items-center"> <li class="nev-item"> <a href="/" class="nav-link" ><i class="fas fa-home fa-2x"></i></i> Retour à l'accueil</a> </li> <li class="nav-item dropdown"> <a href="#" class="nav-link dropdown-toggle" id="accountDropdownlink" data-toggle="dropdown"> <img src="https://randomuser.me/api/portraits/women/56.jpg" alt="Avatar de Cécile" class="avatar avatar-small" class="mr-2"><span class="ml-2 mt-5">Cécile</span> </a> <div class="dropdown-menu dropdown-menu-right" aria-labelledby="accountDropdownlink"> <a class="dropdown-item" href="/activity/new">Créer une activité</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/account">Mon compte</a> <a class="dropdown-item" href="/account/edit">Modifier mon profil</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/logout">Déconnexion</a> </div> </li> </ul> </div> </nav></div> <div class="container mt-5"> <div id="calendar-holder"></div> </div> <footer class="bg-warning text-center py-3 mt-3"> </footer> <script src="/js/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script> <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script> <script> document.addEventListener('DOMContentLoaded', () => { var calendarEl = document.getElementById('calendar-holder'); var calendar = new FullCalendar.Calendar(calendarEl, { //locale: 'fr', defaultView: 'dayGridMonth', editable: true, eventSources: [ { url: "/fc-load-events", method: "POST", extraParams: { // filters: JSON.stringify({}) }, failure: () => { alert("There was an error while fetching FullCalendar!"); }, }, ], header: { locale:'fr', left: 'prev,next today', center: 'title', right: 'dayGridMonth,timeGridWeek,timeGridDay', }, plugins: [ 'interaction', 'dayGrid', 'timeGrid' ], // https://fullcalendar.io/docs/plugin-index timeZone: 'Europe/Paris', }); calendar.setOption('locale', 'fr'); calendar.render(); }); </script> <script src="https://cdn.jsdelivr.net/npm/@fullcalendar/core@4.1.0/main.min.js"></script> <script src="https://cdn.jsdelivr.net/npm/@fullcalendar/interaction@4.1.0/main.min.js"></script> <script src="https://cdn.jsdelivr.net/npm/@fullcalendar/daygrid@4.1.0/main.min.js"></script> <script src="https://cdn.jsdelivr.net/npm/@fullcalendar/timegrid@4.1.0/main.min.js"></script> ,对于逗号,yourString.Split("\t")

有关此处的拆分的更多信息:https://docs.microsoft.com/en-us/dotnet/api/system.string.split?view=netframework-4.8

答案 1 :(得分:0)

我该如何解决

  • 使用File.ReadAllLines(@"FilePath")从示例文件中读取行并将其存储在列表中。
  • 创建一个StreamWriter,将所需的值写入输出文件
  • 如果要在输出文件中包含标题,请写入标题,然后将其从列表中删除。
  • 有一个循环,该循环将用line.split('\t')分割行,检查该行的年龄,并使用output.writeline("Stuff to write")将该行写成符合条件的行

查看进度后

using(var writer=new StreamWriter(WritePath))移到string s ="";上方,因为您只写原始文件的最后一行。

基本上,您正在读取的每一行都覆盖了文件,而不是附加文件。

在while循环中,您可以检查年龄,然后以所需的格式编写所需的行。