c# error : length cannot be less than 0 - c#

I have a problem with my C# program: I have created a quiz with 10 questions and 10 images.
I get this Length cannot be less than zero.\r\nParameter name: length at the line
int imageIndex = int.Parse(line.Substring(0, delimiter));
Even if in my notepad file I included the image index:
3:What is the foo in the bar?
10:How can you add a widget?
4:Why pick a bar over a foo?
Here is the code:
if (nr >= questions.Count)
{
button1.Enabled = false;
}
else
{
Random r = new Random();
int x;
do
{
x = r.Next(questions.Count);
}
while (questions[x].displayed == true);
textBox1.Text = questionText;
radioButton1.Text = questions[x].answer1;
radioButton2.Text = questions[x].answer2;
questions[x].displayed= true;
current_question = x;
}

You've previously got a line like this:
int delimiter = line.IndexOf(':');
... but you're then not checking the return value. If it's -1, that means your delimiter wasn't found in that particular line - but you're passing it into Substring anyway. Check the value of delimiter before you use it - that way you can throw a more useful exception (or skip the line, or whatever you want to do).
I would actually suggest that you change your code significantly - instead of keeping questions as a List<string> or whatever it is, I'd create a Question class. Parse the lines of text as you read them, discarding failures - or throwing an exception - at that point, rather than waiting until you happen to hit the bad question. You can then have a List<Question> which will make the rest of the code simpler.
You might also want to keep a Queue<Question> which is initially a copy of the complete list, shuffled. When you want to show a new question, just take the next element from that queue. That way you won't need to loop round while you pick already-shown questions. (You'll want to include an Index or QuestionNumber property within the Question class, presumably...)
Note that it's possible that it's working for all the lines that you're really aware of, but that you've got some empty lines at the end of your file. You may well just want to skip empty lines.

Substring parameters are initial index, and length. delimiter from the code does not looks like length.
http://msdn.microsoft.com/en-us/library/aka44szs.aspx
Change following
int delimiter = line.IndexOf(':');
int imageIndex = int.Parse(line.Substring(0, delimiter));
string questionText=line.Substring(delimiter + 1);
pictureBox1.Image = imageList1.Images[imageIndex];
textBox1.Text = questionText;
radioButton1.Text = questions[x].answer1;
questions[x].displayed= true;
current_question = x;
To
int delimiter = line.IndexOf(':');
if(!String.IsNullOrEmpty(line) && delimiter > 0 )
{
int imageIndex = int.Parse(line.Substring(0, delimiter));
string questionText=line.Substring(delimiter + 1);
pictureBox1.Image = imageList1.Images[imageIndex];
textBox1.Text = questionText;
radioButton1.Text = questions[x].answer1;
questions[x].displayed= true;
current_question = x;
}

private string copystring(string instr ,int start ,int length)
{
return instr.Length >= (start + 1)
? (instr.Length > (start + length) ? instr.Substring(start, length) : instr.Substring(start,instr.Length-start))
: "";
}

Related

RichTextBox - How highlight end of line?

If the searched word is at the beginning or end of the line of, I want to color it. If it's in the middle of the line not colorful. I tried many thing but it's not working right.
Seems like beginning of the line is working. But end of the first line only can color. I want to color for all lines end. I think i need index of each line's beggining and loop but i can't do it.
How can i fix it?
private void button1_Click(object sender, EventArgs e)
{
int wordLength = textBox1.Text.Length;
string word = textBox1.Text;
for (int i = 0; i < richTextBox1.Lines.Count(); i++)
{
int startIndex = richTextBox1.GetFirstCharIndexFromLine(i);
richTextBox1.Find(word, startIndex, startIndex+wordLength, RichTextBoxFinds.None);
richTextBox1.SelectionColor = Color.Red;
richTextBox1.SelectionBackColor = Color.Yellow;
int newLineIndex = richTextBox1.Lines[i].Length;
richTextBox1.Find(textBox1.Text, (newLineIndex - wordLength), newLineIndex, RichTextBoxFinds.None);
richTextBox1.SelectionColor = Color.Red;
richTextBox1.SelectionBackColor = Color.Yellow;
}
Try:
int newLineIndex = i + 1 < richTextBox1.Lines.Length ? richTextBox1.GetFirstCharIndexFromLine(i + 1) - 1 : richTextBox1.TextLength;
I suggest to change your code a little bit. You'll notice why when the RichTextBox text length grows.
Asking for the Lines[] content is not exactly a good thing, much worse in a loop, when you access this Property probably many of times.
You can see in the .Net Source Code what happens (each time - the Lines Property values are not cached and cannot be).
GetLineFromCharIndex() and GetFirstCharIndexFromLine() use instead SendMessage to send the EM_LINEFROMCHAR and EM_LINEINDEX messages to the Edit control - which uses cached values - and are pretty fast.
Use Regex.Matches() to collect the indexes of the matched word(s) (you can use more than one word, separated by a pipe: "|", but here we handle just one word. When matching more than one word, use a List<Match> and the Match.Length instead of searchWord.Length) and extract just the Index position of each match.
Then, loop the indexes and check whether the current index position meets the criteria.
The current line end is found with IndexOf("\n", [StartPosition]), using the first line index (which is also used for the selection) as the starting position.
The RichTextBox Control uses only \n as line separator, so we don't need to worry about \r.
string searchWord = "John";
var txt = richTextBox1.Text;
int textLenght = txt.Length;
// the indexes list can be created with the alternative method (using IndexOf() in a loop)
var indexes = Regex.Matches(txt, searchWord, RegexOptions.Multiline)
.OfType<Match>()
.Select(m => m.Index).ToList();
foreach (int index in indexes) {
int currentLine = richTextBox1.GetLineFromCharIndex(index);
int lineFirstIndex = richTextBox1.GetFirstCharIndexFromLine(currentLine);
int lineLastIndex = txt.IndexOf("\n", lineFirstIndex);
if (index == lineFirstIndex ||
index == lineLastIndex - searchWord.Length ||
index == textLenght - searchWord.Length) {
richTextBox1.Select(index, searchWord.Length);
richTextBox1.SelectionColor = Color.Red;
}
}
Edit: Since Regex.Matches is not allowed, you can use IndexOf() in a loop:
var indexes = new List<int>();
int wordPosition = -1;
do {
if ((wordPosition = txt.IndexOf(searchWord, wordPosition + 1)) >= 0) {
indexes.Add(wordPosition);
}
} while (wordPosition >= 0);

How to replace a particular character from string in c#?

I have a string like AX_1234X_12345_X_CXY, I want to remove X from after the first underscore _ i.e. from 1234X to 1234. So final output will be like AX_1234_12345_X_CXY. How to do it?? If I use .Replace("X", "") it will replace all X which I don't want
You can iterate trough the string from the first occurrence of '_' .
you can find the first occurrence of '_' using IndexOf().
when loop will get to 'X' it will not append it to the "fixed string".
private static void Func()
{
string Original = "AX_1234X_12345_X_CXY";
string Fixed = Original.Substring(0, Original.IndexOf("_", 0));
// in case you want to remove all 'X`s' after first occurrence of `'_'`
// just dont use that variable
bool found = false;
for (int i = Original.IndexOf("_", 0); i < Original.Length; i++)
{
if (Original[i].ToString()=="X" && found == false)
{
found = true;
}
else
{
Fixed += Original[i];
}
}
Console.WriteLine(Fixed);
Console.ReadLine();
}
Why not good old IndexOf and Substring?
string s = "AX_1234X_12345_X_CXY";
int pUnder = s.IndexOf('_');
if (pUnder >= 0) { // we have underscope...
int pX = s.IndexOf('X', pUnder + 1); // we should search for X after the underscope
if (pX >= 0) // ...as well as X after the underscope
s = s.Substring(0, pX) + s.Substring(pX + 1);
}
Console.Write(s);
Outcome:
AX_1234_12345_X_CXY
string original = #"AX_1234X_12345_X_CXY";
original = #"AX_1234_12345_X_CXY";
One way is String.Remove, because you can tell exactly where to remove from. If the offending "X" is always in the same place, you can use:
string newString = old.Remove(7,1);
This will remove 1 character starting as position 7 (counting from zero as the beginning of the string).
If not always in the same character position, you might try:
int xPos = old.IndexOf("X");
string newString = old.Remove(xPos,1);
EDIT:
Based on OP comment, the "X" we're targeting occurs just after the first underscore character, so let's index off of the first underscore:
int iPosUnderscore = old.IndexOf("_");
string newString = old.Remove(iPosUnderscore + 1 ,1); // start after the underscore
Try looking at string.IndexOf or string.IndexOfAny
string s = "AX_1234X_12345_X_CXY";
string ns = HappyChap(s);
public string HappyChap(string value)
{
int start = value.IndexOf("X_");
int next = start;
next = value.IndexOf("X_", start + 1);
if (next > 0)
{
value = value.Remove(next, 1);
}
return value;
}
If and only if this is always the format then it should be a simple matter of combining substrings of the original text without including the x in that position. But the op hasn't stated that this is always the case. So if this is always the format and the same character position is always removed then you could simply just
string s = "AX_1234X_12345_X_CXY";
string newstring = s.Substring(0, 7) + s.Substring(8);
OK, based on only the second set of numbers being variable in length, you could then do something like:
int startpos = s.IndexOf('_', 4);
string newstring = s.Substring(0, startpos - 1) + s.Substring(startpos);
with this code, the following tests resulted in:
"AX_1234X_12345_X_CXY" became "AX_1234_12345_X_CXY"
"AX_123X_12345_X_CXY" became "AX_123_12345_X_CXY"
"AX_234X_12345_X_CXY" became "AX_234_12345_X_CXY"
"AX_1X_12345_X_CXY" became "AX_1_12345_X_CXY"
Something like this could work. I'm sure there's a more elegant solution.
string input1 = "AX_1234X_12345_X_CXY";
string pattern1 = "^[A-Z]{1,2}_[0-9]{1,4}(X)";
string newInput = string.Empty;
Match match = Regex.Match(input1, pattern1);
if(match.Success){
newInput = input1.Remove(match.Groups[1].Index, 1);
}
Console.WriteLine(newInput);

How to edit and replace a group of lines of a text file using c#?

I have a large text file(20MB), and I'm trying to change every 4th & 5th line to 0,0
I've tried with the following code but I will be interested to know if theres any better way of doing it..
EDIT:
Power = new List<float>();
Time = new List<float>();
string line;
float _i =0.0f;
float _q =0.0f;
int counter = 0;
StreamReader file = new StreamReader(iqFile2Open);
while ((line = file.ReadLine()) != null)
{
if (Regex.Matches(line, #"[a-zA-Z]").Count == 0)
{
string[] IQ = line.Split(',');
if (IQ.Length == 2)
{
_i = float.Parse(IQ[0]);
_q = float.Parse(IQ[1]);
double _p = 10 * (Math.Log10((_i * _i) + (_q * _q)));
if((counter%4)==0 || (counter%5)==0)
sw.WriteLine("0,0");
else
sw.WriteLine(string.Format("{0},{1}", _i, _q));
counter++;
}
}
}
Thanks in advance.!
You can read in all of the lines, map each line to what it should be based on it's position, and then write them all out:
var lines = File.ReadLines(inputFile)
.Select((line, i) => ComputeLine(line, i + 1));
File.WriteAllLines(outputFile, lines);
As for the actual mapping, you can mod the line number by 5 to get an "every 5th item" result, and then just compare the result to the two mod values you care about. Note that since you don't want the first item wiped out it's important that the index is 1-indexed, not zero indexed.
private static string ComputeLine(string line, int i)
{
if (i % 5 == 4 || i % 5 == 0)
return "0,0";
else
return line;
}
This streams through each line in the file, rather than loading the entire file into memory. Because of this it's important that the input and output files be different. You can copy the output file to the input file if needed, or you could instead use ReadAllLines to bring the entire file into memory (assuming the file stays suitably small) thus allowing you to write to the same file you read from.
What exactly are you trying to replace? Are you replacing by specific LINE or specific TEXT?
If you are looking to replace specific text you can easily do a string.Replace() method...
StreamReader fileIn = new StreamReader("somefile");
string fileText = fileIn.Readlines();
fileText = fileText.Replace("old", "new");
//Repeat last line for all old strings.
//write file...

C#/WPF: How to find the closest line breaks from the selection of a TextBox

I have a TextBox txtEditor. I want to find the nearest line breaks and select it
Example 1: With "no selection"
suppose the selection/cursor is *
this is the 1st line of text
this is *the 2nd line of text
this is the 3rd line of text
I want to extend the selection such that the selection is now
this is the 1st line of text
*this is the 2nd line of text*
this is the 3rd line of text
Example 2: With selection
this is the 1st line of text
this is *the 2nd line* of text
this is the 3rd line of text
I want to extend the selection such that the selection is now
this is the 1st line of text
*this is the 2nd line of text*
this is the 3rd line of text
Update: Possible solution
I found a possible solution, wonder if anyone got a better solution?
string tmp = txtEditor.Text;
int selStart = txtEditor.SelectionStart;
int selLength = txtEditor.SelectionLength;
int newSelStart;
int newSelLength;
string tmp1 = tmp.Substring(0, selStart);
if (tmp1.LastIndexOf(Environment.NewLine) < 0)
{
newSelStart = 0;
}
else
{
newSelStart = tmp1.LastIndexOf(Environment.NewLine) + Environment.NewLine.Length;
}
tmp1 = tmp.Substring(selStart);
if (tmp1.IndexOf(Environment.NewLine) < 0)
{
newSelLength = tmp.Length;
}
else
{
newSelLength = tmp1.IndexOf(Environment.NewLine) + selStart - newSelStart;
}
txtEditor.SelectionStart = newSelStart;
txtEditor.SelectionLength = newSelLength;
Well, mostly the problem is that your code is somwhat bloated (and thus harder to read) and a lot less efficient than it needs to be. Performance probably doesn't really matter in your case (those duplicate calls to IndexOf and LastIndexOf rub me the wrong way), but personally I'd rewrite your code like this:
string tmp = txtEditor.Text;
int selStart = txtEditor.SelectionStart;
int selLength = txtEditor.SelectionLength;
int newSelStart = tmp.LastIndexOf(Environment.NewLine, selStart);
if (newSelStart == -1)
newSelStart = 0;
int newSelEnd = tmp.IndexOf(Environment.NewLine, selStart);
if (newSelEnd == -1)
newSelEnd = tmp.Length;
txtEditor.SelectionStart = newSelStart;
txtEditor.SelectionLength = newSelEnd - newSelStart;

c# getting a string within another string

i have a string like this:
some_string = "A simple demo of SMS text messaging.\r\n+CMGW: 3216\r\n\r\nOK\r\n\"
im coming from vb.net and i need to know in c#, if i know the position of CMGW, how do i get "3216" out of there?
i know that my start should be the position of CMGW + 6, but how do i make it stop as soon as it finds "\r" ??
again, my end result should be 3216
thank you!
Find the index of \r from the start of where you're interested in, and use the Substring overload which takes a length:
// Production code: add validation here.
// (Check for each index being -1, meaning "not found")
int cmgwIndex = text.IndexOf("CMGW: ");
// Just a helper variable; makes the code below slightly prettier
int startIndex = cmgwIndex + 6;
int crIndex = text.IndexOf("\r", startIndex);
string middlePart = text.Substring(startIndex, crIndex - startIndex);
If you know the position of 3216 then you can just do the following
string inner = some_string.SubString(positionOfCmgw+6,4);
This code will take the substring of some_string starting at the given position and only taking 4 characters.
If you want to be more general you could do the following
int start = positionOfCmgw+6;
int endIndex = some_string.IndexOf('\r', start);
int length = endIndex - start;
string inner = some_string.SubString(start, length);
One option would be to start from your known index and read characters until you hit a non-numeric value. Not the most robust solution, but it will work if you know your input's always going to look like this (i.e., no decimal points or other non-numeric characters within the numeric part of the string).
Something like this:
public static int GetNumberAtIndex(this string text, int index)
{
if (index < 0 || index >= text.Length)
throw new ArgumentOutOfRangeException("index");
var sb = new StringBuilder();
for (int i = index; i < text.Length; ++i)
{
char c = text[i];
if (!char.IsDigit(c))
break;
sb.Append(c);
}
if (sb.Length > 0)
return int.Parse(sb.ToString());
else
throw new ArgumentException("Unable to read number at the specified index.");
}
Usage in your case would look like:
string some_string = #"A simple demo of SMS text messaging.\r\n+CMGW: 3216\r\n...";
int index = some_string.IndexOf("CMGW") + 6;
int value = some_string.GetNumberAtIndex(index);
Console.WriteLine(value);
Output:
3216
If you're looking to extract the number portion of 'CMGW: 3216' then a more reliable method would be to use regular expressions. That way you can look for the entire pattern, and not just the header.
var some_string = "A simple demo of SMS text messaging.\r\n+CMGW: 3216\r\n\r\nOK\r\n";
var match = Regex.Match(some_string, #"CMGW\: (?<number>[0-9]+)", RegexOptions.Multiline);
var number = match.Groups["number"].Value;
More general, if you don't know the start position of CMGW but the structure remains as before.
String s;
char[] separators = {'\r'};
var parts = s.Split(separators);
parts.Where(part => part.Contains("CMGW")).Single().Reverse().TakeWhile(c => c != ' ').Reverse();

Categories

Resources