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"
Related
I have two predefined arrays, say:
first = ["alpha" "beta"];
second = ["{" "}"];
and I want to create a function which receives a string and splits the string in different string arrays(or cell). Each array(or cell) should contain either a single member of one of the two arrays, a variable that is not a member of the arrays (without including the blank space) or a string. Ex:
Input string:
"alpha{ beta} new {} new2 "This is a string" }"
Output string:
"alpha" "{" "beta" "new" "{" "}" "new2" "This is a string" "}"
Hope it is clear!
Bests,
Stergios
I tried this:
S = "alpha{ beta} new new2 {} new3}";
T = ["alpha","beta", "{","}"];
[X,Y] = strsplit(S,[T " "], 'CollapseDelimiters',false);
X = strtrim(X); % you forgot to mention, that you also want to remove whitespace
X(2,:) = [Y,""];
X(strlength(X)==0) = []
but S does not accept strings of strings and if I use '' every word will be in a different cell!
I am having a number of strings in the loop, some strings contain "," and some doesn't contain, I want my code to check if there is any "," present in a string remove them and then print the string, and if its not present, print the string as it is.
here is my code:
for x in range(y):
c = containers[x].find("div",{"class":"cong-data"})
meeting = c.p.next_element
print(meeting)
Thanks in advance.
I recommend using Regular Expression sub() function.
import re
string = 'aabcdefg,hijklmnop,qrstu,ssads'
remove_commas = re.sub(',', '', string)
print(string)
print(remove_commas)
output:
aabcdefg,hijklmnop,qrstu,ssads
aabcdefghijklmnopqrstussads
I have text in Excel like this:
120
124569 abasd 12345
There are sapces both to the left and to the right side.
I copy this from Excel and paste as text. When I check this, it shows like this when I click on button.
Code:
abArray= abArray & "," & gridview1.Rows(i).Cells(2).Text
For k = 3 To 17
bArray= abArray& "," & Val(gridview1.Rows(i).Cells(k).Text)
Next
In abArray this shows as:
0, abasd ,12345,0,0,0,0,0
I want to remove/trim spaces both from left and right.
I have tried abArray.Trim() but this still show spaces.
If you want to remove all the spaces out of the end result consider String.Replace:
Returns a new string in which all occurrences of a specified Unicode character or String in the current string are replaced with another specified Unicode character or String.
Example use:
Dim s As String = "0, abasd ,12345,0,0,0,0,0"
s = s.Replace(" ", "")
This would output:
0,abasd,12345,0,0,0,0,0
It may also be worth using a StringBuilder to join all your values together as this is good practice when looping as you are. At this point you could use String.Trim. This would preserve any spaces that are within your value. In order words it would only remove the spaces from the beginning and the end of the value.
Example use:
Dim sb As New StringBuilder
For k = 0 To 17
sb.Append(String.Format("{0},", gridview1.Rows(i).Cells(k).Text.Trim()))
Next
Dim endResult As String = sb.ToString().TrimEnd(","c)
endResult would output:
0,abasd,12345,0,0,0,0,0
You will have to import System.Text in order to make use of the StringBuilder class.
Use the VB.NET Trim function to remove leading and trailing spaces, change this one line of code:
abArray= abArray& "," & Val(Trim(gridview1.Rows(i).Cells(k).Text))
abArray.Trim() does not work because you did not give the Trim function anything to trim.
Try it like this
abArray = abArray & "," & gridview1.Rows(i).Cells(2).Text.Trim
For k = 3 To 17
abArray= abArray& "," & Val(gridview1.Rows(i).Cells(k).Text.Trim)
Next
For an example, lets say I have this table:
tbl = {"hi ", "my ", "name ", "is ", "King"}
Can I get this to return:
"hi my name is King"
Without
for k, v in ipairs( tbl )
print(v)
end
Because I am trying to process an unknown quantity of inputs, and compare the result to another string.
You can use table.concat() to get the result string:
local str = table.concat(tbl)
print(str)
It can do more, in particular, table.concat() takes a second optional parameter, which can be used as the separator, for instance, to use commas to separate each elements:
local str = table.concat(tbl, ',')
The biggest advantage of table.concat() versus direct string concatenation is performance, see PiL ยง11.6 for detail.
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