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
1151
votes
8 answers

What is the difference between String.slice and String.substring?

Does anyone know what the difference is between these two methods? String.prototype.slice String.prototype.substring
tmim
  • 12,091
  • 4
  • 18
  • 12
501
votes
27 answers

How to trim a file extension from a String in JavaScript?

For example, assuming that x = filename.jpg, I want to get filename, where filename could be any file name (Let's assume the file name only contains [a-zA-Z0-9-_] to simplify.). I saw x.substring(0, x.indexOf('.jpg')) on DZone Snippets, but wouldn't…
ma11hew28
  • 121,420
  • 116
  • 450
  • 651
381
votes
12 answers

How to grab substring before a specified character in JavaScript?

I am trying to extract everything before the ',' comma. How do I do this in JavaScript or jQuery? I tried this and not working.. 1345 albany street, Bellevue WA 42344 I just want to grab the street address. var streetaddress= substr(addy, 0,…
Anjana Sharma
  • 4,535
  • 5
  • 37
  • 51
175
votes
9 answers

Extract a substring according to a pattern

Suppose I have a list of string: string = c("G1:E001", "G2:E002", "G3:E003") Now I hope to get a vector of string that contains only the parts after the colon ":", i.e substring = c(E001,E002,E003). Is there a convenient way in R to do this? Using…
alittleboy
  • 10,616
  • 23
  • 67
  • 107
147
votes
10 answers

Get last field using awk substr

I am trying to use awk to get the name of a file given the absolute path to the file. For example, when given the input path /home/parent/child/filename I would like to get filename I have tried: awk -F "/" '{print $5}' input which works…
Mariappan Subramanian
  • 9,527
  • 8
  • 32
  • 33
124
votes
12 answers

How do I remove the last comma from a string using PHP?

I am using a loop to get values from my database and my result is like: 'name', 'name2', 'name3', And I want it like this: 'name', 'name2', 'name3' I want to remove the comma after the last value of the loop.
JoJo
  • 1,443
  • 2
  • 12
  • 12
112
votes
6 answers

What linux shell command returns a part of a string?

I want to find a linux command that can return a part of the string. In most programming languages, it's the substr() function. Does bash have any command that can be used for this purpose. I want to be able to do something like this... substr…
Binny V A
  • 2,036
  • 3
  • 20
  • 23
85
votes
2 answers

Ruby - How to select some characters from string

I am trying to find a function for select e.g. first 100 chars of the string. In PHP, there exists the substr function Does Ruby have some similar function?
user1946705
  • 2,858
  • 14
  • 41
  • 58
77
votes
4 answers

Extract the first 2 Characters in a string

I need to extract the 1st 2 characters in a string to later create bin plot distribution. vector: x <- c("75 to 79", "80 to 84", "85 to 89") I have gotten this far: substrRight <- function(x, n){ substr(x, nchar(x)-n, nchar(x)) } invoke…
Seb
  • 979
  • 2
  • 7
  • 9
76
votes
6 answers

How to subtract 7 days from current date with Moment.js

I would like to subtract 7 days from current date to get formatted date YYYY-MM-DD using moment.js library. I tried to do by this way: dateTo = moment(new Date()).format('YYYY-MM-DD'); dateFrom = moment(new Date() -…
redrom
  • 11,502
  • 31
  • 157
  • 264
71
votes
7 answers

php substr() function with utf-8 leaves � marks at the end

Here is simple code
Nazar
  • 1,385
  • 4
  • 15
  • 18
69
votes
4 answers

How to return all except last 2 characters of a string?

id = '01d0'; document.write('
'+id.substr(0,-2)); How can I take a string like '01d0and get the01` (all except the last two chars)? In PHP I would use substr(0,-2) but this doesn't seem to work in JavaScript. How can I make this work?
Logan
  • 10,649
  • 13
  • 41
  • 54
47
votes
12 answers

Extract a substring between two characters in a string PHP

Is there a PHP function that can extract a phrase between 2 different characters in a string? Something like substr(); Example: $String = "[modid=256]"; $First = "="; $Second = "]"; $id = substr($string, $First, $Second); Thus $id would be…
Sebastian
  • 505
  • 1
  • 5
  • 7
46
votes
12 answers

Using PHP substr() and strip_tags() while retaining formatting and without breaking HTML

I have various HTML strings to cut to 100 characters (of the stripped content, not the original) without stripping tags and without breaking HTML. Original HTML string (288 characters): $content = "
With a span over…
Peter Craig
  • 7,101
  • 19
  • 59
  • 74
45
votes
6 answers

Delete first 3 characters and last 3 characters from String PHP

I need to delete the first 3 letters of a string and the last 3 letters of a string. I know I can use substr() to start at a certain character but if I need to strip both first and last characters i'm not sure if I can actually use this. Any…
Howdy_McGee
  • 10,422
  • 29
  • 111
  • 186
1
2 3
95 96