Questions tagged [substring]

Part of a string, or the function/method that returns part of a string

substring functions/methods come in basically two flavours:

  • The parameters to the function are start index and length.
    • SQL's version uses this flavour, notably using one-based indexing
  • The parameters to the function are start index and end index. C like languages (including Java) use this flavour, and all use zero-based indexing

Definition

  • A substring is a string contained in a given string.

  • The substring can start at any index of the given string and finish in any index within the given string following the start.

  • It can be an empty string, the entire given string, or (most likely) a shorter string completely encompassed by the given string.

See Also

9558 questions
175
votes
9 answers

Get Substring - everything before certain char

I'm trying to figure out the best way to get everything before the - character in a string. Some example strings are below. The length of the string before - varies and can be any length 223232-1.jpg 443-2.jpg 34443553-5.jpg so I need the value…
PositiveGuy
  • 46,620
  • 110
  • 305
  • 471
169
votes
8 answers

How to split a string at the first `/` (slash) and surround part of it in a ``?

I want to format this date:
23/05/2013
. First I want to split the string at the first / and have the rest in the next line. Next, I’d like to surround the first part in a tag, as follows:
Mustapha Aoussar
  • 5,833
  • 15
  • 62
  • 107
169
votes
1 answer

Find substring in the string in TWIG

I want to find substring of the string or check if there is no such substring using Twig. On the words, I need analogue of 'strstr' or 'strpos' in php. I googled and searched this issue in stackoverflow but nothing found. Does someone know how to…
user1440167
  • 1,913
  • 2
  • 12
  • 12
161
votes
10 answers

How to replace case-insensitive literal substrings in Java

Using the method replace(CharSequence target, CharSequence replacement) in String, how can I make the target case-insensitive? For example, the way it works right now: String target = "FooBar"; target.replace("Foo", "") // would return "Bar" String…
J. Lin
  • 2,259
  • 5
  • 20
  • 17
161
votes
19 answers

How to split the name string in mysql?

How to split the name string in mysql ? E.g.: name ----- Sachin ramesh tendulkar Rahul dravid Split the name like firstname,middlename,lastname: firstname middlename lastname --------- ------------ ------------ sachin ramesh …
Madhav
  • 2,285
  • 3
  • 17
  • 16
154
votes
5 answers

Extract a substring from a string in Ruby using a regular expression

How can I extract a substring from within a string in Ruby? Example: String1 = " " I want to extract substring from String1 (i.e. everything within the last occurrence of < and >).
Madhusudhan
  • 8,374
  • 12
  • 47
  • 68
147
votes
27 answers

Shorten string without cutting words in JavaScript

I'm not very good with string manipulation in JavaScript, and I was wondering how you would go about shortening a string without cutting any word off. I know how to use substring, but not indexOf or anything really well. Say I had the following…
Josh Bedo
  • 3,410
  • 3
  • 21
  • 33
143
votes
5 answers

How can I replace a regex substring match in Javascript?

var str = 'asd-0.testing'; var regex = /asd-(\d)\.\w+/; str.replace(regex, 1); That replaces the entire string str with 1. I want it to replace the matched substring instead of the whole string. Is this possible in Javascript?
dave
  • 7,717
  • 19
  • 68
  • 100
138
votes
19 answers

Replace substring with another substring C++

How could I replace a substring in a string with another substring in C++, what functions could I use? eg: string test = "abc def abc def"; test.replace("abc", "hij").replace("def", "klm"); //replace occurrence of abc and def with other substring
Steveng
  • 2,385
  • 6
  • 23
  • 20
134
votes
3 answers

Why is String.prototype.substr() deprecated?

It is mentioned on the ECMAScript standard here that : ... These features are not considered part of the core ECMAScript language. Programmers should not use or assume the existence of these features and behaviours when writing new ECMAScript code.…
ElJackiste
  • 4,011
  • 5
  • 23
  • 36
125
votes
1 answer

Python: How to check a string for substrings from a list?

How can I check a string for substrings contained in a list, like in Check if a string contains an element from a list (of strings), but in Python?
user1045620
  • 1,253
  • 2
  • 9
  • 4
123
votes
22 answers

Extract only right most n letters from a string

How can I extract a substring which is composed of the rightmost six letters from another string? Ex: my string is "PER 343573". Now I want to extract only "343573". How can I do this?
Shyju
  • 214,206
  • 104
  • 411
  • 497
121
votes
12 answers

How to remove first 10 characters from a string?

How to ignore the first 10 characters of a string? Input: str = "hello world!"; Output: d!
csharper
  • 1,313
  • 3
  • 9
  • 7
119
votes
7 answers

Time complexity of Java's substring()

What is the time complexity of the String#substring() method in Java?
TimeToCodeTheRoad
  • 7,032
  • 16
  • 57
  • 70
117
votes
14 answers

Display only 10 characters of a long string?

How do I get a long text string (like a querystring) to display a maximum of 10 characters, using JQuery? Sorry guys I'm a novice at JavaScript & JQuery :S Any help would be greatly appreciated.
Nasir
  • 4,785
  • 10
  • 35
  • 39