Select the Letters After - in a String

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);
}

SQL Select everything after character

You can use:

select right(col, charindex('-', reverse(col)) - 1)

How do I select 3 character in a string after a specific symbol

Given:

examples=[ "BAHDGF - ZZZGH1237484", "HDG54 - ZZZ1HDGET4" ]

You could use a regex:

examples.each {|e| p e, e[/(?<=-\s)ZZZ/]}

Prints:

"BAHDGF - ZZZGH1237484"
"ZZZ"
"HDG54 - ZZZ1HDGET4"
"ZZZ"

Or .split with a regex:

examples.each {|e| p e.split(/-\s*(ZZZ)/)[1] }
'ZZZ'
'ZZZ'

If the 3 characters are something other than 'ZZZ' just modify your regex:

> "BAHDGF - ABCGH1237484".split(/\s*-\s*([A-Z]{3})/)[1]
=> "ABC"

If you wanted to use .partition it is two steps. Easiest with a regex partition and then just take the first three characters:

> "BAHDGF - ABCGH1237484".partition(/\s*-\s*/)[2][0..2]
=> "ABC"

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"));

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.

Get string after character

For the text after the first = and before the next =

cut -d "=" -f2 <<< "$your_str"

or

sed -e 's#.*=\(\)#\1#' <<< "$your_str"

For all text after the first = regardless of if there are multiple =

cut -d "=" -f2- <<< "$your_str"

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);


Related Topics



Leave a reply



Submit