I have a string that has words and spaces, "2h 3m 1s". I want to extract 2h
out of it; so get everything before first space.
var date = "1,340d 1h 15m 52s" // I want to extract "1,340d".
What is the best practice of doing it? What substring function is the best approach?
SELECT SUBSTRING_INDEX(yourColumnName,' ',1) as anyVariableName from yourTableName; In the above query, if you use -1 in place of 1 then you will get the last word.
The easiest way to get the first word in string in python is to access the first element of the list which is returned by the string split() method. String split() method – The split() method splits the string into a list. The string is broken down using a specific character which is provided as an input parameter.
Method 3: Using strstr() Function: The strstr() function is used to search the first occurrence of a string inside another string. This function is case-sensitive. . strstr ( $sentence , ' ' , true);
If your string is heavy, componentsSeparatedByString()
tends to be faster.
Swift 2:
var date = "1,340d 1h 15m 52s"
if let first = date.componentsSeparatedByString(" ").first {
// Do something with the first component.
}
Swift 3/4/5:
if let first = date.components(separatedBy: " ").first {
// Do something with the first component.
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With