What's wrong with this line? - string

I am trying to remove all the " from a string called s1, I have this line
s1=replace (s1, """, "")
But I get a compile error saying it is expecting a list separator or )
How can I fix it?
Thanks in advance.

Your second string isn’t properly delimited. If you want to use a quotation mark (") inside your string, you need to double it. Since your string only consists of a quotation mark, it looks as follows:
Quotation mark to start the string, ".
Double quotation mark that represents a single quotation mark inside the string, "".
Ending quotation mark, ".
In summary:
s1 = Replace(s1, """", "")

Konrad's suggestion is the one you should go with, but here's another for completeness/amusement.
s1 = Replace(s1, Chr(34), "")
And if you ever get bored at a party and need something to read on your phone, here's the list of the 256 such ASCII codes you can use with Chr().
http://msdn.microsoft.com/en-us/library/4z4t9ed1%28v=VS.80%29.aspx

Did you really write """? You have to escape the " in the middle - just double it like:
replace( s1, """", "" )

Commonly used syntaxes are:
s1=replace (s1, "\"", "")
s1=replace (s1, """", "")
s1=replace (s1, '"', "")

Related

VB Net Quotes in a string

I need quotes in a string in VB Net.
c = C:\test.doc
How can I realize that the string looks like:
PrintFile /File="C:\test.doc" /OutputFile="C:\test.pdf"
online I read that I have to double the quotes...
but this doesn't work:
" PrintFile /File=""" & c & """ /OutputFile=""" & pfadFertig & pdfName & ".pdf"""
How can I get c and pfadFertig & pdfName & .pdf appear in quotes?
You do indeed have to double the quotes. This is a great example of why you should use String.Format or, even better, string interpolation. Using concatenation operators (&) already makes your code harder to read but with all the extra quotes, it's harder still. Do this:
Dim str = $"PrintFile /File=""{c}"" /OutputFile=""{pfadFertig}{pdfName}.pdf"""
It's obviously much easier to read.
Also I suspect that pfadFertig is a folder path, in which case you should be using Path.Combine to create the file path:
Dim str = $"PrintFile /File=""{c}"" /OutputFile=""{Path.Combine(pfadFertig, pdfName)}.pdf"""
Path.Combine will ensure the correct number of slashes regardless of what trailing or leading slashes are included in the inputs, so you can never make a mistake. In this case, I could probably excuse one concatenation operator as it may seem more natural:
Dim str = $"PrintFile /File=""{c}"" /OutputFile=""{Path.Combine(pfadFertig, pdfName & ".pdf")}"""

How do I add the ' symbol to a string

I had someone help with a prior question to turn hexadecimal to string, but I want the string to output surrounded with '
so it returns 'I0KB' instead of just I0KB.
What I have:
with open('D:\new 4.txt', 'w') as f:
f.write('if not (GetItemTypeId(GetSoldItem())==$49304B42) then\n')
def hex_match_to_string(m):
return ''.join([chr(int(m.group(1)[i:i+2], 16)) for i in range(0, len(m.group(1)), 2)])
# ...
line = re.sub(r'\$((?:\w\w\w\w\w\w\w\w)+)', hex_match_to_string, line)
file_out.write(line)
output:
if not (GetItemTypeId(GetSoldItem())==I0KB) then
but I want it to output
if not (GetItemTypeId(GetSoldItem())=='I0KB') then
and using
def hex_match_to_string(m):
return ''.join(',[chr(int(m.group(1)[i:i+2], 16)) for i in range(0, len(m.group(1)), 2)],')
...gives me a syntax error even though I read that join(a,b,c) is the way to combine strings.
Thanks in advance for the help, and sorry I am clueless for what should be an easy task.
You can put a backslash character followed by a quote ( \" or \' ). This is called an escape sequence and Python will remove the backslash, and put just the quote in the string.
You should not add the quotes to the argument passed to join, but wrap the result of the join with quotes:
return "'" + ''.join([chr(int(m.group(1)[i:i+2], 16)) for i in range(0, len(m.group(1)), 2)]) + "'"
I think it's important to distinguish between enclosing a string between, single, double, or triple quotation marks. See answers here regarding the most common use of the third (the so-called doc-strings).
While most of the time you can use " and ' interchangeably, you can use them together in order to escape the quotation:
>>> print("''")
''
>>> print('"')
"
You can also use the double quotes three times to escape any double quotes in between:
>>> print(""" " " "j""")
" " "j
But I'd suggest against the last option, because it doesn't always work as expected, for example, print(""""""") will throw an error. (And of course, you could always use the \ to escape any special character.)

Replace, Clean & Trim VBA

I m trying to:
Replace double spaces with single.
Clean
Trim
the same string and i use:
AmountI = Replace(Application.WorksheetFunction.Clean(Trim(.Cells(j, 9).Value)), " ", " ")
I want to know if there is a specific order of those three VBA functions or the order does not play any role in the outcome.thanks for the help!
vba.trim doesn't remove double spacing, only leading/trailing spaces. You need the worksheet version Application.Trim or WorksheetFunction.Trim to remove interim double spaces.
If you have triple spacing, a single Replace will leave you with a double space. Application.Trim will not.
You don't want Clean to process characters (i.e. spaces) that you are going to remove anyways so Trim first.
.Value2 (without currency or date information) is marginally faster processing when you don't have currency or date data; you have a string so use .Value2.
AmountI = Application.Clean(Application.Trim(.Cells(j, 9).Value2))
AmountI = Trim(Replace(Application.WorksheetFunction.Clean(.Cells(j, 9).text), " ", " "))
My logic is:
1 .Text is faster than .Value
2 Clean may close up two spaces by removing a non-printing char between them, so do this before...
3 Turn double spaces to single with replace, then
4 Remove leading and trailing spaces

Particular string split in R

I'd like to split a text string in R but I want to take some aspects into consideration. For instance, if the string has a dot . or a !, I want my function to take them as elements of my split list. Below an example of what I want to get.
mytext="Caracas. Montevideo! Chicago."
split= "Caracas", "." ,"Montevideo", "!", "Chicago", "."
My current approach consists in replacing previously with the built-in R function gsub the "." by " . " and then I use strsplit function as well.
mytext=gsub("\\."," .",mytext)
mytext=gsub("\\!"," !",mytext)
unlist(strsplit(mytext,split=' '))
So, my question is: is there another way of implementing this by configuring the parameters for the strsplit function or another approach you coonsider could be more efficient.
Any help or suggestion is appreciated.
Look-ahead is what you're looking for here:
strsplit(mytext, split = "(?=(\\.|!))", perl = TRUE)
#[[1]]
#[1] "Caracas" "." " Montevideo" "!" " Chicago" "."
eddi's solution doesn't split the whitespaces. Try this:
> regmatches(mytext, gregexpr(text=mytext, pattern="(?=[\\.\\!])|(?:\\s)", perl=T), invert=T)
[[1]]
[1] "Caracas" "." "Montevideo" "!" "Chicago" "."

Special character replacing

Is it possible to remove the " character using an Excel formula?
I tried the REPLACE function like this
REPLACE(<sometext>, """, "")
and this
REPLACE((<sometext>, char(34), "")
but it doesn't work.
NB: I am using the Polish version of Excel, so it is probable, that the syntax is different than in the English version. For example, in Polish formulas we use ; instead of , as argument separator.
The quotation mark character " must be represented by two of them "" when it's inside a string, to revolve the ambiguity between this "textual" quotation mark and the "code" quotation marks that delimit the beginning and end of your string.
In short, the right formula to remove all " from the text in A1 is:
=SUBSTITUTE(A1,"""","")
with emphasis on the fact that the old_text argument has to be """" (four quotes) and not """ (three quotes) as you wrote in your question.
And maybe SUBSTITUTE is called Replace in the Polish edition? Anyhow, you have to use the Polish equivalent of SUBSTITUTE and whatever argument separator is appropriate (; or ,).
Replace doesn't work the way, you want it to.
Use SUBSTITUTE.
=SUBSTITUTE(A1, CHAR(34), "-")
OR use ; as separator for your example
=SUBSTITUTE(A1; CHAR(34); "-")

Resources