Limit a string or a UI Text

Good Evening I was wondering if it was possible to set a limite to te number of word in a string or in a text component I ve tried this

private int Maxletters = 6;
private string Name1;
void Update ()
    {
        Name1=Name1.Substring (3,Maxletters);
    }

It says array out of index,any suggestion?
Thanks to all,and have a nice day

substring requires a start index and a char count, or just a start index. Which means if you have no letters in your string and you try to substring it at index 3, there is no index 3 and it will be out of range.

You shouldn’t do this in update. You should simply when updating your text/string do a check at that point to see if it has more than 3 chars and then substring it. Otherwise, just set the value. But this also depends on how you are setting this. If this is an inputfield where a player can enter info, you may also want to provide feedback and there is also a way on inputfields to set char limits.

1 Like

Well thanks for your reply,it’s a text that I convert into a string,could you provide a sample cose ,please?

I’m not sure where you’re getting the text from still, but it’s simple

string myString = "myText";
if(myString.Length > 3)
{
      myString = myString.Substring(0, 3);
}
else
//Do something else if you need to

A string is simply an array of chars is a good way to look at them, so they come with a length property you can check against.

Thanks for your reply I tried in this way but I could type infinte letters:

void CheckString ()
    {
        if(Name1.Length >15)
        {
            Name1 = Name1.Substring (0, 15);
        }
    }
   
    // Update is called once per frame
    void Update () {
        Name1 =1Text.text;
        CheckString ();
1 Like

You never assign Name1 back to the text input

Here you go:

    private int Max_Letters = 6;

    private string _name1;

    public string Name
    {
        get
        {
            return _name1;
        }
        set
        {
            _name1 = value.Substring(0, Max_Letters);
        }
    }

Then you can do:

Name = "UnityGameIta";
Text.text = Name;
1 Like

If you are doing an inputfield, why not just use the character limiter built into the input field?
And as KelsoMRK said, you aren’t assigning the value back.

Well I worked on the suggestions and I figured it out,when the number of the string is equal to 15 letters I me able not to add a new letter

Name1 = 1Text.text;

        if(Name1.Length >15)
        {
            Name1 = Name1.Substring (0, MaxCaratteri);
        }
        1Text.text = Name1;

this is in update
Thanks for your help

Like a keyboard?
If so, just create a variable int and every time a button is pressed add 1 to it. Everytime a ‘backspace’ occurs take 1. In the function on the button where you add a letter have an if characterCount < maxCharacter, do something, else return, this is not allowed, too many characters.

/* It wasn’t easy, but I came up with a solution to detect which button is being pressed in Unity.
I still have to program the C# code to run only once for every key but it’s almost what I need.
Never mind… I figured that out within a couple minutes. */

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Controller : MonoBehaviour
{
public string key;
private string keySet;
// Start is called before the first frame update
void Start()
{
key = “None”;
}
void OnGUI()
{
key = Input.inputString;
if (key.Length > 1)
{
// index of string array and number of characters accepted
key = key.Substring(0, 1);
}
if (Input.anyKeyDown && key != keySet)
{
if (key != “None”)
{
Debug.Log(key);
keySet = key;
}
}
}
}

public TMP_InputField mainInputField;
public string playerName;
public Text Text;
void Start()
{
//Changes the character limit in the main input field.
mainInputField.characterLimit = 10;
}
public void Update()
{
playerName = mainInputField.text;
Text.text = playerName;
}

Please don’t necro posts. Also, here’s how to post code on these forums: Using code tags properly