4

Ok so I made a code that includes string/int arrays with some for loops. In my code there is a part where it counts more than 1 of the string but how do you make the string plural when there is more than one? Here's a part of the code that I'm talking about:

using System.Collections; 
using System.Collections.Generic; 
using UnityEngine; 

public class VoidFunctions : MonoBehaviour 
{   
  public string[] Enemies = {"Ghoul", "Skeleton", "Zombie"}; 
  public int[] enemyCount = {1, 2, 2};
  public virtual void Start()
  {
     for (int i = 0; i < Enemies.Length; i++)
     {
         Fight(Enemies[i], enemyCount[i]);
     }
  }
  void Fight(string enemy, int amount) 
  {
    print(this.name + " encountered a " + enemy);
    print(this.name + " killed " + amount + " " + enemy);     
  }
}

So for the second string "Skeleton", there is 2 killed but it comes out "killed 2 Skeleton"...how do you make it plural?

2
  • 4
    You can use the Pluralization Service msdn.microsoft.com/en-us/library/… Commented Oct 9, 2017 at 18:09
  • 1
    I advise you to create an Enemy class instead of trying to track every enemy property with independent arrays. Commented Oct 9, 2017 at 18:26

2 Answers 2

2

As Ness Rosales stated, you could use pluralization software or conversion charts. Although for this type of project, I would consider the use of such software overkill if you have under 20 items.

What I would do is change the array of enemies to have both singular and plural forms of each noun:

public string[][2] Enemies =
{
    {“Ghoul”, ”Ghouls”}, {“Skeleton”, “Skeletons”}, {“Zombie”, “Zombies”}
};

From here, you can make an if/else statement to get string 0 or 1 of each noun depending on the quantity.

Sign up to request clarification or add additional context in comments.

3 Comments

The problem with that approach is that you have to type both form for each word, then search that list. What does happen when the list does not contain the requested word ?
@Aybe The only viable options are to either use a massive library of singular-plural conversions, or just take the conversions that are needed (in this case, 3) and store it in an array. Of course, we are assuming that the user does not dynamically add singular nouns and expects the program to output the plural form. In that case, yes, you are correct and this method will not work.
I'm a super beginner on this anyway and the "project" is really just a short code with 10 examples of void functions XD but I did a little more to make it more fun How exactly would you word the if/else statement and would it be with the for loops or in the void function?
2

There are multiple ways to achieve that:

  • a simple way that simply adds an s at the end (western language dependent)
  • a robust way with PluralizationService as pointed out by @Ness Rosales

Code:

using System;
using System.Data.Entity.Design.PluralizationServices;
using System.Globalization;

namespace WindowsFormsApp1
{
    internal class MyClass
    {
        private static void CheckParameters(string word, int count)
        {
            if (string.IsNullOrWhiteSpace(word))
                throw new ArgumentException("Value cannot be null or whitespace.", nameof(word));

            if (count <= 0)
                throw new ArgumentOutOfRangeException(nameof(count));
        }

        public static string GetLabel1(string word, int count)
        {
            CheckParameters(word, count);

            var label = $"{word}{(count > 1 ? "s" : string.Empty)}";

            return label;
        }

        public static string GetLabel2(string word, int count)
        {
            CheckParameters(word, count);

            // TODO this should be a member instead of being instantiated every time
            var service = PluralizationService.CreateService(CultureInfo.CurrentCulture);

            var label = count > 1 ? service.Pluralize(word) : service.Singularize(word);

            return label;
        }
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.