What's the easiest way to count the number of times a character ('\' in my case) appears in a string using MSBuild? I tried using Split(\) to no avail.
MsBuild 4.0 allows the use of property functions http://msdn.microsoft.com/en-us/library/dd633440.aspx
You can use this to split the string. You must then substract the length by 1 to get the occurrence count.
<Target Name="SplitCount">
<PropertyGroup>
<path>test\document\home</path>
</PropertyGroup>
<PropertyGroup>
<test>$(path.Split('\').length)</test>
</PropertyGroup>
<Message Text="occurrence count: $([MSBuild]::Subtract($(test), 1))"><Message>
</Target>
In the MSBuild Community Tasks, there is a RegexMatch task that would give you a list, which you could then count perhaps.
Another option would be to write your own custom task. Then add a bit of Linq like so:
string input = "This \\ is \\ a \\ test";
var items = (from c in input where c == '\\' select c).ToList();
var count = items.Count;
Related
I need to update an XML file. Its structure is
<product sku="xyz">
...
<custom-attributes>
<custom-attribute name="attrib1">test</custom-attribute>
...
</custom-attributes>
</product>
I want to add a line with a custom-attribute which is multi-valued so the required structure looks like this :
<custom-attributes>
<custom-attribute name="attrib1">test</custom-attribute>
...
<custom-attribute name="new1">
<value>word1</value>
<value>word2</value>
....
</custom-attribute>
</custom-attributes>
I wrote the following python code
precision = {"name" : "new1"}
for sku in soup.find_all('product'):
tagCustoms = sku.find('custom-attributes')
mynewtag = soup.new_tag('custom-attribute', attrs = precision)
tagCustoms.append(mynewtag)
for word in words: # words is a list
mynewtag.insert(1,soup.new_tag('value'))
It works ... except I can't find how to define the content within value's tag .. how to assign each word from words 'list within the same loop ?
I am stuck with this result
<custom-attribute name="new1">
<value></value>
<value></value>
....
</custom-attribute>
</custom-attributes>
I tried this code
for sku in soup.find_all('product'):
tagCustoms = sku.find('custom-attributes')
mynewtag = soup.new_tag('custom-attribute', attrs = precision)
tagCustoms.append(mynewtag)
for word in words: # words is a list
mynewtag.insert(1,soup.new_tag('value'))
mynewtag.value.string = word
but it only add the first word of the list the first value tag.
Many thanks in advance
There are several ways to handle this, but try this one and see if it works.
Change your for loop to:
for word in words:
ntag = soup.new_tag('value')
ntag.string = word
mynewtag.insert(1,ntag)
On groovy templates for jmeter page there is an example I wanted to follow:
String xml = “
<actions>
<action type=”error” info=”itsErrors”/>
<action type="warning" info=”warnWarn”/>
<action type=”info” info=”justLogInfo”/>
</actions>"
XmlParser parser = new XmlParser()
def actions= parser.parseText (xml)
actions.action.each { action ->
println "${action.'#type'}: ${action.'#info'}";
}
At least in my JMeter 5.1 it did not work as posted, but when I fixed quotation marks it did:
String xml = """
<actions>
<action type="error" info="itsErrors"/>
<action type="warning" info="warnWarn"/>
<action type="info" info="justLogInfo"/>
</actions>"""
XmlParser parser = new XmlParser()
def actions= parser.parseText (xml)
actions.action.each { action ->
println "${action.'#type'}: ${action.'#info'}";
}
My question is usage of # mainly, dot and quotes too (.'#type'). I tried web search for Groovy # and found nothing, for JMeter notations found https://jmeter.apache.org/usermanual/functions.html with only one instance of usage:
Example: ${__XPath(/path/to/build.xml, //target/#name)} This will
match all targets in build.xml and return the contents of the next
name attribute
And about variables same link:
Referencing a variable in a test element is done by bracketing the
variable name with '${' and '}'.
Groovy docs page for xml gives other notations:
https://groovy-lang.org/processing-xml.html
def text = '''
<list>
<technology>
<name>Groovy</name>
</technology>
</list>
'''
def list = new XmlParser().parseText(text)
assert list instanceof groovy.util.Node
assert list.technology.name.text() == 'Groovy'
What each notation in "${action.'#type'}: ${action.'#info'}" means?
It isn't a JMeter variable even with ${}, is it?
I managed to keep in working only w/put ', other parts seems necessary: ", ., #, {}, $. I may have put extra in last phrase, some I can explain, but just to be sure I understand it right.
It's GPath syntax used in groovy
The most common way of querying XML in Groovy is using GPath
For XML, you can also specify attributes, e.g.:
a["#href"] → the href attribute of all the a elements
a.'#href' → an alternative way of expressing this
a.#href → an alternative way of expressing this when using XmlSlurper
I am fairly new to Puppet and Ruby. Most likely this question has been asked before but I am not able to find any relevant information.
In my puppet code I will have a string variable retrieved from the fact hostname.
$n="$facts['hostname'].ex-ample.com"
I am expecting to get the values like these
DEV-123456-02B.ex-ample.com,
SCC-123456-02A.ex-ample.com,
DEV-123456-03B.ex-ample.com,
SCC-999999-04A.ex-ample.com
I want to perform the following action. Change the string to lowercase and then replace the
-02, -03 or -04 to -01.
So my output would be like
dev-123456-01b.ex-ample.com,
scc-123456-01a.ex-ample.com,
dev-123456-01b.ex-ample.com,
scc-999999-01a.ex-ample.com
I figured I would need to use .downcase on $n to make everything lowercase. But I am not sure how to replace the digits. I was thinking of .gsub or split but not sure how. I would prefer to make this happen in a oneline code.
If you really want a one-liner, you could run this against each string:
str
.downcase
.split('-')
.map
.with_index { |substr, i| i == 2 ? substr.gsub(/0[0-9]/, '01') : substr }
.join('-')
Without knowing what format your input list is taking, I'm not sure how to advise on how to iterate through it, but maybe you have that covered already. Hope it helps.
Note that Puppet and Ruby are entirely different languages and the other answers are for Ruby and won't work in Puppet.
What you need is:
$h = downcase(regsubst($facts['hostname'], '..(.)$', '01\1'))
$n = "${h}.ex-ample.com"
notice($n)
Note:
The downcase and regsubst functions come from stdlib.
I do a regex search and replace using the regsubst function and replace ..(.)$ - 2 characters followed by another one that I capture at the end of the string and replace that with 01 and the captured string.
All of that is then downcased.
If the -01--04 part is always on the same string index you could use that to replace the content.
original = 'DEV-123456-02B.ex-ample.com'
# 11 -^
string = original.downcase # creates a new downcased string
string[11, 2] = '01' # replace from index 11, 2 characters
string #=> "dev-123456-01b.ex-ample.com"
Visual Studio is giving me the following error when I submit and store in the database.
"string must be exactly one character long"
to try to resolve tried this but without success:
cmd.Parameters.Add("#nomeEmpresa", OleDb.OleDbType.Integer).Value = Convert.ToChar(cbxEmpresa.Text)
cmd.Parameters.Add("#nomeContacto", OleDb.OleDbType.Integer).Value = Convert.ToChar(txtNomeContacto.Text)
cmd.Parameters.Add("#apelidoContacto", OleDb.OleDbType.Integer).Value = Convert.ToChar(txtApelidoContacto.Text)
cmd.Parameters.Add("#funcao", OleDb.OleDbType.Integer).Value = Convert.ToChar(txtFuncao.Text)
how can I solve this problem?
If you look at the documentation of Convert.ToChar you could read
Converts the first character of a specified string to a Unicode
character. Namespace: System Assembly: mscorlib (in mscorlib.dll)
Syntax
public static char ToChar( string value )
valueType: System.String
A string of length 1.
That's the reason of your error.
However your code seems to be incorrect. If you want to pass Integer values types by your user to your sql you need to convert your input using something like Int32.TryParse(textbox.text)
Instead if you want to pass string values you need to change your parameter type to SqlDbType.NVarChar.
Convert.ToChar(string) requires that the string only contain a single character. You need to gaurrantee this for each of the strings before you call it, or manually select the first character from the string, or something similar.
Docs for Convert.ToChar(string), throws FormatException "The length of value is not 1."
http://msdn.microsoft.com/en-us/library/5f3ew98y(v=vs.110).aspx
I need to validate for valid code name.
So, my string can have values like below:
String test = "C000. ", "C010. ", "C020. ", "C030. ", "CA00. ","C0B0. ","C00C. "
So my function needs to validate below conditions:
It should start with C
After that next 3 characters should be numeric before .
Rest it can be anything.
So in above string values, only ["C000.", "C010.", "C020.", "C030."] are valid ones.
EDIT:
Below is the code I tried:
if (nameObject.Title.StartsWith(String.Format("^[C][0-9]{3}$",nameObject.Title)))
I'd suggest a regex, for example (written off the top of my head, may need work):
string s = "C030.";
Regex reg = new Regex("C[0-9]{3,3}\\.");
bool isMatch = reg.IsMatch(s);
This regex should do the trick:
Regex.IsMatch(input, #"C[0-9]{3}\..*")
Check out http://www.techotopia.com/index.php/Working_with_Strings_in_C_Sharp
for a quick tutorial on (among other things) individual access of string elements, so you can test each element for your criteria.
If you think your criteria may change, using regular expressions gives you maximum flexibility (but is more runtime intensive than regular string-element evaluation). In your case, it may be overkill, IMHO.