First Entry from String Split

First entry from string split

If you need to extract the first (or nth) entry from each split, use:

word <- c('apple-orange-strawberry','chocolate')

sapply(strsplit(word,"-"), `[`, 1)
#[1] "apple" "chocolate"

Or faster and more explictly:

vapply(strsplit(word,"-"), `[`, 1, FUN.VALUE=character(1))
#[1] "apple" "chocolate"

Both bits of code will cope well with selecting whichever value in the split list, and will deal with cases that are outside the range:

vapply(strsplit(word,"-"), `[`, 2, FUN.VALUE=character(1))
#[1] "orange" NA

split string only on first instance of specified character

Use capturing parentheses:

'good_luck_buddy'.split(/_(.*)/s)
['good', 'luck_buddy', ''] // ignore the third element

They are defined as

If separator contains capturing parentheses, matched results are returned in the array.

So in this case we want to split at _.* (i.e. split separator being a sub string starting with _) but also let the result contain some part of our separator (i.e. everything after _).

In this example our separator (matching _(.*)) is _luck_buddy and the captured group (within the separator) is lucky_buddy. Without the capturing parenthesis the luck_buddy (matching .*) would've not been included in the result array as it is the case with simple split that separators are not included in the result.

We use the s regex flag to make . match on newline (\n) characters as well, otherwise it would only split to the first newline.

Split string and get first value only

string valueStr = "title, genre, director, actor";
var vals = valueStr.Split(',')[0];

vals will give you the title

How to get the first element of a string split with space

Use bash parameter-expansion without needing to use arrays,

myUser=$(who am i)
printf "%s\n" "${myUser%% *}"
user4035

(or) just use a simple awk, command,

who am i | awk '{print $1}'
user4035

i.e. in a variable do,

myUser=$(who am i | awk '{print $1}')
printf "%s\n" "$myUser"

(or) if your script is run with sudo privileges, you can use this bash environment-variable,

printf "%s\n" "${SUDO_USER}"

String split and get first and last occurrences

If you're really getting a performance problem from the large array (dont' optimize prematurely), you could use slice to extract the single strings and indexOf/lastIndexOf to find their positions:

str.slice(0, str.indexOf(','))
and
str.slice(str.lastIndexOf(',')+1) // 1==','.length

Split a string and remove the first element in string

The 'split' method works in this case

https://apidock.com/ruby/String/split

'4.0.0-4.0-M-672092'.split('-')[1..-1].join('-')

# => "4.0-M-672092"

Just be careful, in this application is fine, but in long texts this might become unoptimized, since it splits all the string and then joins the array all over again


If you need this in wider texts to be more optimized, you can find the "-" index (which is your split) and use the next position to make a substring

text = '4.0.0-4.0-M-672092'
text[(text.index('-') + 1)..-1]

# => "4.0-M-672092"

But you can't do it in one line, and not finding a split character will result in an error, so use a rescue statement if that is possible to happen

Splitting on first occurrence

From the docs:

str.split([sep[, maxsplit]])

Return a list of the words in the string, using sep as the delimiter string. If maxsplit is given, at most maxsplit splits are done (thus, the list will have at most maxsplit+1 elements).

s.split('mango', 1)[1]

String split method returning first element as empty using regex

You should use matching. Change your expression to:

`^\[(.*?)\.\.(.*)\]$`

And get your results from the two captured groups.

As for why split acts this way, it's simple: you asked it to split on the [ character, but there's still an "empty string" between the start of the string and the first [ character.



Related Topics



Leave a reply



Submit