Space not space on string - string

I have a cell in Excel that is pasted from outlook. When I read it in vba, there is some spaces on it and I need to remove.
It took me some hours to find the solution to remove it.
I tried:
Replace(string_value," ","") and did not remove the spaces
Application.find(" ", string_value) and returned error
Application.trim(string_value) and did not remove the spaces
...
My solution:
string_value = Replace(Str(string_value), " ", "")
Does anyone knows why?

Outlook emails can be written in two modes:
1) Plain text;
2) HTML (for allowing colors, layouts etc.)
You probably are into the second case, because a table cannot be inserted in plain text mode, and when you get the value of the variable string_value you get it in full HTML string.
What happens in fact is that Outlook converts your body in HTML to allow it being viewed from any mail server (HTML is the international web language, read by any browser uniformally, or almost). Which means, the character space will not any longer be " ", but rather its HTML equivalent, i.e. &nbsp (or something else, I don't know if there's a breakline or not, I'm not a real expert of HTML).
Hence, when you use one of the three methods above, none of them is able to find the "visual space", or better the string character " ". However, when you use the function Str(), the HTML string will be re-converted to VBA Language (i.e. &nbsp or equivalent will be converted in " " and your approach will hence work.

That is because the statement:
string_value = Replace(Str(string_value), " ", "")
does several things. Usually str() takes a Long argument. Becuase you are giving it a String, VBA (in the background) converts the string to a Long. In doing this it discards any junk characters (non-digits) like CHR(160).Str() pads the result with a blank, which Replace() removes !
Str Function on MSDN

Use:
Replace(string_value,CHAR(160),"")

Related

BluePrism count number of times a character exists in a string

I have a calculation which will remove a blank space and replace with a full stop. This is correct for 90% of my cases. However, sometimes two blanks will appear in my value. For the second space I want to delete it. Is this possible?
I think it may be possible using a code stage, but I am not sure what the code would be.
My current calculation is Replace([Item Data.Name], " ", ".")
Example data John B Smith I want the result to be John.BSmith
For anything that'd like to do with the strings, there is a really powerful tool called Regular Expressions (regex). I encourage you to play with it, because it's a really powerful tool in the hands of RPA developer.
To replace the second space in any string with a "." you can use the following action.
Object: Utility - Strings
Action: Regex - Find and Replace
Input:
Regex Pattern: "(?<= .*) "
Text: "John B Smith"
Replacement: "."
The above action is not a standard Blueprism one, so it has to be added to your VBO. The action looks as follows:
The VB.net code for that action is as follows:
Dim R as New Regex(Regex_Pattern, RegexOptions.SingleLine)
Dim M as Match = R.Match(Text)
replacement_result = R.Replace(Text,Regex_Pattern,replacement_string)
There might be a need for some additional assemblies, so please see below a printscreen of references and namespaces used in my object:
I resolved this issue by using the Utility - Strings object and the split text action. I split my name by space. This outputted a collection which I was then able to loop through and add a full stop after the fist instance but then trim the other instances.
Please see screenshot
I think the simplest solution would be
Replace(Replace(Text," "," ")" ","."))
if you know that it will give one or two spaces
First replace the two white spaces to single and then again single white space to dot(.)

Concatenation with empty string raises ERR:INVALID DIM

In TI-BASIC, the + operation is overloaded for string concatenation (in this, if nothing else, TI-BASIC joins the rest of the world).
However, any attempt to concatenate involving an empty string raises a Dimension Mismatch error:
"Fizz"+"Buzz"
FizzBuzz
"Fizz"+""
Error
""+"Buzz"
Error
""+""
Error
Why does this occur, and is there an elegant workaround? I've been using a starting space and truncating the string when necessary (doesn't always work well) or using a loop to add characters one at a time (slow).
The best way depends on what you are doing.
If you have a string (in this case, Str1) that you need to concatenate with another (Str2), and you don't know if it is empty, then this is a good general-case solution:
Str2
If length(Str1
Str1+Str2
If you need to loop and add a stuff to the string each time, then this is your best solution:
Before the loop:
" →Str1
In the loop:
Str1+<stuff_that_isn't_an_empty_string>→Str1
After the loop:
sub(Str1,2,length(Str1)-1→Str1
There are other situations, too, and if you have a specific situation, then you should post a simplified version of the relevant code.
Hope this helps!
It is very unfortunate that TI-Basic doesn't support empty strings. If you are starting with an empty string and adding chars, you have to do something like this:
"?
For(I,1,3
Prompt Str1
Ans+Str1
End
sub(Ans,2,length(Ans)-1
Another useful trick is that if you have a string that you are eventually going to evaluate using expr(, you can do "("+Str1+")"→Str1 and then freely do search and replace on the string. This is a necessary workaround since you can't search and replace any text involving the first or last character in a string.

Making sure string numbers operations are culture independent

I download an exchange rate through an XmlHttp request that gets inside the code as a string (being the .innerText of a <div> element) and represents a double type number: 1.525.
When building this script, I've done it on my OS which has the English culture model (i.e. 1.525 means 1 unit and 0.525 decimals).
However, this script will now run on a French OS which uses the comma , instead of the . to separate decimals.
Which means, the operation Application.Evaluate(10000/myRate) will fail if the . is instead of the ,.
Easy solution would be to replace the "." with a "," via Application.Evaluate(10000/Replace(myRate,".",","). However, this is clearly not nice because now the script would fail on an English system.
With VB.NET I would be able to make it culture-independent by converting it like:
myRate.ToDouble(System.Globalization.CultureInfo.InvariantCulture)
I've tried Googling the VBA alternative for a while without success; does anyone know if there's a more elegant way of internationalize my script than just replacing the "." with a ","?
Here's my current solution (that I don't really like):
On Error Resume Next
test = CDbl(myRate)/2
If Err.Number <> 0 Then
myRate = Replace(myRate,".",",")
On Error GoTo 0
End If
use the Application.DecimalSeparator property?
Application.Evaluate(10000/CDbl(Replace(myRate,".", Application.DecimalSeparator))
You can temporary change decimal and thousands separator, by using Application object.
To read several current OS (International) settings: Application.International(index) property
To change:
Application.ThousandsSeparator = "."
In MS Access, we do not have Application.DecimalSeparator nor Application.ThousandsSeparator nor Application.International. The only thing we have is Val(vString) and Str(vNumber). They both convert the argument using the English (invariant) culture.
So, in MS Access the OP's question could be solved like this:
vResult = 10000/Val(myRate)

String output strangeness

I'm very much a newbie to Ada and I'm trying to teach myself, with the assistance of one of John Barnes' books.
While dabbling with strings, I wrote a very basic program to read user input from the keyboard and then output it again to the console. However, when the output is produced, it appears to contain additional characters that have not been explicitly typed. Looking at it, my gut reaction is that it's unicode/Konsole-related but I definitely cannot discount whether or not it's something that I have done wrong in my code.
I have attached a screenshot of the program and its output. Any ideas would be greatly appreciated.
Thanks!
Image showing code and output
The difference is that strings are NOT null terminated; they are exactly as long as they say they are : in this case, 100 characters.
And not all the characters in your example are what you typed.
Quick fix : only output the Len characters you typed...
Put_Line ("Your String: '" & UserString(1 .. Len) & "' (Chars: " & natural'image(Len) & ")");
(I have used the standard portable natural'image(Len) rather than the compiler-specific Len'img form)
This leaves some remaining problems :
if you type more than 100 characters, the result will be safe but unsatisfactory.
you need to specify the slice boundaries (1..Len) everywhere you need the string.
Better fix : create a string of exactly the length you typed. Easiest way is calling the function form of Get_Line, and initialising an unconstrained String from the function's return value. The unconstrained String has no defined length when it is declared, it gets its length from the initialisation. For this to work, we can start a new block...
Put_Line("Enter a string: ");
declare
UserString : constant String := Get_Line;
-- the string is now the correct length, however much you typed.
begin
Put_Line ("Your String: '" & UserString & "' (Chars: " &
natural'image(UserString'Length) & ")");
-- other processing on UserString here
end;
-- UserString is now out of scope, automatically freed
Notice that there is no more need for the Len variable. The string's length is an attribute of the string, not a separate quantity that may be incorrectly managed; one less thing to go wrong.
And the declare block is a safe and powerful technique for allocating runtime sized storage without needing heap allocation routines, pointers, deallocation etc. Several less things to go wrong.
It is usually a good idea (thanks Simon!) to make UserString a Constant. Not essential (otherwise you can edit it in place, but not change its length) but assuming you don't intend to edit it - one less thing to go wrong.
There are other techniques involving Bounded_String or Unbounded_String but they aren't really necessary here.

Coldfusion not converting accented text or MS Word chars

Running Coldfusion 8, I am trying to clean text input before saving to a database that will take things like the MS equivalent of ' " - and accented letters, and converting them.
I have tried replace, REReplace, and various UDFs found on the internet. None seem to work. In fact, I tried this:
<cfscript>
function cleanString(string) {
var newString = string;
newString = replace("'", "'", ALL);
return newString;
}
</cfscript>
The single quote to be replaced above is a MS Word style single quote. Coldfusion threw an error, the error scope said invalid syntax and the single quote in the error scope was a square. If I change it to the chr() form, and replace with ', I get a blank. If I do chr() to the entity, I get a blank.
I am more than certain I have jumped this hurdle before, and not sure why nothing is working now. Is there a new setting in CF8 vs CF7 regarding character encoding that I am missing?
There is a great script for demoronizing (yes, that's a technical term) text copied from MS word and the like. It can be found at CFLib:
http://cflib.org/index.cfm?event=page.udfbyid&udfid=725
I've used it several times, and been happy with it out-of-the-box (though I have added some additions for specific applications).

Resources