Questions tagged [substr]

A function/method that returns part of a string. Use [substring] for any questions about substrings that don't specifically involve a function/method named 'substr'.

substr functions/methods are known in the following languages (alphabetical order)

The parameters are:

  • start index (usually zero-based)
  • length (optional in some languages)
1429 questions
41
votes
3 answers

Extract string before "|"

I have a data set wherein a column looks like this: ABC|DEF|GHI, ABCD|EFG|HIJK, ABCDE|FGHI|JKL, DEF|GHIJ|KLM, GHI|JKLM|NO|PQRS, BCDE|FGHI|JKL .... and so on I need to extract the characters that appear before the first | symbol. In…
29
votes
7 answers

Using PHP's substr() with special characters at the end results in question marks

When I use the substr() function in PHP, I get an question mark (a square with a question mark - depending on the browser) at the end of the string when this last character was a special one, like ë or ö, etc... $introtext =…
Bert
  • 845
  • 2
  • 7
  • 17
26
votes
7 answers

PHP function substr() error

When I use substr() I get a strange character at the end $articleText = substr($articleText,0,500); I have an output of 500 chars and � <-- How can I fix this? Is it an encoding problem? My language is Greek.
Stoikidis
  • 265
  • 1
  • 3
  • 4
24
votes
4 answers

How to retrieve the last 4 characters from a mysql database field?

In order to be able to simulate substr from PHP and mysql, I want to do something like select * from table where last_four_chars(field) = '.png'
Toni Michel Caubet
  • 19,333
  • 56
  • 202
  • 378
21
votes
5 answers

Django substr / substring in templates

Could someone tell me, does the method like substr in PHP (http://pl2.php.net/manual/en/function.substr.php) exist in Django templates?
Jazi
  • 6,569
  • 13
  • 60
  • 92
21
votes
3 answers

Can std::string overload "substr" for rvalue *this and steal resources?

It just occurred to me I noticed that std::string's substr operation could be much more efficient for rvalues when it could steal the allocated memory from *this. The Standard library of N3225 contains the following member function declaration of…
Johannes Schaub - litb
  • 496,577
  • 130
  • 894
  • 1,212
20
votes
3 answers

PHP substr but keep HTML tags?

I am wondering if there is an elegant way to trim some text but while being HTML tag aware? For example, I have this string: $data = 'some title text here that could get very long'; And let's say I need to return/output this string…
user381800
20
votes
4 answers

Put dash between every third character

I had a problem on how to put dash in every 3rd character. For example, I want ABCDEF turn into ABC-DEF I have this code: $string = 'ABCDEF'; echo substr_replace(chunk_split($string,3),'-','3','2'); // the output is ABC-DEF However this code does…
softboxkid
  • 877
  • 6
  • 13
  • 31
18
votes
5 answers

PHP substr after a certain char, a substr + strpos elegant solution?

let's say I want to return all chars after some needle char 'x' from: $source_str = "Tuex helo babe". Normally I would do this: if( ($x_pos = strpos($source_str, 'x')) !== FALSE ) $source_str = substr($source_str, $x_pos + 1); Do you know a…
Marco Demaio
  • 33,578
  • 33
  • 128
  • 159
17
votes
5 answers

What is the difference between slice() and substr() in JavaScript?

Can I ask what the difference is between string object slice() and substr() in JavaScript?
dramasea
  • 3,370
  • 16
  • 49
  • 77
16
votes
4 answers

What is the equivalent of REGEXP_SUBSTR in mysql?

I want to extract a word from a string column of a table. description =========================== abc order_id: 2 xxxx yyy aa mmm order_id: 3 nn kk yw Expected result set order_id =========================== 2 3 Table will at most have 100 rows,…
Harish Shetty
  • 64,083
  • 21
  • 152
  • 198
14
votes
4 answers

substr() from the 1. to the last character C++

I would like to use substr() function in order to get the chain of characters from the 1. to the last, without 0. Should I do sth like this: string str = xyz.substr(1, xyz.length()); or (xyz.length() - 1) ? And why?
whatfor
  • 333
  • 2
  • 3
  • 7
14
votes
4 answers

Using substr to trim string on Oracle

I want to trim a string to a specified length. If the string is shorter, I don't want to do anything. I found a function substr() which does the job. However there is nothing in the Oracle documentation what happens if the string is shorter, than…
Michal Krasny
  • 5,434
  • 7
  • 36
  • 64
14
votes
13 answers

How to get first x chars from a string, without cutting off the last word?

I have the following string in a variable. Stack Overflow is as frictionless and painless to use as we could make it. I want to fetch first 28 characters from the above line, so normally if I use substr then it will give me Stack Overflow is as…
djmzfKnm
  • 26,679
  • 70
  • 166
  • 227
13
votes
4 answers

Substring in vim script

Is there any substr() like function to get substring in vim script? If not what is the best replacement or alternative for this kind of task?
Arafat Hasan
  • 2,811
  • 3
  • 21
  • 38
1
2
3
95 96