I have a string which contains a lot of useless information after the first char: space. So I built a StringBuilder to remove unnecesary characters after the first space.
public static string Remove(string text)
{
int index1 = text.IndexOf(' ');
int index2 = text.Lenght;
StringBuilder sv = new StringBuilder(text.Lenght);
sv.Append(text);
sv.Remove(index1, index2);
string text2 = sv.ToString();
return text2;
}
Can somebody explain why this throws me an error? Thank you!
The reason for this exception is that you misunderstood the purpose of the second parameter: rather than specifying the ending index, it specifies the length of the segment to be removed.
Since your code is passing the length of the entire text string, the only valid input for the first parameter would be zero. To pass a proper value, subtract the first index from the second index, and add 1 to the result.
Note: It looks like you are removing everything from the string starting at the first space ' '. A simpler way of doing it would be with substring:
int index = text.IndexOf(' ');
return index >= 0 ? text.Substring(0, index) : text;
The documentation for Remove says it all - only one exception is raised for that method
ArgumentOutOfRangeException: If startIndex or length is less than zero, or startIndex + length is greater than the length of this instance.
So one of two things is going on
Your string does not contain a space (startIndex will be less than zero)
The startIndex+length is greater than the total length of the string.
Crucially, with the code you've posted 1. above could sometimes be true, and 2. will always be true! You should have done index2-index1 for the second parameter.
Related
I have code which checks if a word if a palindrome or not. Within the for loop there is a -1 value. Can someone explain to me why -1 is used after the name.Length in c#
public static void Main()
{
string name = "Apple";
string reverse = string.Empty;
for (int i = name.Length - 1; i >= 0; i--)
{
reverse +=name[i];
}
if (name == reverse)
{
Console.WriteLine($"{name} is palindrome");
}else
{
Console.WriteLine($"{name} is not palindrome");
}
That's because whoever wrote the code, wanted to write:
reverse += name[i];
String operator [] takes values from 0 upto (string's length-1). If you pass length or more, you will get an exception. So, code's author had to ensure that i==Length won't be ever passed there. So it starts from Length-1 and counts downwards.
Also, note that the other bound of i is 0 (>=, not >, so 0 is included), so the loop visits all values from 0 to length-1, so it visits all characters from the string. Job done.
However, it doesn't have to be written in that way. The only thing is to ensure that the string operator [] wont see values of of its range. Compare this loop, it's identical in its results:
for (int i = name.Length; i >= 1; i--)
{
reverse += name[i-1];
}
Note that I also changed 0 to 1.
Of course, it's also possible to write a loop with the same effects in a lot of other ways.
The first element in an array is at the index 0 (array[0]). Because the indexing starts at 0 instead of 1 it means that the final element in the array will be at index array.Length-1.
If you had the word and then your array would look like:
name[0] = 'a'
name[1] = 'n'
name[2] = 'd'
the name.Length would equal 3. As you can see, there isn't an element at index 3 in the array so you need to subtract 1 from the length of the array to access the last element.
The for loop in your example starts with the last element in the array (using i as the index). If you tried to set i to i = name.Length then you would get an index out of bounds error because there isn't an element at the position name.Length.
String operator [] takes values from 0. The first element in an string is at the index 0, so we need to adjust by subtracting one.
For Example:
string str = "test";
int length = str.length; //Length of the str is 4. (0 to 3)
Let's say I have a string called test,
string test = "hello my dear world"
and I want to get the last letter's index, (which is 19) .
How do I get that index, and insert it into an int/string?
This is extremely simple.
string test = "Hello World";
char theLastCharacterOfTest = test[test.Length - 1]; // 'd'
int theIndexOfTheLastCharacter = test.Length - 1; // 10
Want an explanation? Here it is!
Let's start with getting the index of the last character. Since C# uses a 0-based index system (i.e. the first index is 0), the last index is the length of the string - 1.
The last character is just the character at the last index, right? And the indexer of string returns the character at the index passed in. If we combine these two, we get test[test.Length - 1].
I don't think you are very familiar with indexers, so here's a link:
https://msdn.microsoft.com/en-us/library/6x16t2tx.aspx
I'm not sure whether you're looking for the index or position of the last character (you said the index is 19, but that's the position...the index is 18). Here's both:
string test = "hello my dear world";
// The length of the text gives the position of the last char
int position = test.Length;
// C# has a 0-based index. You need the string length -1 to get the last char's position
int index = test.Length - 1;
Here's a working example.
int index = test.Length;
char LastLetter = test[index - 1];
please search , before posting your question.
string test = "hello my dear world";
int index = test.FindIndex(x => x.StartsWith("d"));
OR
int index = test.Length - 1;
I'm making a program which reverses words to sdrow.
I understand that for it to work it needs to be written this way.
I'm more interested on WHY it has to be this way.
Here is my code:
Console.WriteLine("Enter a word : ");
string word = Console.ReadLine();
string rev = "";
int length;
for (length = word.Length - 1; length >= 0; length--)
{
rev = rev + word[length];
}
Console.WriteLine("The reversed word is : {0}", rev);
My questions are:
a) why you must use quotes to initialize your string
b) Why must you start your loop at one less than the total length of your string
c) How arrayname[int] works
I'm pretty new to C# and this site, as well. I hope my questions make sense and that I've asked them in the correct and proper way.
Thank you for your time!
This is how I've interpreted your question.
You want to know why you must use quotes to initialize your string
Why must you start your loop at one less than the total length of your string
How arrayname[int] works
I think that the best way to explain this to you is to go through your code, and explain what it does.
Console.WriteLine("Enter a word : ");
The first line of code prints Enter a word : into the console.
string word = Console.ReadLine();
This line "reads" the input from the console, and puts it into a string called word.
string rev = "";
This initiates a string called rev, setting it's value to "", or an empty string. The other way to initiate the string would be this:
string rev;
That would initiate a string called rev to the value of null. This does not work for your program because rev = rev + word[length]; sets rev to itself + word[length]. It throws an error if it is null.
The next line of your code is:
int length;
That sets an int (which in real life we call an integer, basically a number) to the value of null. That is okay, because it gets set later on without referencing itself.
The next line is a for loop:
for (length = word.Length - 1; length >= 0; length--)
This loop sets an internal variable called length to the current value of word.Length -1. The second item tells how long to run the loop. While the value of length is more than, or equal to 0, the loop will continue to run. The third item generally sets the rate of increase or decrease of your variable. In this case, it is length-- that decreases length by one each time the loop runs.
The next relevant line of code is his:
rev = rev + word[length];
This, as I said before sets rev as itself + the string word at the index of length, whatever number that is at the time.
At the first run through the for loop, rev is set to itself (an empty string), plus the word at the index of length - 1. If the word entered was come (for example), the index 0 would be c, the index 1 would be o, 2 would be m, and 3 = e.
The word length is 4, so that minus one is 3 (yay - back to Kindergarten), which is the last letter in the word.
The second time through the loop, length will be 2, so rev will be itself (e) plus index 2, which is m. This repeats until length hits -1, at which point the loop does not run, and you go on to the next line of code.
...Which is:
Console.WriteLine("The reversed word is : {0}", rev);
This prints a line to the console, saying The reversed word is : <insert value of rev here> The {0} is an internal var set by the stuff after the comma, which in this case would be rev.
The final output of the program, if you inserted come, would look something like this:
>Enter a word :
>come
>
>The reversed word is : emoc
a) you must to initialize or instantiate the variable in order to can work with it. In your case is better to use and StringBuilder, the string are inmutable objects, so each assignement means to recreate a new string.
b) The arrays in C# are zero index based so their indexes go from zero to length -1.
c) An string is an characters array.
Any case maybe you must to try the StringBuilder, it is pretty easy to use
I hope this helps
a) you need to initialize a variable if you want to use it in an assignment rev = rev + word[length];
b) you are using a for loop which means that you define a start number length = word.Length - 1, a stop criteria length >= 0 and a variable change length--
So lenght basically descends from 5 to 0 (makes 6 loops). The reason is that Arrays like 'string' a char[] are zerobased indexed.. means that first element is 0 last is array.Length - 1
c) So a string is basically a chain of char's.. with the []-Operator you can access a single index. The Return Type of words[index] is in this case a character.
I hope it helped
a) You need to first instantiate the string rev before you can assign it values in the loop. You could also say "string rev = String.Empty". You are trying to add word to rev and if you don't tell it first that rev is an empty string it doesn't know what it is adding word to.
b) The characters of the string have indexes to show which position they appear in the string. These indexes start at 0. So you're first character will have an index of 0. If the length of your string is 6 then the last character's index will be 5. So length - 1 will give you the value of the last character's index which is 5. A c# for loop uses the following parameters
for (int i = 0; i < 10; i++)
{
Console.WriteLine(i);
}
where "int i = 0;" is the starting index; "i < 10" tells the loop when to stop looping; and "i++" tells it to increment i (the index) after each loop.
So in your code you are saying start at the last character of my string; perform this loop while there are still characters in the string; and decrease the index of the string after each loop so the next loop will look at the previous character in the string.
c) word[length] then in this scenario is saying add the character that has the position with index "length" to the rev string. This will basically reverse the word.
As usual you can do it in linq and avoid the (explicit) loops
using System;
using System.Linq;
namespace ConsoleApplication1
{
class Program
{
public static void Main(string[] args)
{
Console.WriteLine("Enter a word : ");
string word = Console.ReadLine();
Console.WriteLine("The reversed word is : {0}", new string (word.Reverse().ToArray()));
Console.ReadLine();
}
}
}
var reversedWords = string.Join(" ",
str.Split(' ')
.Select(x => new String(x.Reverse().ToArray())));
Take a look here :
Easy way to reverse each word in a sentence
I am trying to extract the first 200 words of a string and sometimes I get the following error:
"Index and length must refer to a location within the string. Parameter name: length"
The code is:
int i = GetIndex(fullarticle, 200);
string result = fullarticle.Substring(0, i);
How do I fix this?
It goes out of range as your string is shorter than 200 characters
To remedy you could use Math.Min it will pick the lower value between string length and 200.
fullarticle.Substring(0, Math.Min(fullarticle.Length, 200));
Hope this saves you some time.
It appears safe to assume the error is coming from string.Substring. Given that you get this error when startIndex + length > given.Length or startIndex < 0 or length < 0, GetIndex is either returning a value greater than fullarticle.Length or a negative number. The error exists in GetIndex so if you wish to carry on with the code you have, you should post the code of GetIndex to get the best answer.
If you're up for something different, you could try this:
static string GetShortIntroduction(string phrase, int words)
{
// simple word count assuming spaces represent word boundaries
return string.Join(" ", phrase.Split().Take(words));
}
This is probably happening because the string has less than 200 words in it and likely coming from GetIndex return a value for i greater than the number of characters in fullarticle. As an example of the error
"s".Substring(0,2)
throws
ArgumentOutOfRangeException: Index and length must refer to a location within the string.
Parameter name: length
If your intent is to get up to the first 200 words in a string, you would need to check
The string is not null
The number of words in the string; if it's less than 200 words, that should be your maximum index, otherwise use 200.
substring based on 2.
Looks like i is larger than the whole fullarticle length. Check your GetIndex function.
I've got a RichTextBox, and would like to highlight a whole word, given just its starting index.
I've been able to highlight a word if the starting index and length is known, however in this case I do not know the length of the word. Is it possible to highlight from a starting index to the first occurance of a space?
UPDATE:
This is what I've tried so far:
resultsRichTextBox.Select(novelOffset - 2, searchString.Length);
Unfortunately 'searchstring' is not always the length of the word being searched for, so I need a way of finding the amount of characters from novelOffset - 2 till the nearest space.
You can do something like this:
int length = this.richTextBox1.Text.Skip(startIdx)
.TakeWhile(x => char.IsLetterOrDigit(x))
.Count();
this.richTextBox1.Select(startIdx, length);
this.richTextBox1.SelectionBackColor = Color.Yellow;
Obviously you can change char.IsLetterOrDigit with x != ' ' or whatever you prefer.
You can use the String.IndexOf(Char, Int32):
Reports the zero-based index of the first occurrence of the specified
Unicode character in this string. The search starts at a specified
character position.
It will give you the starting and end index of your word. You can then highlight it!
int endIndex = resultsRichTextBox.Text.IndexOf(' ', novelOffset - 2);
resultsRichTextBox.Select(novelOffset - 2, (endIndex - (novelOffset - 2)) );
You only need to handle what happens if it doesn't find any space after the word. The endIndex value will be -1 if that happens. I would simply set the value of endIndex to searchString.Length.
int startIndex;
//fill startIndex with the known value
int endIndex = startIndex;
while(rtb.Text.CharAt(endIndex) != ' ' && endIndex < rtb.Text.Length)
{
endIndex++;
}
rtb.Select(startIndex, endIndex);
You can use a Find method given you provide the starting index of the word and look for the SPACE. Find will return you the index of next space and in fact the end of the word (found - 1).
You can then use a select call.