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

isolating a sub-string in a string before a symbol in SQL Server 2008

i am trying to extract a substring(everything before a hyphen, in this case) from a string as shown below: Net Operating Loss - 2007 Capital Loss - 1991 Foreign Tax Credit - 1997 and want the year and name(substring before hyphen) separately, using…
CarbonD1225
  • 851
  • 6
  • 24
  • 39
43
votes
9 answers

Substring index and length must refer to a location within the string

I have a string that looks like string url = "www.example.com/aaa/bbb.jpg"; "www.example.com/" is 18 fixed in length. I want to get the "aaa/bbb" part from this string (The actual url is not example nor aaa/bbb though, the length may vary) so…
Manto
  • 1,069
  • 2
  • 15
  • 34
42
votes
5 answers

Python best way to remove char from string by index

I'm removing an char from string like this: S = "abcd" Index=1 #index of string to remove ListS = list(S) ListS.pop(Index) S = "".join(ListS) print S #"acd" I'm sure that this is not the best way to do it. EDIT I didn't mentioned that I need to…
Alvaro Silvino
  • 9,441
  • 12
  • 52
  • 80
42
votes
16 answers

How to find smallest substring which contains all characters from a given string?

I have recently come across an interesting question on strings. Suppose you are given following: Input string1: "this is a test string" Input string2: "tist" Output string: "t stri" So, given above, how can I approach towards finding smallest…
Rajendra Uppal
  • 19,218
  • 15
  • 59
  • 57
42
votes
9 answers

How To Get All The Contiguous Substrings Of A String In Python?

Here is my code, but I want a better solution, how do you think about the problem? def get_all_substrings(string): length = len(string) alist = [] for i in xrange(length): for j in xrange(i,length): alist.append(string[i:j + 1]) …
lqhcpsgbl
  • 3,694
  • 3
  • 21
  • 30
41
votes
4 answers

Counting number of occurrences of a string inside another (Perl)

What is the fastest way to count the number of times a certain string appears in a bigger one? My best guess would be to replace all instances of that string with nothing, calculate the difference of lengths and divide by the length of the…
ronash
  • 856
  • 1
  • 9
  • 15
41
votes
7 answers

C++ string::find complexity

Why the c++'s implemented string::find() doesn't use the KMP algorithm (and doesn't run in O(N + M)) and runs in O(N * M)? Is that corrected in C++0x? If the complexity of current find is not O(N * M), what is that? so what algorithm is implemented…
Farzam
  • 1,272
  • 3
  • 14
  • 25
40
votes
3 answers

Given a starting and ending indices, how can I copy part of a string in C?

In C, how can I copy a string with begin and end indices, so that the string will only be partially copied (from begin index to end index)? This would be like 'C string copy' strcpy, but with a begin and an end index.
Josh Morrison
  • 7,488
  • 25
  • 67
  • 86
40
votes
3 answers

C# String.Substring equivalent for StringBuilder?

StringBuilder does not appear to have a Substring(start, len) method... what am I missing here?
James
  • 1,973
  • 1
  • 18
  • 32
39
votes
7 answers

Why doesn't string.Substring share memory with the source string?

As we all know, strings in .NET are immutable. (Well, not 100% totally immutable, but immutable by design and used as such by any reasonable person, anyway.) This makes it basically OK that, for example, the following code just stores a reference to…
Dan Tao
  • 125,917
  • 54
  • 300
  • 447
39
votes
6 answers

How do I remove a substring after a certain character in a string using Ruby?

How do I remove a substring after a certain character in a string using Ruby?
JayX
  • 1,754
  • 1
  • 18
  • 27
38
votes
17 answers

Replace nth occurrence of substring in string

I want to replace the n'th occurrence of a substring in a string. There's got to be something equivalent to what I WANT to do which is mystring.replace("substring", 2nd) What is the simplest and most Pythonic way to achieve this? Why not duplicate:…
aleskva
  • 1,644
  • 2
  • 21
  • 40
37
votes
6 answers

Find all locations of substring in NSString (not just first)

There is a substring that occurs in a string several times. I use rangeOfString, but it seems that it can only find the first location. How can I find all the locations of the substring? NSString *subString1 = @""; NSString *subString2 =…
forest
  • 411
  • 1
  • 4
  • 6
37
votes
3 answers

Slice a string in groovy

I have a 18 character string I want characters 2-8 from. In python I can do this: sliceMe = "nnYYYYYYnnnnnnnnnn" print sliceMe[2:8] prints YYYYYY I am looking for a way to do this same thing in groovy, and every explanation is REALLY long. Whats…
Mikey
  • 4,692
  • 10
  • 45
  • 73
37
votes
4 answers

How to extract first 8 characters from a string in pandas

I have column in a dataframe and i am trying to extract 8 digits from a string. How can I do it Input Shipment…
Rahul rajan
  • 1,186
  • 4
  • 18
  • 32