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
65
votes
12 answers

Swift Get string between 2 strings in a string

I am getting a string from html parse that is; string = "javascript:getInfo(1,'Info/99/something', 'City Hall',1, 99);" my code is something like var startIndex = text.rangeOfString("'") var endIndex = text.rangeOfString("',") var range2 =…
Alp
  • 1,863
  • 1
  • 20
  • 38
65
votes
2 answers

Simple way to check if a string contains another string in C?

I'm pretty new to the language. Let's say I have a string from an HTTP request, such as char * request = "GET /favicon.ico HTTP/1.1"; And I specifically want to know if favicon is in that request, perhaps with a boolean value. What is a relatively…
iaacp
  • 4,655
  • 12
  • 34
  • 39
62
votes
12 answers

Javascript: Returning the last word in a string

right to it: I have a words string which has two words in it, and i need to return the last word. They are seperated by a " ". How do i do this? function test(words) { var n = words.indexOf(" "); var res = words.substring(n+1,-1); return…
Andrew P
  • 1,487
  • 5
  • 17
  • 18
62
votes
6 answers

How to replace a substring of a string

Assuming I have a String string like this: "abcd=0; efgh=1" and I want to replace "abcd" by "dddd". I have tried to do such thing: string.replaceAll("abcd","dddd"); It does not work. Any suggestions? EDIT: To be more specific, I am working in…
MichalB
  • 3,281
  • 6
  • 32
  • 46
61
votes
4 answers

How to find if an array contains a string

Possible Duplicate: How to search for string in MS Access VBA array I am currently working on an Excel macro, and I could not find a way to do like if array.contains(mystring) I wrote the following, and it gives me the message "Invaild Qualifier"…
Nicola-V
  • 633
  • 1
  • 5
  • 6
61
votes
5 answers

Find dictionary items whose key matches a substring

I have a large dictionary constructed like so: programs['New York'] = 'some values...' programs['Port Authority of New York'] = 'some values...' programs['New York City'] = 'some values...' ... How can I return all elements of programs whose key…
Abid A
  • 7,588
  • 4
  • 32
  • 32
60
votes
7 answers

Need to get a string after a "word" in a string in c#

i'm having a string in c# for which i have to find a specific word "code" in the string and have to get the remaining string after the word "code". The string is "Error description, code : -1" so i have to find the word code in the above string…
Narayan
  • 1,189
  • 6
  • 15
  • 33
59
votes
13 answers

Number of occurrences of a substring in an NSString?

How can I get the number of times an NSString (for example, @"cake") appears in a larger NSString (for example, @"Cheesecake, apple cake, and cherry pie")? I need to do this on a lot of strings, so whatever method I use would need to be relatively…
igul222
  • 8,557
  • 14
  • 52
  • 60
58
votes
3 answers

How to match a pattern given in a variable in awk?

I want to extract a substring where certain pattern exist from pipe separated file, thus I used below command, awk -F ":" '/REWARD REQ. SERVER HEADERS/{print $1, $2, $3, $4}' sample_profile.txt Here, 'REWARD REQ. SERVER HEADERS' is a pattern which…
Chintamani Manjare
  • 1,543
  • 1
  • 13
  • 28
58
votes
8 answers

Which String method: "contains" or "indexOf > -1"?

Which of the following ways is an efficient way of determining substring containment? if (str.indexOf("/") > -1) or if (str.contains("/"))
sashank
  • 1,531
  • 2
  • 13
  • 26
55
votes
8 answers

SQL SELECT everything after a certain character

I need to extract everything after the last '=' (http://www.domain.com?query=blablabla - > blablabla) but this query returns the entire strings. Where did I go wrong in here: SELECT RIGHT(supplier_reference, CHAR_LENGTH(supplier_reference) -…
popkutt
  • 949
  • 3
  • 10
  • 19
54
votes
18 answers

A SQL Query to select a string between two known strings

I need a SQL query to get the value between two known strings (the returned value should start and end with these two strings). An example. "All I knew was that the dog had been very bad and required harsh punishment immediately regardless of what…
coopertkm
  • 727
  • 2
  • 7
  • 10
52
votes
4 answers

Faster way to read fixed-width files

I work with a lot of fixed width files (i.e., no separating character) that I need to read into R. So, there is usually a definition of the column width to parse the string into variables. I can use read.fwf to read in the data without a problem. …
Mark Danese
  • 2,331
  • 1
  • 20
  • 25
51
votes
12 answers

How to select first 10 words of a sentence?

How do I, from an output, only select the first 10 words?
AAA
  • 3,120
  • 11
  • 53
  • 71
50
votes
5 answers

LINQ: Group by month and year within a datetime field

I have a table with a datetime field. I want to retrieve a result set grouped by the month/year combination and the number of records that appear within that month/year. How can this be done in LINQ? The closest I've been able to figure out is in…
tsilb
  • 7,977
  • 13
  • 71
  • 98