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
13
votes
3 answers

Pascal substr equivalent

I was looking for a Pascal equivalent for (for example) the php's substr function, which works like this: $new_string = substr('abcdef', 1, 3); // returns 'bcd' I've already found it, but I always take excessively long to do so, so I'm posting the…
Michael Konečný
  • 1,696
  • 1
  • 16
  • 20
10
votes
1 answer

regex string and substring

I have a character string 'aabaacaba'. Starting from left, I am trying to get substrings of all sizes >=2, which appear later in the string. For instance, aa appears again in the string and so is the case with ab. I wrote following regex…
Sumit
  • 2,242
  • 4
  • 25
  • 43
10
votes
3 answers

How efficient is PHP's substr?

I'm writing a parser in PHP which must be able to handle large in-memory strings, so this is a somewhat important issue. (ie, please don't "premature optimize" flame me, please) How does the substr function work? Does it make a second copy of the…
zildjohn01
  • 11,339
  • 6
  • 52
  • 58
9
votes
5 answers

PHP limit text string NOT including html tags?

Here's what's NOT working for me: Jack and I love him very much because he\'s my favorite dog in the whole wide world and nothing could make me not love him, I…
Jamie Carter
  • 127
  • 1
  • 1
  • 8
9
votes
4 answers

Substring of a std::string in utf-8? C++11

I need to get a substring of the first N characters in a std::string assumed to be utf8. I learned the hard way that .substr does not work... as... expected. Reference: My strings probably look like this: mission:\n\n1億2千万匹
Jonny
  • 15,955
  • 18
  • 111
  • 232
9
votes
2 answers

Case insensitive standard string comparison in C++

void main() { std::string str1 = "abracadabra"; std::string str2 = "AbRaCaDaBra"; if (!str1.compare(str2)) { cout << "Compares" } } How can I make this work? Bascially make the above case insensitive. Related question I…
Santhosh Kumar
  • 381
  • 1
  • 5
  • 13
9
votes
2 answers

Split string using loop to specific length sub-units

I need to split a string into specific lengths, e.g. if user specifies it to units of max length 4, then the loop should run on the original input "0123456789asdf" to get "0123", "4567", "89as", "df". I can't really figure out the best way to do…
sccs
  • 1,123
  • 3
  • 14
  • 27
9
votes
2 answers

Why does substr not return undef at the end of a string?

I'm not sure whether this is defined behaviour or not. I have the following code: use strict; use warnings; use Data::Dumper; my $string = 'aaaaaa0aaaa'; my $char = substr($string, length($string), 1); my $char2 = substr($string, length($string)+1,…
mpe
  • 1,000
  • 1
  • 8
  • 25
8
votes
3 answers

How to prevent showing the diamond question mark symbol, even using mb_substr and utf-8

I have read some other questions, tried the answers but got no result at the end. What I get is for example this Μήπως θα έπρεπε να � ... and I can't remove that weird question mark. What I do is to get the content of an RSS feed that is encoded…
EnexoOnoma
  • 8,454
  • 18
  • 94
  • 179
8
votes
3 answers

Using substr() in multiple languages including arabic language with php

I know there are a lot of questions already posted with arabic language in php, but I was unable to get solution to my problem and hence I am posting this question: I have a PhP site that is running perfectly in English language. Now, I want it to…
Nitesh
  • 2,286
  • 2
  • 43
  • 65
8
votes
2 answers

Extract first X digits of N digit numbers

How to select first 2 digits of a number? I just need the name of the function Example: 12455 turns into 12, 13655 into 13 Basically it's the equivalent of substring for integers.
8
votes
6 answers

javascript grab part of a string after a . (dot)

I'm unable to read regex. Let's say we have this string: "mydomain.bu.pu" and I want to grab the ".bu.pu" part of it; I'm thinking about using something like: indexOf and later substr ... But I confess I'm kind of lost... Any help please? :D Thanks…
MEM
  • 30,529
  • 42
  • 121
  • 191
8
votes
1 answer

Why does std::string::substr throw an exception instead of returning an empty string?

I have been wondering about the rationale behind the design of std::string's substr(pos, len) method for a while now. It still does not make sense to me, so I decided to ask the experts. The function throws a std::out_of_range exception if the pos…
tglas
  • 949
  • 10
  • 19
8
votes
3 answers

R: how to display the first n characters from a string of words

I have the following string: Getty <- "Four score and seven years ago our fathers brought forth on this continent a new nation, conceived in liberty, and dedicated to the proposition that all men are created equal." I want to display the first 10…
mapleleaf
  • 758
  • 3
  • 8
  • 14
8
votes
4 answers

PHP get everything in a string before underscore

I have this code here: $imagePreFix = substr($fileinfo['basename'], strpos($fileinfo['basename'], "_") +1); this gets me everything after the underscore, but I am looking to get everything before the underscore, how would I adjust this code to get…
user3723240
  • 395
  • 3
  • 11
  • 29
1 2
3
95 96