I have a scrolling field. It's containing some text and special characters like (---,'',.,",) e.t.c. I want add a single space before every special characters.Is it possible
I d not know how long your text is, but you can:
on mouseUp
get fld "yourField"
put "abcdefghijklmnopqrstuvwxyz" into normalChars
repeat for each char tChar in it
if tChar is not in normalChars then put space & tChar after temp
else put tChar after temp
end repeat
put temp into fld "yourOtherField"
end mouseUp
Now this assumes that all non-letter chars are "special" chars. Can you modify the handler to go the other way? That is, to identify only certain chars, rather than identify all non-letter chars?
Craig Newman
ya. Please use this code
replace "--" with " --" in field "yourField"
replace "---" with " ---" in field "yourField"
Related
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
I want to catch content between two $ symbols.
Eg: - This is $ only$ an example $so$ respond $quickly$.
Here I want to store text between dollars (only, so and quickly) into an array.
In using this code for catching. But it catches (only, an example, so, respond, quickly). I need "only", "so" and "quickly".
replace "\$" with "\XXdollarXX" in field "MytextField"
put the text of field "MytextField" into ss
repeat with i = 0 to the number of chars in ss
if char i of ss contains "$" then
repeat with x = i+1 to the number of chars in ss
if char x of ss contains "$" then
--answer x
put x into Lpos
put char i to Lpos of ss into jar
answer jar
put Lpos into i
end if
end repeat
end if
end repeat
If you examine your code while stepping through, you will see this has to work that way. The "offset" function, though, can be used in a loop, and contains a parameter "chars to skip". By continuously updating that parameter, and considering that only every other instance is pertinent, you can collect the text between the pairs of "$" as:
between instance 1 and 2
between instance 3 and 4
etc.
Make a button and a field. Put some text into the field sprinkled with "$" for testing. In the button script:
on mouseUp
put 0 into charsToSkip
repeat until charsToskip > the length of fld 1
if the optionKey is down then exit to top --YOU WILL NEED THIS RIGHT NOW...
get offset("$",fld 1,charsToSkip)
add it to charsToSkip
put charsToSkip & return after accum
end repeat
end mouseUp
I leave it to you to add the ability to exit this loop properly. Step through the handler and you will see the value in accum building in the way I mentioned.
Craig Newman
I have created a script to convert text to morsecode, and now I want to modify it to include a slash between words.So something like space slash space between morsecode words. I know my loop before the main loop is incorrect and I want to fix it to do as stated before I just really need help Thank You!!!:
...
Word=input('Please enter a word:','s');
...
Code=MC_1;
...
case ' '
Code='/'
otherwise
Valid=0;
end
if Valid
fprintf('%s ',Code);
else
disp('Input has invalid characters!')
break
end
I know you want to write a loop to remove multiple spaces in between words, but the best way to remove white space in your particular problem would be to use regular expressions, specifically with regexprep. Regular expressions are used to search for particular patterns / substrings within a larger string. In this case, what we are trying to find are substrings that consist of more than one whitespace. regexprep finds substrings that match a pattern, and replaces them with another string. In our case, you would search for any substrings within your string that contain at least one more whitespace characters, and replace them with a single whitespace character. Also, I see that you've trimmed both leading and trailing whitespace for the string using strtrim, which is great. Now, all you need to do is callregexprep like so:
Word = regexprep(Word, '\s+', ' ');
\s+ is the regular expression for finding at least one white space character. We then replace this with a single whitespace. As such, supposing we had this string stored in Word:
Word = ' hello how are you ';
Doing a trim of leading and trailing whitespace, then calling regexprep in the way we talked about thus gives:
Word = strtrim(Word);
Word = regexprep(Word, '\s+', ' ')
Word =
hello how are you
As you can see, the leading and trailing white space was removed with strtrim, and the regular expression takes care of the rest of the spaces in between.
However, if you are dead set on using a loop, what you can do is use a logical variable which is set to true when we detect a white space, and then we use this variable and skip other white space characters until we hit a character that isn't a space. We would then place our space, then /, then space, then continue. In other words, do something like this:
Word = strtrim(Word); %// Remove leading and trailing whitespace
space_hit = false; %// Initialize space encountered flag
Word_noSpace = []; %// Will store our new string
for index=1:length(Word) %// For each character in our word
if Word(index) == ' ' %// If we hit a space
if space_hit %// Check to see if we have already hit a space
continue; %// Continue if we have
else
Word_noSpace = [Word_noSpace ' ']; %// If not, add a space, then set the flag
space_hit = true;
end
else
space_hit = false; %// When we finally hit a non-space, set back to false
Word_noSpace = [Word_noSpace Word(index)]; %// Keep appending characters
end
end
Word = Word_noSpace; %// Replace to make compatible with the rest of your code
for Character = Word %// Your code begins here
...
...
What the above code does is that we have an empty string called Word_noSpace that will contain our word with no extra spaces, and those spaces replaced with a single whitespace. The loop goes through each character, and should we encounter a space, we check to see if we have already encountered a space. If we have, just continue on in the loop. If we haven't, then concatenate a whitespace. Once we finally hit a non-space character, we simply just add those characters that are not spaces to this new string. The result will be a string with no extra spaces, and those are replaced with a single white space.
Running the above code after you trim the leading and trailing white space thus gives:
Word =
hello how are you
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); "-")
I would like to create a macro or a script in Vim that does the following:
wrap a block of text in double quotes
escaping any quotes that appear in the text itself
have the quotes on the right side in vertical alignment
For example:
<html>
<head></head>
<body>
<h1>High Score Server</h1>
<table>
ROWS
</table>
</body>
</html>
would become:
"<html> "
"<head></head> "
"<body> "
"<h1>High Score Server</h1>"
"<table> "
"ROWS "
"</table> "
"</body> "
"</html> ";
I am able to achieve this with a macro, but without the vertical alignment of the quotes on the right side. Can anyone help me with this one?
What I'd do :
With "surround" and "Align" plugins :
1) with cursor on first line (0,0), type <C-V>)$s"
2) then <S-V>):Align " and <Enter>.
Another solution without plugins :
1) set virtual mode
:set ve=all
2) <C-V> to go in block-wise selection, with cursor at the position 0,0
3) go down to the bottom of the text, then Shift-I, type " and Esc. This should prepend the quotes.
4) now go on the left end (since ve=all, you can go where there is no text)
5) <C-V>, go down to bottom, type r"
This is long to explain, but easy to do and reproduce. Also useful in lots of case.
function Enquote()
let [startline, endline]=sort([line("'<"), line("'>")])
let lines=getline(startline, endline)
let lengths=map(copy(lines), 'len(split(v:val, ''\zs''))')
let maxlen=max(lengths)
call map(lines, '''"''.v:val.repeat(" ", maxlen-lengths[v:key]).''"''')
return setline(startline, lines)
endfunction
Explanation:
line("'<") and line("'>") get the line numbers of start and end of last visual selection.
sort([...]) sorts this line numbers since you may have started selecting lines from the end of the selection.
let [a, b]=[c, d] is a parallel assignment: sort will produce a sorted list of two items, where first item is lesser or equal to second item. Obviously, lesser is a first selected line.
len(split(v:val, '\zs')) is an advanced strlen() which supports unicode.
max(list) finds a maximum value. Obvious.
So, map(copy(lines), 'len(split(v:val, ''\zs''))') applies this strlen to all items in list. copy() is required since we do not want our list to be modified.
map(lines, '''"''.v:val.repeat(" ", maxlen-lengths[v:key]).''"''') modifies an lines in a way you require. I switched from printf to repeat because printf does not handle multibyte characters correctly (by «correctly» I mean that «¥» is one character long, while printf considers it two bytes long).
setlines(linenumber, listoflines) actually modifies buffer.
Making use of the unix program "par" to do this may well solve your problem. There's a Vimcast showing how to integrate it into vim over at http://vimcasts.org/episodes/formatting-text-with-par/
Is it possible to make two passes over the list of lines in vim script? Then you can do something like this (pseudocode):
let N = length of longest line
for each line L:
insert a " character at the beginning
append N - len(L) spaces
append a " character
best i got is a 3-pass regex.
select block in visual mode, then use:
:'<,'>s#^#"#
:'<,'>s#$# #
:'<,'>s#\(.\{28\}\).*#\1"
with the 28 being the length of your longest line.
By all means heed the previous answers and get your vim-fu in shape. Or install/modify/poke-the-author of this plugin:
http://www.vim.org/scripts/script.php?script_id=4727
From the plugin's page:
This script converts multi-line text in a C++ file to a multi-line
string literal, escaping the quote and tab characters. It also does
the reverse conversion, un-escaping some characters. It's not too
complete for now, but it will be someday if needs come.
If you need to make changes use the source-code repository:
https://bitbucket.org/dsign/stringliteral.vim
In two passes:
let l = max(map(getline("'<", "'>"), 'strwidth(v:val)'))
'<,'>s/.*/\=('"'.submatch(0).repeat(' ', l-strwidth(submatch(0)) )).'"'