Click here to Skip to main content
15,892,839 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hey guys!
I am working on a program which should cut a sentence if it reaches a maximum of chracters. But it shouldn´t cut the sentence just in the middle, if the max lenght is reached in the middle of the sentence it should cut it at the index of the ending of the sentence before ("."). Now I got the problem that I always get this error:
index and count must refer to a location within the string

I don´t know what is wrong.
This is my code:
C#
while (i <= maxLenght)
            {
                characters[i] = text.Substring(i, 1);
                characters[i + 1] = text.Substring(i + 1, 1);
                if (characters[i].Equals(".") && i == maxLenght)
                {

                    characters[i].Remove(i, characters.Length - 1 - i);
                }
                else if (i >= maxLenght)
                {
                    while(characters[i] != ".")
                    {
                        i--;
                    }
                    characters[i].Replace(characters[i], " ");
                    i = maxLenght;
                }

                t = String.Join("", characters);
                i++;
                
            }

            Console.WriteLine(t);


Thanks for your help in advance!

What I have tried:

I tried various parameter for index and count but it doesn´t work.
Posted
Updated 22-Apr-20 0:15am

The error message is pretty explicit:
index and count must refer to a location within the string

If you have a string with five characters in it then the index must be in the range 0 to 4, and the index plus the count must be less than 5. If either of those conditions fails, you will get an error because you are asking the system to process data outside the actual string data and it can't do that.

But we can't tell what is happening we have no idea what data you are feeding into that code, and that's going to be important to working out why you have a problem.

So, it's going to be up to you.
Fortunately, you have a tool available to you which will help you find out what is going on: the debugger. If you don't know how to use it then a quick Google for "Visual Studio debugger" should give you the info you need.

Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.

Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!
 
Share this answer
 
Comments
Member 14809651 22-Apr-20 5:34am    
It works now, thanks!
OriginalGriff 22-Apr-20 5:42am    
You're welcome!
Quote:
index and count must refer to a location within the string

Should be rather obvious if you think about this message.
When you refer to a position in a string, it must be in the string, the position must exist.
In a string of given length, valid positions are from 0 to (length-1).
This
C#
while (i <= maxLenght)

goes 1 step too far.
and this
C#
characters[i + 1] = text.Substring(i + 1, 1);

goes 1 step further.

Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

Debugging C# Code in Visual Studio - YouTube[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900