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 " "?
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 .
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.
To split a string with comma, use the split() method in Java. str. split("[,]", 0);
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.
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 ]]])
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