-2

I working on Natural Language Processing, I am trying to remove last part of string, but it remove string from inside a string, my code.

public string RemoveSuffix(string word)
{
  if (word.EndsWith("ی")
  { 
    word = word.Replace("ی","");
  }

  return word;
}
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
tayyaba
  • 93
  • 1
  • 6
  • 2
    What is the given input, how do you mean it does not work – EpicKip Oct 17 '17 at 07:48
  • 1
    String replace function replace all occurrence of sub string what you provide, you should use remove method of string – rtraees Oct 17 '17 at 07:49
  • `word=word.Replace("ی","");` would replace all occurrences not only the suffix. – dcg Oct 17 '17 at 07:50
  • `Replace` does exactly what it says - it replaces this string, no matter where it's found. Why should it replace only the *last* occurence? – Panagiotis Kanavos Oct 17 '17 at 07:53
  • Can't reproduce your problem, this code works for me. Could it be that the language this is in is *Right to Left* and you actually need to trim the start? Please provide a [mcve] to clarify. – Manfred Radlwimmer Oct 17 '17 at 07:56
  • 1
    @ManfredRadlwimmer the OP says `Replace` removes the character from any position, not just the end. Just as it should – Panagiotis Kanavos Oct 17 '17 at 08:01
  • @PanagiotisKanavos Oooh. Thanks for clarifying. I thought he meant the replace works except when he checks with `EndsWith`, my bad. – Manfred Radlwimmer Oct 17 '17 at 08:13

2 Answers2

4

You, probably, are looking for TrimEnd:

  // static: you have no need in "this"
  public static string RemoveSuffix(string word) {
    return word == null // <- do not forget to validate public method's argument(s)
      ? null            // or throw ArgumentNullException      
      : word.TrimEnd('ی'); 
  }
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
  • 1
    Could be also wrote as: `public string RemoveSuffix(string word) => word?.TrimEnd('ی') ?? null;` – Giulio Caccin Oct 17 '17 at 07:54
  • 1
    @GiulioCaccin what's the point of `?? null`? Did you want to write `?? ""` perhaps? – Panagiotis Kanavos Oct 17 '17 at 07:55
  • @PanagiotisKanavos just to keep the semantics and the comments the original author wrote ^_^ It's better with the empty string, i'll update. – Giulio Caccin Oct 17 '17 at 07:57
  • @GiulioCaccin then I repeat, what's the point of `?? null`? Read what you typed again. `??` will only work if the left-hand value is `null`. And then it will return ... null. Might as well remove it altogether – Panagiotis Kanavos Oct 17 '17 at 07:58
  • @PanagiotisKanavos i know, it was to keep semantics without providing another answer. – Giulio Caccin Oct 17 '17 at 07:58
  • @GiulioCaccin But it does not make sense, ?? means if null so you have if null return null.... – EpicKip Oct 17 '17 at 07:59
  • 1
    In the spirit of wisecracking, an exception throwing expression bodied function could be `RemoveSuffix(string word) => word?.TrimEnd('ی') ?? throw new ArgumentNullException(nameof(word))` – Panagiotis Kanavos Oct 17 '17 at 07:59
  • @PanagiotisKanavos it was exactly the kind of stuff i was pointing to. It's also working with `?? null` – Giulio Caccin Oct 17 '17 at 08:02
  • @GiulioCaccin For the last time, your `?? null` is nonsense (just trying to help you understand `??`). You can just leave that whole part out. its like: return null but if returning value is null return null instead... which is nonsense – EpicKip Oct 17 '17 at 11:33
-2

you should have to try

public string RemoveSuffix(string word,string lstPart)
{
 return  word=word.TrimEnd(lstPart);

}

you can change your hard code word with a variable/List Item

rtraees
  • 312
  • 2
  • 15