1

i am trying to make acronym using a title textbox on text change but i keep getting an error whenever i press space the error i get is

System.ArgumentOutOfRangeException: 'Index and length must refer to a location within the string. Parameter name: length'

  • this is what my form looks like: GUI

  • this is what my code looks like:

    private void txtTitle_TextChanged(object sender, EventArgs e)
        {
            txtARC.Text = "";
            if (!String.IsNullOrEmpty(txtTitle.Text))
            {
                string word = txtTitle.Text.ToString();
                string[] wordArry = word.Split(' ');
                for(int i=0; i<wordArry.Length; i++)
                {
                    txtARC.Text = wordArry[i].Substring(0, 1);
                }
            }
        }
laiakini
  • 31
  • 1
  • 1
  • 7
  • It sounds like you're trying to get the first character from a string that has zero characters. When this happens, what is the runtime value of `wordArry[i]`? What do you expect it to be? Why? What was the value of `word`? What did you expect it to be? Why? At its simplest, you can't get the first character from an empty string because it has no characters. You could probably just check the length of the string before trying to get the substring. – David Feb 01 '21 at 20:33

2 Answers2

0

If the text in the input box is "some words and a space ", then wordArry will contain the strings "some", "words", "and", "a," "space", "". Note the empty string at the end! When you try to take the first character of an empty string, you get an exception.

You can do several things here.

You can instruct string.Split() to ignore those empty strings:

string[] wordArry = word.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);

You can check the bounds of the call to Substring:

wordArry[i].Substring(0, Math.Min(1, wordArry[i].Length))

Or you can skip empty strings in the loop, like this

for (int i = 0; i < wordArry.Length; i++)
{
    if(string.IsNullOrEmpty(wordArry[i])) {
        continue;
    }

    // Process the string here  
} 
gnud
  • 77,584
  • 5
  • 64
  • 78
0

Let's extract method - ToAcronym; we can use Regular Expressions to implement it: we can Match all the words (which we define as non empty sequence of letters) and take first character from each word:

Code:

  using System.Linq;
  using System.Text.RegularExpressions;

  ...

  public static string ToAcronym(string value) {
    if (string.IsNullOrWhiteSpace(value))
      return "";

    return string.Concat(Regex
      .Matches(value, @"\p{L}+")
      .Cast<Match>()
      .Select(match => match.Value[0]));
  }

Demo:

  string[] tests = new string[] {
    "Quick Brown Fox",
    "word",
    "Два Слова", // "Two Words" in Russian
    "Have (Some) Punctuation!"
  };

  string report = string.Join(Environment.NewLine, tests
    .Select(test => $"{test,-25} => {ToAcronym(test)}"));

  Console.Write(report);

Outcome:

Quick Brown Fox           => QBF
word                      => w
Два Слова                 => ДС
Have (Some) Punctuation!  => HSP

Your code can be

  private void txtTitle_TextChanged(object sender, EventArgs e) {
    txtARC.Text = ToAcronym(txtTitle.Text);
  }
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215