Jump to content

  • Log In with Google      Sign In   
  • Create Account

Banner advertising on our site currently available from just $5!


1. Learn about the promo. 2. Sign up for GDNet+. 3. Set up your advert!


Count occurrences in selected text


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
4 replies to this topic

#1 wforl   Members   -  Reputation: 169

Posted 06 August 2009 - 07:13 AM

Does anyone know of any applications that can count an occurance of a letter or word or whatever in highlighted text? Im trying to count the number of times a ',' is occurring in a file, but in only the text i have highlighted. Ive tried it in a few apps, Notepad++, in its Find Dialog, has the option to count, and a checkbox to select for "selected text" only, but for me, still seems to count occurances in the whole file. Context, has no count option UltraEdit, will only count on whole files, when selecting the "selection only", then greys out the count option. Also, another function ive been trying to find, is an editor that allows you to view selected data in any format. So say i have a hex editor app open, and i have a number of selected hex characters highlighted, i can view the selcted data as floats or ints or int64 etc etc Im on Windows BTW

Sponsor:

#2 Sean T. McBeth   Crossbones+   -  Reputation: 1674

Posted 06 August 2009 - 07:53 AM

Here, happy birthday.

using System;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
public class FormFind : Form
{
private TableLayoutPanel tableLayoutPanel1;
private MenuStrip menuStrip1;
private ToolStripMenuItem fileToolStripMenuItem;
private ToolStripMenuItem openToolStripMenuItem;
private ToolStripMenuItem exitToolStripMenuItem;
private TextBox textBox1;
private TextBox textBox2;
private Button button1;
private OpenFileDialog openFileDialog1;
private CheckBox checkBox1;

/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;

/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (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.tableLayoutPanel1 = new System.Windows.Forms.TableLayoutPanel();
this.menuStrip1 = new System.Windows.Forms.MenuStrip();
this.fileToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.openToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.exitToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.textBox1 = new System.Windows.Forms.TextBox();
this.textBox2 = new System.Windows.Forms.TextBox();
this.button1 = new System.Windows.Forms.Button();
this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
this.checkBox1 = new System.Windows.Forms.CheckBox();
this.tableLayoutPanel1.SuspendLayout();
this.menuStrip1.SuspendLayout();
this.SuspendLayout();
//
// tableLayoutPanel1
//
this.tableLayoutPanel1.ColumnCount = 3;
this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 100F));
this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 106F));
this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 59F));
this.tableLayoutPanel1.Controls.Add(this.menuStrip1, 0, 0);
this.tableLayoutPanel1.Controls.Add(this.textBox1, 0, 1);
this.tableLayoutPanel1.Controls.Add(this.textBox2, 0, 2);
this.tableLayoutPanel1.Controls.Add(this.button1, 2, 1);
this.tableLayoutPanel1.Controls.Add(this.checkBox1, 1, 1);
this.tableLayoutPanel1.Dock = System.Windows.Forms.DockStyle.Fill;
this.tableLayoutPanel1.Location = new System.Drawing.Point(0, 0);
this.tableLayoutPanel1.Margin = new System.Windows.Forms.Padding(0);
this.tableLayoutPanel1.Name = "tableLayoutPanel1";
this.tableLayoutPanel1.RowCount = 3;
this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 25F));
this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 29F));
this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 100F));
this.tableLayoutPanel1.Size = new System.Drawing.Size(654, 341);
this.tableLayoutPanel1.TabIndex = 0;
//
// menuStrip1
//
this.tableLayoutPanel1.SetColumnSpan(this.menuStrip1, 3);
this.menuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.fileToolStripMenuItem});
this.menuStrip1.Location = new System.Drawing.Point(0, 0);
this.menuStrip1.Name = "menuStrip1";
this.menuStrip1.Size = new System.Drawing.Size(654, 24);
this.menuStrip1.TabIndex = 0;
this.menuStrip1.Text = "menuStrip1";
//
// fileToolStripMenuItem
//
this.fileToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.openToolStripMenuItem,
this.exitToolStripMenuItem});
this.fileToolStripMenuItem.Name = "fileToolStripMenuItem";
this.fileToolStripMenuItem.Size = new System.Drawing.Size(37, 20);
this.fileToolStripMenuItem.Text = "&FIle";
//
// openToolStripMenuItem
//
this.openToolStripMenuItem.Name = "openToolStripMenuItem";
this.openToolStripMenuItem.Size = new System.Drawing.Size(152, 22);
this.openToolStripMenuItem.Text = "&Open";
this.openToolStripMenuItem.Click += new System.EventHandler(this.openToolStripMenuItem_Click);
//
// exitToolStripMenuItem
//
this.exitToolStripMenuItem.Name = "exitToolStripMenuItem";
this.exitToolStripMenuItem.Size = new System.Drawing.Size(152, 22);
this.exitToolStripMenuItem.Text = "E&xit";
this.exitToolStripMenuItem.Click += new System.EventHandler(this.exitToolStripMenuItem_Click);
//
// textBox1
//
this.textBox1.Dock = System.Windows.Forms.DockStyle.Fill;
this.textBox1.Location = new System.Drawing.Point(3, 28);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(483, 20);
this.textBox1.TabIndex = 1;
//
// textBox2
//
this.textBox2.AcceptsReturn = true;
this.textBox2.AcceptsTab = true;
this.tableLayoutPanel1.SetColumnSpan(this.textBox2, 3);
this.textBox2.Dock = System.Windows.Forms.DockStyle.Fill;
this.textBox2.Location = new System.Drawing.Point(3, 57);
this.textBox2.Multiline = true;
this.textBox2.Name = "textBox2";
this.textBox2.ScrollBars = System.Windows.Forms.ScrollBars.Both;
this.textBox2.Size = new System.Drawing.Size(648, 281);
this.textBox2.TabIndex = 2;
this.textBox2.WordWrap = false;
//
// button1
//
this.button1.Dock = System.Windows.Forms.DockStyle.Fill;
this.button1.Location = new System.Drawing.Point(598, 28);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(53, 23);
this.button1.TabIndex = 3;
this.button1.Text = "Count!";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// openFileDialog1
//
this.openFileDialog1.FileName = "openFileDialog1";
//
// checkBox1
//
this.checkBox1.AutoSize = true;
this.checkBox1.Dock = System.Windows.Forms.DockStyle.Fill;
this.checkBox1.Location = new System.Drawing.Point(492, 28);
this.checkBox1.Name = "checkBox1";
this.checkBox1.Size = new System.Drawing.Size(100, 23);
this.checkBox1.TabIndex = 4;
this.checkBox1.Text = "Case Sensitive";
this.checkBox1.UseVisualStyleBackColor = true;
//
// FormFind
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(654, 341);
this.Controls.Add(this.tableLayoutPanel1);
this.MainMenuStrip = this.menuStrip1;
this.Name = "FormFind";
this.Text = "Find In Selected Text";
this.tableLayoutPanel1.ResumeLayout(false);
this.tableLayoutPanel1.PerformLayout();
this.menuStrip1.ResumeLayout(false);
this.menuStrip1.PerformLayout();
this.ResumeLayout(false);

}

#endregion
public FormFind()
{
InitializeComponent();
}

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

private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Close();
}

private void openToolStripMenuItem_Click(object sender, EventArgs e)
{
if (openFileDialog1.ShowDialog() == DialogResult.OK)
textBox2.Text = System.IO.File.ReadAllText(openFileDialog1.FileName);
}

private void button1_Click(object sender, EventArgs e)
{
string find = textBox1.Text;
string text = textBox2.Text;
if (textBox2.SelectionLength > 0)
text = textBox2.SelectedText;
if (!checkBox1.Checked)
{
find = find.ToLower();
text = text.ToLower();
}
int count = text.IndexOf(find) == 0 ? 0 : -1;
for (int i = 0; -1 < i && i < text.Length; i = text.IndexOf(find, i + 1))
count++;
MessageBox.Show(string.Format("{0} occurances", count));
}
}
}


[Formerly "capn_midnight". See some of my projects. Find me on twitter tumblr G+ Github.]


#3 HostileExpanse   Members   -  Reputation: 112

Posted 06 August 2009 - 07:59 AM

On Textpad, if I wanted to count occurrences, I suppose I could do a "find and replace" within selected text, while using the same string in both the find and replace boxes. That would give me a note of the number of occurrences, and shouldn't affect anything.


If needed, I could simply undo the replace as well ... that might be needed, if the search was some type of regex, where the "find" string and the "replace" string might not be able to have the same value.



This trick might work on other editors, too.

#4 wforl   Members   -  Reputation: 169

Posted 06 August 2009 - 08:14 AM

ha ha capn_midnight,
That works great :)

That also works great too HostileExpanse.



#5 Zahlman   Moderators   -  Reputation: 1682

Posted 06 August 2009 - 05:41 PM

In Vim, I select the text, do :%s/,//g (well, actually the command is :'<,'>%s/,//g but Vim fills in the '<,'> part), note the report of how many substitutions were made, and then undo (or quit without saving, as appropriate).




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS