Split the string to get only the first 5 characters
You can't be in multiple cwd
s simultaneously. To run the command for each directory that matches the pattern, you can use Dir#glob
:
Dir.glob('/var/cache/acpchef/src/ap-kernelmodule-10*').each do |cwd|
Mixlib::ShellOut.new("command run here", cwd: cwd).run_command
end
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.
How to get first 5 characters from string
For single-byte strings (e.g. US-ASCII, ISO 8859 family, etc.) use substr
and for multi-byte strings (e.g. UTF-8, UTF-16, etc.) use mb_substr
:
// singlebyte strings
$result = substr($myStr, 0, 5);
// multibyte strings
$result = mb_substr($myStr, 0, 5);
Javascript How to get first three characters of a string
var str = '012123';var strFirstThree = str.substring(0,3);
console.log(str); //shows '012123'console.log(strFirstThree); // shows '012'
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
Strip all but first 5 characters - Python
A string in Python is a sequence type, like a list or a tuple. Simply grab the first 5 characters:
some_var = 'AAAH8192375948'[:5]
print some_var # AAAH8
The slice notation is [start:end:increment]
-- numbers are optional if you want to use the defaults (start defaults to 0, end to len(my_sequence) and increment to 1). So:
sequence = [1,2,3,4,5,6,7,8,9,10] # range(1,11)
sequence[0:5:1] == sequence[0:5] == sequence[:5]
# [1, 2, 3, 4, 5]
sequence[1:len(sequence):1] == sequence[1:len(sequence)] == sequence[1:]
# [2, 3, 4, 5, 6, 7, 8, 9, 10]
sequence[0:len(sequence):2] == sequence[:len(sequence):2] == sequence[::2]
# [1, 3, 5, 7, 9]
strip
removes a character or set of characters from the beginning and end of the string - entering a negative number simply means that you are attempting to remove the string representation of that negative number from the string.
Split a string only by first space in python
Just pass the count as second parameter to str.split
function.
>>> s = "238 NEO Sports"
>>> s.split(" ", 1)
['238', 'NEO Sports']
split string only on first instance - java
string.split("=", limit=2);
As String.split(java.lang.String regex, int limit)
explains:
The array returned by this method contains each substring of this string that is terminated by another substring that matches the given expression or is terminated by the end of the string. The substrings in the array are in the order in which they occur in this string. If the expression does not match any part of the input then the resulting array has just one element, namely this string.
The
limit
parameter controls the number of times the pattern is applied and therefore affects the length of the resulting array. If the limit n is greater than zero then the pattern will be applied at most n - 1 times, the array's length will be no greater than n, and the array's last entry will contain all input beyond the last matched delimiter.
The string
boo:and:foo
, for example, yields the following results with these parameters:
Regex Limit Result
: 2 { "boo", "and:foo" }
: 5 { "boo", "and", "foo" }
: -2 { "boo", "and", "foo" }
o 5 { "b", "", ":and:f", "", "" }
o -2 { "b", "", ":and:f", "", "" }
o 0 { "b", "", ":and:f" }
Related Topics
Invalid Active Developer Path on MAC Os X After Installing Ruby
How Would You Parse a Url in Ruby to Get the Main Domain
No Such File to Load - Readline
Nokogiri, Open-Uri, and Unicode Characters
How to Get Request Referer Path
How to Convert a String to a Constant in Ruby
Double Pipe Symbols in Ruby Variable Assignment
How to Get a Date from Date_Select or Select_Date in Rails
Ruby - Difference Between Array#≪≪ and Array#Push
Ruby Method Array#≪≪ Not Updating the Array in Hash
Need to Split Arrays to Sub Arrays of Specified Size in Ruby
How to Capture Stdout to a String
Why Use "Self" to Access Activerecord/Rails Model Properties
Getting Fields_For and Accepts_Nested_Attributes_For to Work With a Belongs_To Relationship
How to Read a User Uploaded File, Without Saving It to the Database
Best Practices For Reusing Code Between Controllers in Ruby on Rails