Remove rest of string after the second to last index of

php get string after character
remove string after character javascript
php strip string
php get last character of string
remove specific characters from string php
javascript remove part of string
javascript remove everything before character

I'm relatively new to JS and pretty confused. I have a URL string: https://a/b/c/d/e/f. These are all dynamic characters and change often (could be https://x/s/g/e/h/d for example) In some cases I want to remove only f, in which I use LastIndexOf. In other cases I want to remove both the last and second to last EG: e/f. How can this be done successfully? I tried using a split but this just replaced the '/' with ',' for some reason.

working f example

var url = https://a/b/c/d/e/f
var new = url.substring(0, url.lastIndexOf('/') +1);

current split e/f example

var new = url.split('/')
console.log(new[new.length -2]);

This prints as: https:,,a,b,c,d,e,f,

Here you are

const str = 'https://a/b/c/d/e/f'

function removeSegments(url, times) {
    const segments = url.split('/')
    return segments.slice(0, segments.length - times).join('/')

console.log(removeSegments(str, 1)) // 'https://a/b/c/d/e'
console.log(removeSegments(str, 2)) // 'https://a/b/c/d'

Introduction to Computing & Problem Solving With PYTHON, Program str1=input(“Enter first string:”) str2=input(“Enter second string:”) new_a new string after removing the character=”,(first_part + last_part)) Output Enter a Program to change a given string to a new string where the first and last chars  The java string lastIndexOf() method returns last index of the given character value or substring. If it is not found, it returns -1. If it is not found, it returns -1. The index counter starts from zero.

Here is one option, using regex replacement. To remove the final path only:

url = "https://a/b/c/d/e/f";
url = url.replace(/\/[^/]+$/mg, "");

To remove the final two paths:

url = "https://a/b/c/d/e/f";
url = url.replace(/\/[^/]+\/[^/]+$/mg, "");

How to remove portion of a string after certain character in JavaScript, This method gets the characters in a string between “start” and “end”, excluding “​end” itself. Syntax: string.substring(start, end). Parameters: start: It is required  In above reply when first time he is calling the indexof it is taking a single parameter and that is the character which need to be found .and when he is calling that again he is also passing the start index from where the character will be searched again.

You can easily do that using the URL API.

You need to split the pathname by / and then use Array#slice to take only the parts you want. Then join those again by /.

Here is an example:

var url = new URL('https://a/b/c/d/e/f');

  url.origin +
  url.pathname.split('/').slice(0, -2).join('/')

How to remove portion of a string after a certain character in PHP , The substr() and strpos() function is used to remove portion of string after certain character. How to get the last character of a string in JavaScript? How to get  The first character in the string is H, which corresponds to the index 0. The last character is ?, which corresponds to 11. The whitespace characters also have an index, at 3 and 7. Being able to access every character in a string gives us a number of ways to work with and manipulate strings. Accessing Characters

split() creates an array of substrings that is passed in the split function. So when you write

url.split('/') it will create an array of substrings with / as delimeter.

The answer could be :

get the index of the element till when you want to remove your string by using :

var index = indexOf(x)

then pass it in the function.

var str1 = str.substr(0, index)

str1 will be your answer

substr - Manual, If length is omitted, the substring starting from start until the end of the string will THIS SCRIPT WILL RETURN PART OF STRING WITHOUT WORD BREAK ### echo substr($description,0,strpos($description,' ',30)); //strpos to find ' ' after Remove the last character of a string Second character position is 1, and so on. To remove everything before the last / input = input.Substring(input.LastIndexOf("/")); To remove everything after the last / input = input.Substring(0, input.LastIndexOf("/") + 1); An even more simpler solution for removing characters after a specified char is to use the String.Remove() method as follows: To remove everything after the first /

You can use:

var num;
for ( var i=0; i< num; i++) {
    url= url.substring(0, url.lastIndexOf('/'));

Ruby Strings Cheat Sheet, Ruby Strings cheat sheet of all shortcuts and commands. Return a new string with last character removed from str # "string" => "strin". str.insert(index, str2). Insert str2 Modify str by removing leading and trailing whitespace. Comparison I Search str2 in str and return part before it, the match, and part after in an array. Re: extract text from cell left of the second backslash thanks for the help, both answers work for exrtacting text from the left. I no longer have a requirment for getting the right side of the string.

JavaScript String lastIndexOf() Method, The lastIndexOf() method returns the position of the last occurrence of a specified value in a string. Note: The string is searched from the end to the beginning,  Found Last Index of g at : 11 3. lastIndexOf(String str) : This method accepts a String as an argument, if the string argument occurs one or more times as a substring within this object, then it returns the index of the first character of the last such substring is returned. If it does not occur as a substring, -1 is returned.

7. Strings, If you omit the second index, the slice goes to the end of the string. Thus: elif word > "banana": print "Your word, " + word + ", comes after banana." else: print  LastIndexOf() acts on instances of the string type. These can be referenced by the string keyword. There are 9 versions (overloads) of LastIndexOf.Overload. Tip: LastIndexOf internally searches the instance string from the final character backwards to the first character. Returns: If the specified value parameter is located, its index is

Strings, The expression fruit[-1] yields the last letter, fruit[-2] yields the second to last, and so on. If you omit the second index, the slice goes to the end of the string: elif word > 'banana': print 'Your word,' + word + ', comes after banana. (spaces, tabs, or newlines) from the beginning and end of a string using the strip method:

  • split just splits the string into an array of strings and not replace it
  • You probably shouldn't use new as a variable name
  • @Pete thanks, that was just a quick name used for the purpose of the question
  • This was the easiest and cleanest way for me to replicate this into my code, thank you
  • Glad it helped you!
  • Thanks for this, when I add the reg ex in, on replace(/**** it gives an error reading: unterminated Regular expression literal.
  • @Uciebila Your comment makes no sense, and I can only comment that my answer solved your problem in fewer lines of code than the answer you accepted.
  • From some research I found thatbecause my code is within a react app, it is a .js and not a .jsx. Which meant that using the above code threw an error that wouldnt be fixed until I changed my file extension, which would break the react app. With the way I added the accepted answer into my code it only needed two lines
  • Strange. It is too bad that your framework is limiting what you can and cannot do.