C#

Moderators: None (Apply to moderate this forum)
Number of threads: 2712
Number of posts: 5738

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
search for a word in textfile in c# Posted by joekske on 12 Mar 2003 at 7:03 AM
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;

namespace WindowsApplication1
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.OpenFileDialog openFileDialog1;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.TextBox textBox3;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;

public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

//
// TODO: Add any constructor code after InitializeComponent call
//
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
this.textBox1 = new System.Windows.Forms.TextBox();
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.textBox2 = new System.Windows.Forms.TextBox();
this.textBox3 = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(8, 16);
this.textBox1.Multiline = true;
this.textBox1.Name = "textBox1";
this.textBox1.ScrollBars = System.Windows.Forms.ScrollBars.Both;
this.textBox1.Size = new System.Drawing.Size(696, 496);
this.textBox1.TabIndex = 0;
this.textBox1.Text = "";
//
// button1
//
this.button1.Location = new System.Drawing.Point(736, 40);
this.button1.Name = "button1";
this.button1.TabIndex = 1;
this.button1.Text = "button1";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(736, 72);
this.button2.Name = "button2";
this.button2.TabIndex = 2;
this.button2.Text = "button2";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// textBox2
//
this.textBox2.Location = new System.Drawing.Point(712, 112);
this.textBox2.Name = "textBox2";
this.textBox2.ScrollBars = System.Windows.Forms.ScrollBars.Both;
this.textBox2.Size = new System.Drawing.Size(120, 20);
this.textBox2.TabIndex = 3;
this.textBox2.Text = "";
//
// textBox3
//
this.textBox3.Location = new System.Drawing.Point(720, 144);
this.textBox3.Name = "textBox3";
this.textBox3.TabIndex = 4;
this.textBox3.Text = "";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(840, 605);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.textBox3,
this.textBox2,
this.button2,
this.button1,
this.textBox1});
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);

}
#endregion

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}

private void Form1_Load(object sender, System.EventArgs e)
{


}

private void button1_Click(object sender, System.EventArgs e)
{ textBox1.Clear();
openFileDialog1.ShowDialog();
StreamReader sr = new StreamReader(openFileDialog1.FileName);

String woord;
woord = sr.ReadLine();
while(woord != null)
{
textBox1.Text = textBox1.Text + woord ;
woord = sr.ReadLine();
}
sr.Close();
}

private void button2_Click(object sender, System.EventArgs e)
{
textBox1.Clear();
openFileDialog1.ShowDialog();
StreamReader sr = new StreamReader(openFileDialog1.FileName);

String woord;
int gevonden = 0;
woord = sr.ReadLine();
while(woord != null)
{

textBox1.Text = textBox1.Text + woord ;

for(int i=0;i<woord.Length-1;i++)
{
if(woord.Substring(0,i).Equals(textBox2.Text))
{
gevonden++;
}
}

woord = sr.ReadLine();
}
sr.Close();
textBox3.Text = "" + gevonden;
}
}
}

Report
Re: search for a word in textfile in c# Posted by Brutes on 13 Mar 2003 at 1:11 AM
string is immutable. You cant change the content of a string once you have assigned to it.
C# has provided a work around for this, but it is very memory intensive.
You are also using String instead of string. Take a look in the framework documentation to see the difference between String and string.
Use StringBuilder instead.


Report
Re: search for a word in textfile in c# Posted by joekske on 13 Mar 2003 at 6:03 AM
thank you for your reply, but i have found a way to look for a word in a textfile on my one. If you want i can give you the code.
greets joekske
Report
Re: search for a word in textfile in c# Posted by fairplay2284 on 1 Nov 2010 at 1:27 AM
Hi joekske,

can you share with me your source-code for search for a word in textfile in c#. thanks in advance. My email fairplay2284@gmail.com.



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.