How to remove part of string in an array on last occurrence

I have this simple array below, for example:

[
  { team: { name: 'Newport County AFC' } },
  { team: { name: 'Southport FC' } },
  { team: { name: 'AFC Fylde' } },
  { team: { name: 'FC United' } }
]

I want to remove the FC and AFC substrings of the given team names only on the last part/occurrence of the string.

I can only come up with this code below, which you can also see here

arr.forEach(function(key, index, array)
{
    let teamname = key.team.name.replace(/AFC|FC/g,'');
    array[index] = teamname;
});

However, there is a problem, why? because some team names start with FC or AFC thus they will be removed and also my approach leaves the strings with whitespaces, which is not the intended way I want.

My expected output is:

['Newport County', 'Southport', 'AFC Fylde', 'FC United']

So how do I achieve it? is there a way to improve the code or a better solution?

You could look for the end of the string as well.

var array = [{ team: { name: 'Newport County AFC' } }, { team: { name: 'Southport FC' } }, { team: { name: 'AFC Fylde' } }, { team: { name: 'FC United' } }];

array.forEach(function(object) {
    object.team.name = object.team.name.replace(/\s*(AFC|FC)$/, '');
});

console.log(array);

Write a C Program to Remove Last Occurrence of a Character in a String with example. C Program to Remove Last Occurrence of a Character in a String Example 1. This programallows the user to enter a string (or character array), and a character value. Next, this C program will find and remove last occurrence of a character inside a string.

You can use Array::map() and String::replace detecting if the string you want to replace is at the end using $. Also, you can use String::trim() to removes whitespace from both ends of the resulting string.

const input = [
    {team: {name: 'Newport County AFC'}},
    {team: {name: 'Southport FC'}},
    {team: {name: 'AFC Fylde'}},
    {team: {name: 'FC United'}}
];

let res = input.map(
    ({team: {name}}) => name.replace(/AFC$|FC$/, "").trim()
);

console.log(res);

Now, let’s say you want to delete everything after or before a substring in a string. We can use String.IndexOf () to find the position of the substring and can use the starting index and number of characters to remove. The following example removes everything before and after substring ‘founder’ in a string.

Here's an option using replace with a different regex

   var teams = [
      {
        team: { name: 'Newport County AFC' }
      },
      {
        team: { name: 'Southport FC' }
      },
      {
        team: { name: 'AFC Fylde' }
      },
      {
        team: { name: 'FC United' }
      }
    ]
    var junk = (/\sA?FC/g)
    var newTeams = teams.map((t) => {
    return t.team.name.replace(junk, "")
    })
    console.log(newTeams)
    
    
    

how to find duplicate strings in a string array using java, total number of strings how many time they have repeated. using java string methods using nested for loops eclipse.

In this tutorial, we're going to be looking at various means we can remove or replace part of a String in Java. We'll explore removing and/or replacing a substring using a String API, then using a StringBuilder API and finally using the StringUtils class of Apache Commons library.

The String.LastIndexOf method does what you need - returns the last index of a char or string. If you're sure that every string will have at least one set of parentheses: var result = node.Text.Substring(0, node.Text.LastIndexOf

Let's say I need to split string like this: Input string: "My. name. is Bond._James Bond!" Output 2 strings: "My. name. is Bond" "_James Bond!" I tried this: int lastDotIndex = inputString.

Comments
  • This is the answer that returns the expected output. Accepted answer does not.
  • @stever Yeah, I can see that, however, maybe that fits his needs too. I think he was more interested on know how to remove FC and AFC from the end that about the expected output. Thanks for support!
  • Likewise! Still if one includes an expected output, it's nice if the accepted answer matches it. Thanks Shidersz!
  • stever actually it was returning the expected output but he edited afterwards... it doesn't matter though.
  • @skyline33 Actually, the first version of the accepted answer returns the same that the last version. However, there is no need to keep this discussion. Is all ok!