Get the Characters After the Last Index of a Substring from a String

Get the characters after the last index of a substring from a string

try this:

your cmd...|sed 's/.*\. //'

this works no matter how many "dot" or "dot and space" do you have in your input. it takes the string after the last "dot and space"

Get the characters after the last index of a substring from a string

try this:

your cmd...|sed 's/.*\. //'

this works no matter how many "dot" or "dot and space" do you have in your input. it takes the string after the last "dot and space"

Create variable from last part of string after the last / character


Easy

Each echo below will output wordpressA

strInput='apps/test/wordpress/wordpressA'   
echo "${strInput##*/}"

strInput='apps/wordpress/wordpressA'
echo "${strInput##*/}"

strInput='apps/wordpressA'
echo "${strInput##*/}"

strInput='wordpressA'
echo "${strInput##*/}"

What is the easiest way of finding the characters of a string after the last occurrence of a given character in Python?

Simply with str.rfind function (returns the highest index in the string where substring is found):

s = 'foo-bar-123-7-foo2'
res = s[s.rfind('-') + 1:]
print(res) # foo2

Get value of a string after last slash in JavaScript

At least three ways:

A regular expression:

var result = /[^/]*$/.exec("foo/bar/test.html")[0];

...which says "grab the series of characters not containing a slash" ([^/]*) at the end of the string ($). Then it grabs the matched characters from the returned match object by indexing into it ([0]); in a match object, the first entry is the whole matched string. No need for capture groups.

Live example

Using lastIndexOf and substring:

var str = "foo/bar/test.html";
var n = str.lastIndexOf('/');
var result = str.substring(n + 1);

lastIndexOf does what it sounds like it does: It finds the index of the last occurrence of a character (well, string) in a string, returning -1 if not found. Nine times out of ten you probably want to check that return value (if (n !== -1)), but in the above since we're adding 1 to it and calling substring, we'd end up doing str.substring(0) which just returns the string.

Using Array#split

Sudhir and Tom Walters have this covered here and here, but just for completeness:

var parts = "foo/bar/test.html".split("/");
var result = parts[parts.length - 1]; // Or parts.pop();

split splits up a string using the given delimiter, returning an array.

The lastIndexOf / substring solution is probably the most efficient (although one always has to be careful saying anything about JavaScript and performance, since the engines vary so radically from each other), but unless you're doing this thousands of times in a loop, it doesn't matter and I'd strive for clarity of code.

Java: Getting a substring from a string starting after a particular character


String example = "/abc/def/ghfj.doc";
System.out.println(example.substring(example.lastIndexOf("/") + 1));

Getting all characters after the last '-' in a string

No need for jQuery for the actual string manipulation - a little clunky, but easy to understand:

text = 'Something -that - has- dashes - World';
parts = text.split('-');
loc = parts.pop();
new_text = parts.join('-');

So,

loc == ' World';
new_text == 'Something -that - has- dashes ';

Whitespace can be trimmed or ignored (as it often doesn't matter inside HTML).



Related Topics



Leave a reply



Submit