i am creating a small cpu simulator and i need to parse the assembly instruction which is like "add $s1 $s2 $s3", i need to get these four individual words ignoring the white spaces, if i use string.split(' '), it wont work if user enters more than one space between the words. i think of implementing it using subscript, but there should be easier way.
any ideas?

Dani AI

Generated

The simplest fix for the whitespace problem was suggested by and confirmed by : use a split approach that ignores empty tokens (the Split overload that removes empty entries). That handles multiple spaces or tabs between fields so a line like add $s1 $s2 $s3 becomes four tokens (mnemonic + three operands).

For slightly more robust tokenization (handles commas, mixed tabs/spaces, and runs of whitespace) a regex split is concise and clear in C#:

using System.Text.RegularExpressions;

string line = "add $s1,   $s2   $s3";
string[] parts = Regex.Split(line.Trim(), @"[\s,]+");
// parts[0] -> "add", parts[1..] -> operands

Python has an equivalent, useful when the thread tag includes Python:

import re
line = "add $s1,  $s2   $s3  # comment"
line = line.split('#', 1)[0].strip()        # remove trailing comments
parts = re.split(r'[\s,]+', line)

Notes and practical tips for a CPU simulator parser: trim and normalize the mnemonic (e.g., ToLowerInvariant()), validate token counts and operand formats early, and handle common assembler syntax (commas, parentheses for memory operands like 4($sp), and comment characters). Regex-based splits are fine for simple tokenization and quick prototyping, but if the assembler dialect grows (labels, directives, quoted strings, negative immediates, macro syntax), implement a small lexer that scans characters and builds tokens—that approach avoids subtle corner-cases and is easier to extend and test. Also, ’s follow-up on deselecting DataGridView rows shows practical GUI fixes can live alongside parsing logic when building a simulator front end.

Recommended Answers

All 6 Replies

Try str.Split(new char[] {' '}, StringSplitOptions.RemoveEmptyEntries); .

commented: very good +3

Try str.Split(new char[] {' '}, StringSplitOptions.RemoveEmptyEntries); .

if that works i will kiss you on your forehead.

wow it worked :

string something = "ali veli      deli";
        string[] somethings = something.Split(new char[]{' '}, StringSplitOptions.RemoveEmptyEntries);

        Response.Write(somethings.Length.ToString());

the output is 3 as expectedly.
now the question for you, how were you able to find this information?

do you also know how to deselect all rows in a windows application?

i found it :

foreach (DataGridViewRow dr in DataGridName.SelectedRows)
{
  dr.Selected = false;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.