How to get everything after a certain character?
The strpos()
finds the offset of the underscore, then substr grabs everything from that index plus 1, onwards.
$data = "123_String";
$whatIWant = substr($data, strpos($data, "_") + 1);
echo $whatIWant;
If you also want to check if the underscore character (_
) exists in your string before trying to get it, you can use the following:
if (($pos = strpos($data, "_")) !== FALSE) {
$whatIWant = substr($data, $pos+1);
}
Get everything after the dash in a string in JavaScript
How I would do this:
// function you can use:
function getSecondPart(str) {
return str.split('-')[1];
}
// use the function:
alert(getSecondPart("sometext-20202"));
javascript: get everything after certain characters from a string?
Split separates the string into tokens separated by the delimiter. It always returns an array one longer than the number of tokens in the string. If there is one delimiter, there are two tokens—one to the left and one to the right. In your case, the token to the left is the empty string, so split()
returns the array ["", "mysite.com/username_here80"]
. Try using
var urlsplit = url.split("mycool://?string=")[1]; // <= Note the [1]!
to retrieve the second string in the array (which is what you are interested in).
The reason you are getting a comma is that converting an array to a string (which is what alert()
does) results in a comma-separated list of the array elements converted to strings.
SQL Select everything after character
You can use:
select right(col, charindex('-', reverse(col)) - 1)
Get everything after and before certain character in SQL Server
If you want to get this out of your table using SQL, take a look at the following functions that will help you: SUBSTRING
and CHARINDEX
. You can use those to trim your entries.
A possible query will look like this (where col
is the name of the column that contains your image directories:
SELECT SUBSTRING(col, LEN(SUBSTRING(col, 0, LEN(col) - CHARINDEX ('/', col))) + 1,
LEN(col) - LEN(SUBSTRING(col, 0, LEN(col) - CHARINDEX ('/', col))) - LEN(SUBSTRING(
col, CHARINDEX ('.', col), LEN(col))));
Bit of an ugly beast. It also depends on the standard format of 'dir/name.ext'.
Edit:
This one (inspired by praveen) is more generic and deals with extensions of different length:
SELECT SUBSTRING(col, LEN(LEFT(col, CHARINDEX ('/', col))) + 1, LEN(col) - LEN(LEFT(col,
CHARINDEX ('/', col))) - LEN(RIGHT(col, LEN(col) - CHARINDEX ('.', col))) - 1);
Get everything after first character
Use indexOf(...)
instead of lastIndexOf(...)
If you want to include the ":"
then do not add one to the index.
Like this:
var val = 'asdasd:111122:123123123';
var response = val.substring(val.indexOf(":"));
alert(response); // ":111122:123123123"
How to extract everything after a specific string?
With str_extract
. \\b
is a zero-length token that matches a word-boundary. This includes any non-word characters:
library(stringr)
str_extract(test, '\\b\\w+$')
# [1] "Pomme" "Poire" "Fraise"
We can also use a back reference with sub
. \\1
refers to string matched by the first capture group (.+)
, which is any character one or more times following a -
at the end:
sub('.+-(.+)', '\\1', test)
# [1] "Pomme" "Poire" "Fraise"
This also works with str_replace
if that is already loaded:
library(stringr)
str_replace(test, '.+-(.+)', '\\1')
# [1] "Pomme" "Poire" "Fraise"
Third option would be using strsplit
and extract the second word from each element of the list (similar to word
from @akrun's answer):
sapply(strsplit(test, '-'), `[`, 2)
# [1] "Pomme" "Poire" "Fraise"
stringr
also has str_split
variant to this:
str_split(test, '-', simplify = TRUE)[,2]
# [1] "Pomme" "Poire" "Fraise"
How to get a string after a specific substring?
The easiest way is probably just to split on your target word
my_string="hello python world , i'm a beginner"
print(my_string.split("world",1)[1])
split takes the word (or character) to split on and optionally a limit to the number of splits.
In this example, split on "world" and limit it to only one split.
Related Topics
PHP Is Confused When Adding and Concatenating
Fastest Way to Convert String to Integer in PHP
Page Redirect After Certain Time PHP
PHP - How to Best Determine If the Current Invocation Is from Cli or Web Server
Composer Warning: Openssl Extension Is Missing. How to Enable in Wamp
How to Detect Strings Like Putjbtghguhjjjanika
How to Split a String by Multiple Delimiters in PHP
If Statement in the Middle of Concatenation
PHP 5 Strpos() Difference Between Returning 0 and False
Upload Multiple Files With PHP and Jquery
Restructure Multidimensional Array of Column Data into Multidimensional Array of Row Data
Switch PHP Versions on Commandline Ubuntu 16.04
How to Check If a String Starts With a Specified String