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
24
votes
2 answers

Is Javascript substring virtual?

If we have a huge string, named str1, say 5 million characters long, and then str2 = str1.substr(5555, 100) so that str2 is 100 characters long and is a substring of str1 starting at 5555 (or any other randomly selected position). How JavaScript…
exebook
  • 32,014
  • 33
  • 141
  • 226
24
votes
2 answers

Get last character of a string from a variable

I am having issues with a problem accomplished very easily in most languages but I can't seem to figure it out in batch. I want to extract the last character of a string. In pseudo code.. if var1.substring(var1.length, -1) = "0" do something In…
mgrenier
  • 1,409
  • 3
  • 21
  • 45
24
votes
6 answers

string.substring vs string.take

If you want to only take a part of a string, the substring method is mostly used. This has a drawback that you must first test on the length of the string to avoid errors. For example you want to save data into a database, and want to cut off a…
Williams
  • 741
  • 1
  • 4
  • 11
24
votes
6 answers

Find substring in a list of strings

I have a list like so and I want to be able to search within this list for a substring coming from another string. Example: List list = new List(); string srch = "There"; list.Add("1234 - Hello"); list.Add("4234 -…
user1380621
  • 243
  • 1
  • 2
  • 4
23
votes
6 answers

Check if variable starts with 'http'

I'm sure this is a simple solution, just haven't found exactly what I needed. Using php, i have a variable $source. I wanna check if $source starts with 'http'. if ($source starts with 'http') { $source = "
Andelas
  • 2,022
  • 7
  • 36
  • 45
23
votes
3 answers

Getting a substring of an attribute in XPATH

Given How do I all but the last 4 characters of @baz? One of my attempts was: /foo/bar/@baz[substring( ., 0, -4 )]
Kit Sunde
  • 35,972
  • 25
  • 125
  • 179
23
votes
3 answers

Pandas: replace substring in string

I want to replace substring icashier.alipay.com in column in df url icashier.alipay.com/catalog/2758186/detail.aspx icashier.alipay.com/catalog/2758186/detail.aspx icashier.alipay.com/catalog/2758186/detail.aspx vk.com to aliexpress.com. Desire…
NineWasps
  • 2,081
  • 8
  • 28
  • 45
23
votes
13 answers

mask all digits except first 6 and last 4 digits of a string( length varies )

I have a card number as a string, for example: string ClsCommon.str_CardNumbe r = "3456123434561234"; The length of this card number can vary from 16 to 19 digits, depending on the requirement. My requirement is that I have to show the first six…
Kartiikeya
  • 2,358
  • 7
  • 33
  • 68
23
votes
4 answers

JavaScript text between double quotes

I would like to get the text between double quotes using JavaScript. I found online something like title.match(/".*?"/); but the thing is that sometimes I have text between double quotes but sometimes there are no quotes. What I am saying is that…
Teo
  • 3,394
  • 11
  • 43
  • 73
23
votes
2 answers

In Java, can you use negative one with substring?

Can negative one be used as the end index for string.substring in Java? Example: String str = "test"; str.substring(0, str.indexOf("q")); Edit: Nowhere in the javadocs does it say directly that endindex cannot be negative. There are implementations…
Slight
  • 1,541
  • 3
  • 19
  • 38
23
votes
3 answers

PHP: Is it possible to correctly SUBSTR a UTF-8 string?

I have (in an SQLite database) the following string: Лампа в вытяжке на кухне меняется, начиная с вытаскивания белого штырька справа. The string is correctly shown by PHP using print. I would like to obtain just the first 50 chars of this string,…
texnic
  • 3,959
  • 4
  • 42
  • 75
23
votes
6 answers

Replacing a substring of a string with Python

I'd like to get a few opinions on the best way to replace a substring of a string with some other text. Here's an example: I have a string, a, which could be something like "Hello my name is $name". I also have another string, b, which I want to…
kwikness
  • 1,425
  • 4
  • 21
  • 37
22
votes
1 answer

Getting index of a character in NSString with offset & using substring in Objective-C

I have a string! NSString *myString=[NSString stringWithFormat:@"This is my lovely string"]; What I want to do is: Assuming the first character in the string is at index 0. Go to the 11th character (That is 'l' in the above case), and find the…
Umair Khan Jadoon
  • 2,874
  • 11
  • 42
  • 63
22
votes
1 answer

SQL Server: How to group by substring

I have the following stored procedure to fetch data from a table. The table has a column "region" that contains value like "APAC: China" etc. for which I am using the substring function in order to remove the : and everything after it. The below…
user2571510
  • 11,167
  • 39
  • 92
  • 138
22
votes
9 answers

Find all substrings that are palindromes

If the input is 'abba' then the possible palindromes are a, b, b, a, bb, abba. I understand that determining if string is palindrome is easy. It would be like: public static boolean isPalindrome(String str) { int len = str.length(); for(int i=0;…
Himanshu Yadav
  • 13,315
  • 46
  • 162
  • 291