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
50
votes
7 answers

Finding a substring in a NSString object

I have an NSString object and I want to make a substring from it, by locating a word. For example, my string is: "The dog ate the cat", I want the program to locate the word "ate" and make a substring that will be "the cat". Can someone help me out…
Sagiftw
  • 1,658
  • 4
  • 21
  • 25
49
votes
3 answers

How can I get a substring from position N to the last char in Ruby?

I'd like to get a substring from a string from position N to the end of the string. What's the way to do it in Ruby?
Only Bolivian Here
  • 35,719
  • 63
  • 161
  • 257
49
votes
5 answers

Check if string contains any substring in an array in Ruby

I am using the Tmail library, and for each attachment in an email, when I do attachment.content_type, sometimes I get not just the content type but also the name. Examples: image/jpeg; name=example3.jpg image/jpeg; name=example.jpg image/jpeg;…
Hommer Smith
  • 26,772
  • 56
  • 167
  • 296
47
votes
2 answers

Split String by delimiter position using oracle SQL

I have a string and I would like to split that string by delimiter at a certain position. For example, my String is F/P/O and the result I am looking for is: Therefore, I would like to separate the string by the furthest delimiter. Note: some of my…
Avinesh Kumar
  • 1,389
  • 2
  • 17
  • 26
46
votes
9 answers

Is there a method like JavaScript's substr in Rust?

I looked at the Rust docs for String but I can't find a way to extract a substring. Is there a method like JavaScript's substr in Rust? If not, how would you implement it? str.substr(start[, length]) The closest is probably slice_unchecked but it…
laktak
  • 57,064
  • 17
  • 134
  • 164
46
votes
8 answers

How to use string.substr() function?

I want to make a program that will read some number in string format and output it like this: if the number is 12345 it should then output 12 23 34 45 . I tried using the substr() function from the c++ string library, but it gives me strange results…
VaioIsBorn
  • 7,683
  • 9
  • 31
  • 28
45
votes
5 answers

Ignore files with names starting with 'output'

I have a program that generates text files output1.txt, output2.txt, output3.txt, etc.. I want Git to ignore these files. I can't block text files, as I have some text files that shouldn't be ignored. Also, the files are dynamically generated (there…
user4302594
44
votes
5 answers

.substring error: "is not a function"

I don't understand why I get an error message using the substring method to declare a variable. I want to use the first part of the URL in a comparison. Site: http://www.elizabet.nl/wordpress This is the part that's going wrong: var currentLocation…
Hannemaatje
  • 473
  • 1
  • 4
  • 9
44
votes
5 answers

How to return part of string before a certain character?

If you look at the jsfiddle from question, var str = "Abc: Lorem ipsum sit amet"; str = str.substring(str.indexOf(":") + 1); This returns all characters after the :, how can I adjust this to return all the characters before the : something like var…
Smudger
  • 10,451
  • 29
  • 104
  • 179
44
votes
1 answer

Find all possible substring in fastest way

For String A = "abcd" then answer should be {a,ab,abc,abcd,b,bc,bcd,c,cd,d} To find all the substring I have used following method for (int i = 0; i < A.length(); i++) { for (int j = i+1; j <= A.length(); j++) { …
user2228588
  • 443
  • 1
  • 4
  • 7
44
votes
6 answers

Ruby idiom for substring from index until end of string

Just wondering if there's a Ruby idiom for extracting a substring from an index until the end of the string. I know of str[index..-1] which works by passing in a range object to the String's [] method but it's a little clunky. In python, for…
Sherwin Yu
  • 3,180
  • 2
  • 25
  • 41
44
votes
6 answers

How to delete a substring using shell script

I have strings called: abc.out def.out How do I delete the substring .out In these strings? What command should I use? (Bourne Shell)
J0natthaaann
  • 555
  • 1
  • 6
  • 11
43
votes
4 answers

Conditional substring in a Jinja2 template

When deploying with ansible, There's 1 specific case where I need to strip a string of a trailing -p substring. The string somemachine-prod-p should become somemachine-prod only if the -p is at the end. The substring function I saw I can use with…
Moshe
  • 4,635
  • 6
  • 32
  • 57
43
votes
7 answers

selecting an array key based on partial string

I have an array and in that array I have an array key that looks like, show_me_160 this array key may change a little, so sometimes the page may load and the array key maybe show_me_120, I want to now is possible to just string match the array key…
sea_1987
  • 2,902
  • 12
  • 44
  • 69
43
votes
3 answers

Splitting a file name into name,extension

I have the name of a file like this: name1.csv and I would like to extract two substrings of this string. One that stores the name1 in one variable and other that stores the extension, csv, without the dot in another variable. I have been searching…
Layla
  • 5,234
  • 15
  • 51
  • 66