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
268
votes
13 answers

Batch file: Find if substring is in string (not in a file)

In a batch file, I have a string abcdefg. I want to check if bcd is in the string. Unfortunately it seems all of the solutions I'm finding search a file for a substring, not a string for a substring. Is there an easy solution for this?
Ben
  • 54,723
  • 49
  • 178
  • 224
262
votes
18 answers

Replace part of a string with another string

How do I replace part of a string with another string using the standard C++ libraries? QString s("hello $name"); // Example using Qt. s.replace("$name", "Somename");
Tom Leese
  • 19,309
  • 12
  • 45
  • 70
253
votes
10 answers

Java: Getting a substring from a string starting after a particular character

I have a string: /abc/def/ghfj.doc I would like to extract ghfj.doc from this, i.e. the substring after the last /, or first / from right. Could someone please provide some help?
Sunny
  • 7,444
  • 22
  • 63
  • 104
250
votes
10 answers

Fastest way to check a string contain another substring in JavaScript?

I'm working with a performance issue on JavaScript. So I just want to ask: what is the fastest way to check whether a string contains another substring (I just need the boolean value)? Could you please suggest your idea and sample snippet code?
Đinh Hồng Châu
  • 5,300
  • 15
  • 53
  • 90
237
votes
12 answers

How do I get the last character of a string?

How do I get the last character of a string? public class Main { public static void main(String[] args) { String s = "test string"; //char lastChar = ??? } }
lonesarah
  • 3,251
  • 8
  • 24
  • 25
229
votes
2 answers

Go test string contains substring

How do I check if a string is a substring of another string in Go? For example, I want to check someString.contains("something").
Elliott Beach
  • 10,459
  • 9
  • 28
  • 41
224
votes
3 answers

What is the best way to do a substring in a batch file?

I want to get the name of the currently running batch file without the file extension. Thanks to this link, I have the file name with the extension... but what is the best way to do a substring in a batch file? Or is there another way to get the…
JosephStyons
  • 57,317
  • 63
  • 160
  • 234
216
votes
14 answers

PHP substring extraction. Get the string before the first '/' or the whole string

I am trying to extract a substring. I need some help with doing it in PHP. Here are some sample strings I am working with and the results I need: home/cat1/subcat2 => home test/cat2 => test startpage => startpage I want to get the string till…
anon355079
203
votes
17 answers

What is the fastest substring search algorithm?

OK, so I don't sound like an idiot I'm going to state the problem/requirements more explicitly: Needle (pattern) and haystack (text to search) are both C-style null-terminated strings. No length information is provided; if needed, it must be…
R.. GitHub STOP HELPING ICE
  • 208,859
  • 35
  • 376
  • 711
197
votes
39 answers

How to get a substring between two strings in PHP?

I need a function that returns the substring between two words (or two characters). I'm wondering whether there is a php function that achieves that. I do not want to think about regex (well, I could do one but really don't think it's the best way…
Nadjib Mami
  • 5,736
  • 9
  • 37
  • 49
193
votes
12 answers

Get value of a string after last slash in JavaScript

I am already trying for over an hour and cant figure out the right way to do it, although it is probably pretty easy: I have something like this : foo/bar/test.html I would like to use jQuery to extract everything after the last /. In the example…
r0skar
  • 8,338
  • 14
  • 50
  • 69
189
votes
8 answers

Extracting substrings in Go

I'm trying to read an entire line from the console (including whitespace), then process it. Using bufio.ReadString, the newline character is read together with the input, so I came up with the following code to trim the newline…
mark2222
  • 2,007
  • 2
  • 12
  • 6
180
votes
5 answers

Get a substring of a char*

For example, I have this char *buff = "this is a test string"; and want to get "test". How can I do that?
user502230
178
votes
27 answers

Find the nth occurrence of substring in a string

This seems like it should be pretty trivial, but I am new at Python and want to do it the most Pythonic way. I want to find the index corresponding to the n'th occurrence of a substring within a string. There's got to be something equivalent to what…
prestomation
  • 7,225
  • 3
  • 39
  • 37
176
votes
8 answers

warning: 'characters' is deprecated: Please use String or Substring directly

characters - an instance property of String, is deprecated from with Xcode 9.1 It was very useful to get a substring from String by using the characters property but now it has been deprecated and Xcode suggests to use substring. I've tried to check…
Krunal
  • 77,632
  • 48
  • 245
  • 261