VbScript to strip space of string? - string

Say I have a string that = Grilled Cheese
How would I echo the string without the space between Grilled Cheese with out changing the string?
Also if I had a string that variables but will always have a space, like a full name. I can show the first letter of the string:
WScript.Echo Left(strUserName, 1) will echo for example G
but how could I show the name after the space (= Cheese). Keep in mind the name after the space may change like "Cheesy" for example
WScript.Echo Mid(strUserName,null) does not work
Thanks!

The Replace function can be used to remove characters from a string. If you just echo the return value without assigning it back to the variable, the original string will remain unchanged:
>>> str = "Grilled Cheese"
>>> WScript.Echo Replace(str, " ", "")
GrilledCheese
>>> WScript.Echo str
Grilled Cheese
Another option would be the Split and Join functions:
>>> str = "Grilled Cheese"
>>> WScript.Echo Join(Split(str, " "), "")
GrilledCheese
>>> WScript.Echo str
Grilled Cheese
Split will also allow you to echo just particular tokens from the original string:
>>> str = "Grilled Cheese"
>>> WScript.Echo Split(str, " ")(1)
Cheese

Related

Re Arrangement position of String(sentence) without using split() method

I have a string like this str , we have to change its position like str2 without using split() method , Is it possible to code in python .
str = "This is stackoverflow "
I need like this , str2 = " is this stackoverflow"

Split string by a space

I have one string that has two words in it:
Cat Dog
How can I split these up so I get:
Str1 = Cat and Str2 = Dog
Please note this is for VB6.
Use the Split function.
Dim output() As String
output = Split("Cat Dog", " ")
Would produce
output(0) = Cat
output(1) = Dog
or like this:
For Each i In output
.... a even here
next

How to check for occurence of name in string?

I have a list of names separated by space in one variable like this:
blacklist = "name1 name2 name3 etc"
What I want is to check existing of some specified name in this list. Like
if nameInBlacklist("player_name") == true then
dosomething()
end
Suppose that the name you want to find is in the string name, you can use:
if (" " .. blacklist .. " "):find(" " .. name .. " ", 1, true) then
-- do something
end
Note that the fourth parameter true is to turn off pattern matching facilities, so that it's OK for name to contain some special characters that need to escape.
If you need to use a function:
function nameInBlacklist(name)
return (" " .. blacklist .. " "):find(" " .. name .. " ", 1, true)
end
Don't compare the return value with true, just use it as the condition directly:
if nameInBlacklist("player_name") then
--do something
end
If you had a long blacklist, you would convert it to a table keyed by the list entries. A short blacklist can easily be checked by string matching:
if (" "..blacklist.." "):find( " "..player_name.." ", 1, true ) then
doSomething()
end
The last parameter to find turns on plain string matching for find.

How to reduce all whitespace in a string to single spaces?

Using VB6 without any additional references such as Regex, how can I convert a string so that all whitespace in a string is reduced to single spaces?
eg.
" A B C D E"
would be converted to
"A B C D E"
Function NormalizeSpaces(s As String) As String
Do While InStr(s, String(2, " ")) > 0
s = replace(s, String(2, " "), " ")
Loop
NormalizeSpaces = s
End Function
(derived from: http://www.techrepublic.com/article/normalizing-spaces-in-vb6-strings/5890164)

How can I concatenate strings in a cell array with spaces between them in MATLAB?

I want to concatenate (padding with spaces) the strings in a cell array {'a', 'b'} to give a single string 'a b'. How can I do this in MATLAB?
You can cheat a bit, by using the cell array as a set of argument to the sprintf function, then cleaning up the extra spaces with strtrim:
strs = {'a', 'b', 'c'};
strs_spaces = sprintf('%s ' ,strs{:});
trimmed = strtrim(strs_spaces);
Dirty, but I like it...
matlab have a function to do this,
ref:
strjoin
http://www.mathworks.com/help/matlab/ref/strjoin.html
strjoin
Join strings in cell array into single string
Syntax
str = strjoin(C) example
str = strjoin(C,delimiter)
Ex:
Join List of Words with Whitespace
Join individual strings in a cell array of strings, C, with a single space.
C = {'one','two','three'};
str = strjoin(C)
str =
one two three
Small improvement (?) on the answer by Alex
strs = {'a','b','c'};
strs_spaces = [strs{1} sprintf(' %s', strs{2:end})];
You can accomplish this using the function STRCAT to append blanks to all but the last cell of your cell array and then concatenate all the strings together:
>> strCell = {'a' 'b' 'c' 'd' 'e'};
>> nCells = numel(strCell);
>> strCell(1:nCells-1) = strcat(strCell(1:nCells-1),{' '});
>> fullString = [strCell{:}]
fullString =
a b c d e
Both join and strjoin are introduced in R2013a. However, the mathworks site about strjoin reads:
Starting in R2016b, the join function is recommended to join elements of a string array.
>> C = {'one','two','three'};
>> join(C) %same result as: >> join(C, ' ')
ans =
string
"one two three"
>> join(C, ', and-ah ')
ans =
string
"one, and-ah two, and-ah three"
Personally I like Alex' solution as well, as older versions of Matlab are abundant in research groups around the world.

Resources