Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by $$anonymous$$ · Mar 27, 2018 at 05:47 PM · uiinputfieldremoveword

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image $$anonymous$$ · Mar 27, 2018 at 06:55 PM 0
Share

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;
             }



avatar image Captain_Pineapple $$anonymous$$ · Mar 27, 2018 at 07:04 PM 0
Share

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 :)

avatar image $$anonymous$$ Captain_Pineapple · Mar 27, 2018 at 07:24 PM 0
Share

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;
             }
         }




Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

131 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges