Parse the contents of a string on-the-fly in PowerShell [duplicate] - string

This question already has answers here:
Expand string Variable stored via Single Quote in Powershell
(2 answers)
Closed 5 years ago.
The first example below is a normal static string getting parsed. The second example is me attempting to do the same thing but get the string to parse live, dynamically. I need to know what to put in the place of (($myparse gets evaluated)) below in order to get it to parse the contents of $myparse on-the-fly. I'm sure it's some kind of script block, but I can't figure out what kind.
The following code correctly parses the static string as "Hello John Smith" and stores it in $mysalutation:
>$firstName = "John"
>$lastName = "Smith"
>$mysalutation = "Hello $firstName$(if($lastname) {" " + $lastName})."
>$mysalutation
Hello John Smith.
What I want to do is parse the same string on the fly:
>$myparse = 'Hello $firstName$(if($lastname) {" " + $lastName}).'
>$myparse
Hello $firstName$(if($lastname) {" " + $lastName}).
>$firstName = "Jason"
>$lastName = "Bourne"
>$mysalutation = (($myparse gets evaluated))
>$mysalutation
Hello Jason Bourne.

You are looking for the ExpandString function:
$ExecutionContext.InvokeCommand.ExpandString($myparse)

Related

Format string with list in Python3

Lets say I have string: s = '{1} goes to {0}'
And I want to format this string with list: l = ['Hollywood', 'Frankie']
I cannot modify string and list both. Is there way to write simple piece of code to handle this case?
PS. I know about question "Python Format String with List", but it is not what Im asking.
Use the unpack operator * when passing the list to the format method.
s = '{1} goes to {0}'
l = ['Hollywood', 'Frankie']
print(s.format(*l))
This outputs:
Frankie goes to Hollywood

Arithmetic operation in jmeter Groovy

I created a script that receives a variable from another sampler.
I put the variable in a new variable (not want to mess with the source).
And I tried to double the result, the problem is that it multiply as a string and not as in math.
The variable is 6, and I wanted to display 12, but it display 6.0 6.0.
Moreover, how can I save the results in a new variable?
System.out.println(" Impression Price *2 is: " + Impression_price*2);
System.out.println(" Impression Price*2 is: " + (Impression_price.multiply(2.0)));
You need to cast your args[3] which is a String to a corresponding numeric type, for example:
def Impression_price = args[3] as float
Demo:
More information: Creating JMeter Variables in Java - The Ultimate Guide
You need to convert String do double using Double.parseDouble, for example:
def Impression_price= Double.parseDouble(args[3]);
When you log you need to convert back to String using String.valueOf, for example:
log.info(String.valueOf(Impression_price*2));
To put a non String value you need to use vars.putObject:
vars.putObject("Impression_price_double", Impression_price *2);

Split string in multiples lines swift

I'm trying to split a string in multiple lines (such as the text of a song); I'm working in the localizable.strings.
I'd like to do something like this:
"TITLE_SONG01" = "It's my life";
"SUBTITLE_SONG01" = "";
"TEXT_SONG01" = "It's my life" +
"is now or never";
but it doesn't work because data isn't in the correct format. Can anyone help me?
You can do that by inserting "\n" where you want the new line to start. For example:
print("Hello\nWorld")
Which will output the Hello on one line and World on another like this:
Hello
World

Importing String with variables from Txt file

I need to import text from txt file with some variables. I use BufferedReader and File Reader. In code I have :
String car = "vw golf";
String color = "nice sunny blue color";
And in my txt file:
I have nice " +car+ " which has "+color+".
My expected output :
I have nice vw golf which has nice sunny blue color.
My actual output is :
I have nice " +car+ " which has "+color+".
If I've understood correctly, what you want to do is replace " + car + " with the value of your car string and likewise for colour. You've tried to do this by writing your text file as if it were a command to be evaluated. However, that won't happen - it will just be outputted as is. I'm going to assume you are using c#. What you need to do is, prior to outputting your string, parse it to replace the markers with the variables. I would recommend you get rid of the double quotes in your text file. You could then do something like this:
string text = this.ReadTextFromFile();
string ammended = text.Replace("+car+", car);
As mentioned, this is assuming you remove the double quotes from your text file so it reads:
I have nice +car+ which has +color+.
Also, you don't need to use the + symbols, but I suppose they are a good way of designating a unique token to be replaced. You could use {car} in the file and then likewise in the Replace startment, for example.
I may not have properly understood what you wanted to do, of course!
Edit: Incase of confustion,
this.ReadTextFile();
was just a short hand way of saying that the text variable contains the contents as read from your text file.

Powershell 3 string manipulating using split and join [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I need to capitalize the first letter of each word using split an join using powershell 3.0
I have been going crazy trying to figure this out.
Any help would be appreciated.
Function Proper( [switch]$AllCaps, [switch]$title, [string]$textentered=" ")
{
if ($AllCaps)
{$textentered.Toupper()}
Elseif ($title)
{$textentered -split " "
$properwords = $textentered | foreach { $_ }
$properwords -join " "
$properwords.substring(0,1).toupper()+$properwords.substring(1).tolower()
}
}
proper -title "test test"
System.Globalization.TextInfo class has the ToTitleCase method you can use, just join your words as normal into a string (called $lowerstring for example) then call the method on that string using the `Get-Culture cmdlet::
$titlecasestring = (Get-Culture).TextInfo.ToTitleCase($lowerstring)
For the string concatenation I tend to use the following format:
$lowerstring = ("the " + "quick " + "brown " + "fox")
But the following are also valid:
$lowerstring = 'the','quick','brown','fox' -join " "
$lowerstring = $a,$b,$c,$d -join " "
EDIT:
Based on the code you supplied, you don't need to split/join strings if what you're passing in is just a phrase in a string, so the following is what you need
Function Proper{
Param ([Parameter(Mandatory=$true, Position=0)]
[string]$textentered,
[switch]$AllCaps,
[switch]$Title)
if ($AllCaps){$properwords = $textentered.Toupper()}
if ($title) {
$properwords = (Get-Culture).TextInfo.ToTitleCase($textentered)
}
if ((!($Title)) -and (!($AllCaps))){
Return $textentered}
}
Proper "test test" -AllCaps
Proper "test test" -Title
Proper "test test"
In the Param () block I set the $textentered parameter as mandatory, and that it must be the first parameter (Position = 0).
If neither of the AllCaps or Title parameters are passed, the original input string is passed back out, unaltered.

Resources