Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I split a string ONLY after the first instance of the delimiter?

Tags:

string

split

vba

I have this code:

strInfo = "3101234567 Ryan Maybach"

Dim varSplit As Variant
varSplit = Split(strInfo, " ")

strPhoneNumber = varSplit(0)
strOwner = varSplit(1)

So, strPhoneNumber = "3101234567" and strOwner = "Ryan"

I want to make it so that strOwner = "Ryan Maybach", the full name, and not just the first name.

How do I split the strInfo string at the first instance of a space " "?

like image 606
phan Avatar asked Feb 27 '12 04:02

phan


People also ask

How do you split a string with the first occurrence of a character in python?

Use the str. split() method with maxsplit set to 1 to split a string on the first occurrence, e.g. my_str. split('-', 1) . The split() method only performs a single split when the maxsplit argument is set to 1 .

How do you separate a string from a delimiter?

You can use the split() method of String class from JDK to split a String based on a delimiter e.g. splitting a comma-separated String on a comma, breaking a pipe-delimited String on a pipe, or splitting a pipe-delimited String on a pipe.

How do you split a string with first comma?

To split a string with comma, use the split() method in Java. str. split("[,]", 0);

How do I split a string by any whitespace?

You can split a String by whitespaces or tabs in Java by using the split() method of java. lang. String class. This method accepts a regular expression and you can pass a regex matching with whitespace to split the String where words are separated by spaces.


1 Answers

From the MSDN documentation on the Split function:

By default, or when Limit equals -1, the Split function splits the input string at every occurrence of the delimiter string, and returns the substrings in an array. When the Limit parameter is greater than zero, the Split function splits the string at the first Limit-1 occurrences of the delimiter, and returns an array with the resulting substrings.

If you only want to split on the first delimiter then you would specify 2 as the maximum number of parts.

Split(expression, [ delimiter, [ limit, [ compare ]]])
like image 77
Stephen Tetreault Avatar answered Oct 19 '22 22:10

Stephen Tetreault