How to check if a String is numeric in PowerShell? [duplicate] - powershell

This question already has answers here:
Check if string contains numeric value in PowerShell?
(7 answers)
Closed 1 year ago.
How can you check if the whole string is a number?
You can check if a string contains a number with regular expression. Can you use regular expressions as well to find out if a string is only a number, without any other characters?
"abc" is supposed to be false, it's not a number
"abc12" is supposed to be false, it's not a number
"123" is supposed to be true

if you are not using variables, and literal which is one quote, exactly what is in between the quotes.
'abc' -is [String]
'abc12' -is [String]
'123' -is [String]

Related

How to map an array of strings into a new array of strings? [duplicate]

This question already has answers here:
Select/map each item of a Powershell array to a new array
(3 answers)
Closed 1 year ago.
I'm trying to write some PowerShell code that takes an array of suffixes and builds a new array with an inputted string prefixed to those suffixes.
How can I do something like C#'s Map or aggregation code to build a new list based off the original list?
In the below powershell ...
$workspaceName = "test"
$deleteableItemSuffixes = #("-api","-asp","-appi")
$deleteableItemNames = ???
I want the resulting array to be ["test-api", "test-asp", "test-appi"], for example.
The -replace operator allows for a concise and efficient solution for string arrays:
# Returns array #('test-api', 'test-asp', 'test-appi')
"-api", "-asp", "-appi" -replace '^', 'test'
-replace is regex-based and accepts arrays as input, in which case the operation is performed on each element.
^ matches the position at the beginning of the string, at which the substitution string, 'test' is inserted, in effect prepending that string to each array element (by contrast, $ matches the end of each string and can be used to append a value).
For other data types and if you need more flexibility, you can use the .ForEach() array method (which, in the case of a collection that is already in memory, is faster than the ForEach-Object cmdlet):
("-api", "-asp", "-appi").ForEach({ 'test' + $_ })
Strictly speaking, what .ForEach() returns isn't a .NET array, but a collection of type [System.Collections.ObjectModel.Collection[psobject]], but the difference usually doesn't matter in PowerShell.
If you do need an actual array, you can simply cast the result:
# Use the appropriate data type, or simply [array] for an [object[]] array.
[string[]] ("-api", "-asp", "-appi").ForEach({ 'test' + $_ })

PowerShell Substring Exception [duplicate]

This question already has answers here:
$string.Substring Index/Length exception
(3 answers)
Closed 5 years ago.
I'm trying to use Substring() function in PowerShell. This is the example :
$string = "example string"
$temp = $string.Substring(5,($string.Length))
In this example I'm trying to get part of $string, from the 5th char, until the end. I'm using the .Length property to get the last index of $string.
The problem is that I'm getting this exception :
Exception calling "Substring" with "2" argument(s) because of .Length property.
What can I do to get part of $string until the last char?
$string.Length indicates you want a substring that is as long as $string, which is not possible if you are starting at the 5th character of $string. [documentation]
Specify $string.Length - 5 or - much simpler - omit the 2nd argument
$string = "example string"
$temp = $string.Substring(5,($string.Length - 5))
$temp = $string.Substring(5) # much simpler
This Tip of the Week helps to get to grips with PowerShell string manipulation.

How can I remove the last character of a String in Swift 4? [duplicate]

This question already has answers here:
Remove last character from string. Swift language
(23 answers)
Remove Last Two Characters in a String
(5 answers)
Closed 5 years ago.
How do I remove the last character of a string in Swift 4? I used to use substring in earlier versions of Swift, but the substring method is deprecated.
Here's the code I have.
temp = temp.substring(to: temp.index(before: temp.endIndex))
dropLast() is your safest bet, because it handles nils and empty strings without crashing (answer by OverD), but if you want to return the character you removed, use removeLast():
var str = "String"
let removedCharacter = str.removeLast() //str becomes "Strin"
//and removedCharacter will be "g"
A different function, removeLast(_:) changes the count of the characters that should be removed:
var str = "String"
str.removeLast(3) //Str
The difference between the two is that removeLast() returns the character that was removed, while removeLast(_:) does not have a return value:
var str = "String"
print(str.removeLast()) //prints out "g"
You can use dropLast()
You can find more information on Apple documentation
A literal Swift 4 conversion of your code is
temp = String(temp[..<temp.index(before: temp.endIndex)])
foo.substring(from: index) becomes foo[index...]
foo.substring(to: index) becomes foo[..<index]
and in particular cases a new String must be created from the Substring result.
but the solution in the4kmen's answer is much better.

Why does perl treat the string "0" as false? [duplicate]

This question already has answers here:
Why is '0' false in Perl?
(6 answers)
Closed 6 years ago.
I found in the text that string '0' is equivalent to 0, hence the condition is false in PERL.
But when I looked at the ASCII table '0' is ASCII 48. So why does perl consider string '0' as a value 0 in this control structure ?
if ('0'){
print "Statement1 \n";
}
else {
print "statement2\n";
}
Because it is specified to do so.
The number 0, the strings '0' and '', the empty list "()", and "undef"
are all false in a boolean context. All other values are true.
Negation of a true value by "!" or "not" returns a special false
value. When evaluated as a string it is treated as '', but as a number, it is treated as 0.
That's the specification. The behavior conforms.
As for why the specification is so written -- Perl makes a practice of implicitly converting between strings and numbers, and in this respect does not use ASCII value but decimal value. Treating '0' in the same manner as 0 in this context is thus consistent with the behavior of allowing '9'+1 to return 10, rather than ':' (the next ASCII value).

how to extract who text between set of matching brackets [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Regular Expression to match outer brackets
Can I use Perl regular expressions to match balanced text?
i would like to extract a whole text which has matching numbers of closing brackets. Here is the example text.
Big String 1 {sub string 1 {{something} something {}} sub string 2{something} sub string {{something}{something else}}}
Is there an easy way for me to extract whole string which belongs to Big String 1 only? The results i would like to have is as such:
Big String 1 = "sub string 1 {{something} something {}} sub string 2{something} sub string {{something}{something else}}"
Thanks,