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

Why does substring slicing with index out of range work?

Why doesn't 'example'[999:9999] result in error? Since 'example'[9] does, what is the motivation behind it? From this behavior I can assume that 'example'[3] is, essentially/internally, not the same as 'example'[3:4], even though both result in the…
ijverig
  • 2,795
  • 3
  • 18
  • 26
112
votes
21 answers

How to find nth occurrence of character in a string?

Similar to a question posted here, am looking for a solution in Java. That is, how to find the index of nth occurrence of a character/string from a string? Example: "/folder1/folder2/folder3/". In this case, if I ask for 3rd occurrence of slash…
Gnanam
  • 10,613
  • 19
  • 54
  • 72
106
votes
11 answers

Index of a substring in a string with Swift

I'm used to do this in JavaScript: var domains = "abcde".substring(0, "abcde".indexOf("cd")) // Returns "ab" Swift doesn't have this function, how to do something similar?
Armand Grillet
  • 3,229
  • 5
  • 30
  • 60
102
votes
14 answers

How do you search an array for a substring match?

I need to search an array in JavaScript. The search would be for only part of the string to match as the string would have additional components. I would then need to return the successfully matched array element with the full string. Example: const…
reub77
  • 1,179
  • 2
  • 9
  • 12
101
votes
9 answers

javascript window location href without hash?

I have: var uri = window.location.href; That provides http://example.com/something#hash What's the best and easiest way to get the entire path without the #hash? uri = http://example.com/something#hash nohash = http://example.com/something I…
matt
  • 42,713
  • 103
  • 264
  • 397
101
votes
5 answers

Find all index position in list based on partial string inside item in list

mylist = ["aa123", "bb2322", "aa354", "cc332", "ab334", "333aa"] I need the index position of all items that contain 'aa'. I'm having trouble combining enumerate() with partial string matching. I'm not even sure if I should be using enumerate. I…
L Shaw
  • 1,067
  • 2
  • 8
  • 7
98
votes
10 answers

Finding second occurrence of a substring in a string in Java

We are given a string, say, "itiswhatitis" and a substring, say, "is". I need to find the index of 'i' when the string "is" occurs a second time in the original string. String.indexOf("is") will return 2 in this case. I want the output to be 10 in…
AmanArora
  • 2,379
  • 6
  • 19
  • 22
97
votes
20 answers

Select query to remove non-numeric characters

I've got dirty data in a column with variable alpha length. I just want to strip out anything that is not 0-9. I do not want to run a function or proc. I have a script that is similar that just grabs the numeric value after text, it looks like…
SQL_Noob
  • 1,161
  • 1
  • 9
  • 18
96
votes
7 answers

bash, extract string before a colon

If I have a file with rows like this /some/random/file.csv:some string /some/random/file2.csv:some string2 Is there some way to get a file that only has the first part before the colon, e.g. /some/random/file.csv /some/random/file2.csv I would…
user788171
  • 16,753
  • 40
  • 98
  • 125
92
votes
2 answers

Qt. get part of QString

I want to get QString from another QString, when I know necessary indexes. For example: Main string: "This is a string". I want to create new QString from first 5 symbols and get "This ". input : first and last char number. output : new QString. How…
AlekseyS
  • 921
  • 1
  • 6
  • 4
86
votes
8 answers

How to Select a substring in Oracle SQL up to a specific character?

Say I have a table column that has results like: ABC_blahblahblah DEFGH_moreblahblahblah IJKLMNOP_moremoremoremore I would like to be able to write a query that selects this column from said table, but only returns the substring up to the…
Pretzel
  • 8,141
  • 16
  • 59
  • 84
85
votes
7 answers

Delete the last two characters of the String

How can I delete the last two characters 05 of the simple string? Simple: "apple car 05" Code String[] lineSplitted = line.split(":"); String stopName = lineSplitted[0]; String stop = stopName.substring(0, stopName.length() - 1); String stopEnd =…
TheBook
  • 1,698
  • 1
  • 16
  • 32
85
votes
1 answer

Remove Trailing Slash From Batch File Input

I have a batch file that I want to improve. Instead of requiring a user to provide a folder path without a trailing slash, is there an easy way for me to just remove the last character from the path if there is a slash on the end? :START @echo What…
Brook
  • 1,095
  • 1
  • 10
  • 15
84
votes
8 answers

Limiting the number of characters in a string, and chopping off the rest

I need to create a summary table at the end of a log with some values that are obtained inside a class. The table needs to be printed in fixed-width format. I have the code to do this already, but I need to limit Strings, doubles and ints to a…
Flethuseo
  • 5,969
  • 11
  • 47
  • 71
83
votes
3 answers

How to split string into substrings on iOS?

I received an NSString from the server. Now I want to split it into the substring which I need. How to split the string? For example: substring1:read from the second character to 5th character substring2:read 10 characters from the 6th character.
Chilly Zhong
  • 16,763
  • 23
  • 77
  • 103