Search Unity

Remove text from string

Discussion in 'iOS and tvOS' started by bpritchard, Aug 15, 2010.

Thread Status:
Not open for further replies.
  1. bpritchard

    bpritchard

    Joined:
    Jan 29, 2009
    Posts:
    444
    Hey everyone,

    I'm trying to figure out a good way (in C#) to remove one string from another...

    so basically i would have something like this...

    str1 = lookAtMe
    str2 = AtMe

    result = String.Remove(str2, str1)

    Basically i want result to be look (so that AtMe comes out).

    Thanks!
    Bryan
     
  2. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    lazy pants like me use Replace in such cases :)

    str1 = lookAtMe
    str2 = AtMe

    result = str1.Replace(str2, "")


    but there are other tricks too.

    What in your case is the problem is that there is no such function (the remove requires int indexes and length, so you would have to extract its position with IndexOf first).

    the string class is documented at http://msdn.microsoft.com/en-us/library/system.string_methods.aspx
     
    dyupa, AminSB00, MagiJedi and 17 others like this.
  3. bpritchard

    bpritchard

    Joined:
    Jan 29, 2009
    Posts:
    444
    Hey thanks for the advice... i took the "lazy way" and it works like a charm. The upswing is the text i'm removing is a constant so YEAH ME!

    Thanks a ton... i do appreciate it.

    Cheers
    Bryan
     
  4. IronClad771

    IronClad771

    Joined:
    Jan 5, 2014
    Posts:
    7
    8 years later and this still works!
     
  5. OronC

    OronC

    Joined:
    Jun 7, 2015
    Posts:
    2
    9.5 years* ,its a basic c# func but does the job perfecly
     
  6. unity_1060679

    unity_1060679

    Joined:
    Mar 24, 2019
    Posts:
    1
    14 years later... it dosent work for me:(
     
  7. alvarometon

    alvarometon

    Joined:
    Jul 9, 2012
    Posts:
    3
    10 year later... This works for me :cool:
     
  8. bond007in

    bond007in

    Joined:
    Dec 9, 2013
    Posts:
    1
    12 year later...
     
  9. khidagrim012

    khidagrim012

    Joined:
    Mar 7, 2020
    Posts:
    1
    One Eternity Later...
     
  10. ufukinyo

    ufukinyo

    Joined:
    Dec 21, 2014
    Posts:
    1
    10 year later. This works for me :cool:
     
  11. Da_Neel

    Da_Neel

    Joined:
    Dec 29, 2013
    Posts:
    15
    10+ years later. I'm in history too now
     
  12. Deleted User

    Deleted User

    Guest

    am I too late 10+ Years, for that small trick?
     
  13. NightwishEpica

    NightwishEpica

    Joined:
    Sep 13, 2019
    Posts:
    3
    11+ years still works, hey future generations!
     
  14. Jman2150

    Jman2150

    Joined:
    Aug 23, 2017
    Posts:
    10
    10 years and six months later and this is exactly what I needed.
     
  15. R3D0

    R3D0

    Joined:
    Sep 24, 2014
    Posts:
    6
    5,594,661 minutes later and it still works
     
    Dan_G and ROBYER1 like this.
  16. saifshk17

    saifshk17

    Joined:
    Dec 4, 2016
    Posts:
    488
    It has been 4047 days or 11 years 29 days or 132 months 29 days and this solution still works.
     
    dyupa and AVTHS like this.
  17. sambhav420

    sambhav420

    Joined:
    Dec 23, 2015
    Posts:
    1
    11 years later, my json string just won't have it.
     
  18. Alacer

    Alacer

    Joined:
    Jun 22, 2016
    Posts:
    14
    Working like a charm for me.
     
  19. WobbleBlocks

    WobbleBlocks

    Joined:
    Jun 17, 2018
    Posts:
    1
    13 years later! still helpful af
     
  20. nervousDebugger

    nervousDebugger

    Joined:
    Apr 4, 2020
    Posts:
    1
    14 years later and... I don't need it
     
  21. iMobCoding

    iMobCoding

    Joined:
    Feb 13, 2017
    Posts:
    165
    Code (CSharp):
    1. var result = "StillNotWorking".Replace("Not", " ");
     
    kcreanor likes this.
  22. joeri_07

    joeri_07

    Joined:
    Dec 18, 2015
    Posts:
    45
    something something involving elapsed time and the usefulness of this piece of code. Thank you!
     
Thread Status:
Not open for further replies.