Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I extract a substring from a string until the second space is encountered?

I have a string like this:

"o1 1232.5467 1232.5467 1232.5467 1232.5467 1232.5467 1232.5467"

How do I extract only "o1 1232.5467"?

The number of characters to be extracted are not the same always. Hence, I want to only extract until the second space is encountered.

like image 570
gbprithvi Avatar asked Apr 08 '10 12:04

gbprithvi


People also ask

How do I extract text before second space in Excel?

Explanation of the formula:FIND("#",SUBSTITUTE(A2," ","#",2))-1: The FIND function will get the position of the # character within the text string returned by the SUBSTITUTE function, subtracting 1 to get the position before the second space character in text.

How do I extract a string before space?

You can quickly extract the text before space from the list only by using formula. Select a blank cell, and type this formula =LEFT(A1,(FIND(" ",A1,1)-1)) (A1 is the first cell of the list you want to extract text) , and press Enter button.

How do you extract a certain part of a string?

The substr() method extracts a part of a string. The substr() method begins at a specified position, and returns a specified number of characters. The substr() method does not change the original string. To extract characters from the end of the string, use a negative start position.


1 Answers

A straightforward approach would be the following:

string[] tokens = str.Split(' '); string retVal = tokens[0] + " " + tokens[1]; 
like image 169
Sorantis Avatar answered Oct 04 '22 16:10

Sorantis