- Home /
In an Input Field, I want to remove the whole word, if my user deletes on character of it
As in title, I want to remove the whole word in the input field, if one character is removed. Do you have help? Thanks in advance
Answer by Captain_Pineapple · Mar 27, 2018 at 06:38 PM
Hey, simply add a script to your input field wich keeps track of the current string in your field. Then you add the following function to your Input field in the "OnValueChanged" section.
public void StringGotChanged(string newstring){
if(!newstring.Contains(oldstring))
{
//something got deleted
// split the strings by Spaces and then iterate over all the words. -> compare them from oldstring to newstring.
//after comparing you can reconstruct the whole string and reset it in the inputfield text
}
else
{
oldstring = newstring;
}
}
When you change the value of the InputField is changed this function is called and the new string is passed along. Now you can compare the new string with the old one. If the new string still contains the old value then it only get longer and nothing was deleted. When this does by some chance not work try comparing the lengths of the strings. This should also work. Don't forget to initialize your "oldstring" value in the Start-function.
I´m not sure, will this code work?
public void StringGotChanged ( string newstring )
{
if ( !newstring.Contains ( oldstring ) )
{
//something got deleted
var splittedNewstring = newstring.Split ( ' ' );
var splittedOldstring = oldstring.Split ( ' ' );
for ( int i = 0; i < splittedOldstring.Length; i++ )
{
if( splittedNewstring[i] != splittedOldstring [i] )
{
splittedNewstring [i].Remove ( i, 1 );
}
}
foreach ( string s in splittedNewstring )
{
newstring += s;
}
// split the strings by Spaces and then iterate over all the words. -> compare them from oldstring to newstring.
//after comparing you can reconstruct the whole string and reset it in the inputfield text
//set input field text here
}
else
{
oldstring = newstring;
}
You will also have to update your oldstring after reassembling the newstring. Apart from that im not sure if it is safe to fiddle with lists/arrays while iterating them - > this might cause some trouble. I'd rather save the strings i'd like to remove in a seperate array and then later on remove these from the splittedNewstring before reassembling. Otherwise i'd say go ahead and try it. Thats the best way to be sure :)
I adapted the code a bit, but my oldStrings Array always contains a space which the new one doesn´t so it throws an exception :(
public void StringGotChanged ( string newstring )
{
if ( !newstring.Contains ( oldstring ) )
{
//something got deleted
var splittedNewstring = newstring.Split ( ' ' );
var splittedOldstring = oldstring.Split ( ' ' );
List<string> stringToStay = new List<string> ();
for ( int i = 0; i < splittedOldstring.Length; i++ )
{
Debug.Log ( splittedNewstring.Length + "new" );
Debug.Log ( splittedOldstring.Length + "old" );
foreach ( string s in splittedOldstring )
{
Debug.Log ( s );
}
if ( splittedNewstring[i] == splittedOldstring [i] ) // exception right here
{
//splittedNewstring [i].Remove ( 1 );
stringToStay.Add ( splittedNewstring[i] );
}
}
newstring = "";
foreach ( string s in stringToStay )
{
newstring += s;
oldstring = newstring;
}
// split the strings by Spaces and then iterate over all the words. -> compare them from oldstring to newstring.
//after comparing you can reconstruct the whole string and reset it in the inputfield text
//set input field text
}
else
{
oldstring = newstring;
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612155457im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Changing just the name of person in UI Text 0 Answers
Customize Inputfield Unity Plus/Pro 0 Answers
Hide Textselection in Scrollable Inputfield 1 Answer
How do I change "return"/"submit" button to "search" in native android keyboard in TMP_InputField? 1 Answer
Prevent focus from leaving inputfield 0 Answers