I have a string:
result = " is programming that manages the execution of programs written in any of several supported languages."
I want to remove white space at the beginning of a sentence.
How I can do it?
You can do this by using Trim(String) in VBA (for trimming left and right spaces) or use LTrim(String) for only trimming leading spaces.
You can use Trim(result) it will remove all white spaces at the beginning and at the end of a string
Related
Apparently the report seems to explicitly trimming any trailing spaces on report fields. But I need those spaces because it's a bank feed. I got an expression something like :
=PadRight( [BAccount.AcctName] , 50, ' ' )
or
=' BLAH '
Still no spaces
Does anyone has any tricks to achieve it ?
TIA
I believe the report engine will trim all whitespace and non-printable characters.
Does anyone has any tricks to achieve it ?
Yes, based on assumption above; one possible trick is to append a printable Unicode character at the end of the string that renders as whitespace but isn't technically considered white space. This type of trick is very common in phishing attacks.
I choose the 'Braille Pattern Blank' character which renders like a space character but isn't considered whitespace.
Reference:
https://www.compart.com/en/unicode/U+2800
You can copy it directly from character box in link above (highlighted blue):
And paste it in Acumatica report designer to append it after the blank space:
I tested this by selecting the field in report, it appended the desired spaces:
I have some text I need remove from a string, but I cannot use the normal Replace() because it is a timestamp that will always be changing.
Text to remove <09:35:40> (could be any time, but always the same format <HH:MM:SS>).
These time stamps could occur in multiple locations throughout a string, all need to be removed (replaced with "").
I've seen regular expressions used for similar applications on other posts, but I don't really understand them, so cannot validate which one to use for my use case here.
Edit:
The < and > also need to be removed.
If feedback could be provided as to the -1, that would be great. Help me improve.
You don't need regular expressions I don't think. What about:
Range("A:A").Replace "<??:??:??>", "", xlPart
Use Application.Trim() to deal with double spaces after replacement;
Range("A:A") is just my placeholder for whatever is your range-object.
You could use Split:
Text = "Text to remove <09:35:40> (could be any time, but always the same format)"
NewText = Split(Text, "<")(0) + Split(Text, "> ")(1)
? NewText
Text to remove (could be any time, but always the same format)
Use this regular expression to select all the sub strings in HH:MM:SS format. Then just replace it with empty string ("")
\d\d:\d\d:\d\d
And use this one to remove select it including these characters <>
\<\d\d:\d\d:\d\d\>
In excel, you can find-replace between two characters using the normal Ctrl+F replace, and searching for <*> (in my use case). However, special characters such as * cannot be used in the Replace() function within VBA code. If you want to perform the same operation, replacing anything between characters, I believe a regular expression is a good way of achieving this. This following code works for me in Excel VBA. Note, I am working on a string before it hits the spreadsheet (E.g. I am formatting the string before I print it to any cells).
Dim regExp As ObjectSet
regExp = CreateObject("vbscript.regexp") 'This way, you do not have enable VBScript Regular Expressions 5.5 in the references.
With regExp
.Global = True 'Get all matches.
.Pattern = "\<\d\d:\d\d:\d\d\>" 'Search for any string that contains the pattern entered in quotes. As per the guide Jayadul Shuvo links.
newString = .Replace(prevString, "") 'Replace instances of strings that contain the pattern, with a nothing ""
End With
I had another requirement where "\start " and "\stop " were before and after the timestamps. I was not aware this could have been the case to start with, new information came to light. E.g. "\start <HH:MM:SS> \stop ". This could also be spread across newlines, so I had to consider the removal of the newline as well.
This essentially meant I had to remove a string between two substrings (and be able to remove the newline) and I have used the following pattern:
"\\\bstart\b\s((.|\n)*?)\\\bstop\b\s"
'\ removes the special operation of \ and matches the \
'\b followed by \b matches the whole word between the \b and \b
'\s matches the space
'(.|\n) matches any single character and newlines
'*? matches zero or more occurrences, but as fewer as possible
I would recommend using a regular expression tester such as: https://regexr.com/3hmb6when creating these patterns, it is so helpful! Use the tabs on the bottom right to see what is replaced and to get an explanation of what is going on.
Picture Snippet of the explanation tab for ((.|\n)*?) on the tester website
I copied some data from a website in a worksheet and noticed some cells in addition to the expected value, also contained a trailing space.
I wanted to remove these spaces due to causing troubles to my code. I spotted a specific cell having this space and tried:
Worksheets(2).Range("D17").Value = Trim(Worksheets(2).Range("D17").Value)
The space was still there.
Then I tried:
Worksheets(2).Range("D17").Value = WorksheetFunction.Trim(Worksheets(2).Range("D17").Value)
Still no luck.
The Replace function not only removes leading and trailing, but also the spaces inside the string:
Worksheets(2).Range("D17").Value = Replace(Worksheets(2).Range("D17").Value," ","")
Some cells contained two words so this was not an option.
I could check the last character for being a space and remove it for every cell, but this did not seem as an elegant solution and the main question remains: Why Trim functions do not work?
After spending a looooong time searching on the net, I found out here that there is also another character for space, the output of Chr(160) and for some reason Trim functions ignore it. So the solution in my case was firstly replace all these 'different' spaces and then trim values:
Worksheets(2).Range("D17").Value = Replace(Worksheets(2).Range("D17").Value, Chr(160), " ")
Worksheets(2).Range("D17").Value = Trim(Worksheets(2).Range("D17").Value)
In case you encounter a similar problem and run into a different-from-Chr(160) 'unknown' character, you can find out what this character is, by isolating this character and using the Asc function.
I have a string, and I need to calculate the number of spaces that I remove when I do trimStart.
For example, I have the following string \t\t \tabcs
so I have two tabs and two spaces and another tab that will be removed using trim start (the rest is non space related chars).
I need to know how many spaces will be removed. since I don't know how much is \t, I can't just count it as a single char.
(My purpose is to calculate the column shift of a string due to the trimming action. Obviously comparing the lengths before and after the trim will not return me the desired result.
Do you have any ideas?
Thanks!
I'm making a simple android game in Lua, and in one of its steps to set the game is set an word (or sentence; basically, a string) input by the player. The "word" may have spaces, but I want to forbid the player to input a string with two or more spaces in a row, like "fly bird".
I tried using string.match(word, " "), string.match(word, "%s%s")
and string.match(word, "%s+%s+") and none of these worked, and somehow, the last one always "detect" double space, no matter if it has or not spaces.
What can I do to detect if there are multiple spaces in a row in a string? (Just detect, not replace, so I can send a warning message to the player.)
If its exactly two spaces you are interested in, simply use find
word:find(' ')
It will return range of first occurrence of two consecutive spaces.
input = input:gsub("%s+", " ")
The above code should take the input and remove all excessive spacing and replace it with just 1 space.