Adding numbers from in a string - python-3.x

I have a string that says:
calculation = "10+3-2"
print(calculation) # This should equal 11
I can't figure out a way the string so Python would do the math. I have tried splitting the string etc but nothing worked.
Can someone please help? Thanks.

Try using the eval() function:
calculation = eval("10+3-2")
print(calculation)
or
calculation = "10+3-2"
print(eval(calculation))

Related

String.Split() method creates empty array spot

I am not a professional programmer, just FYI.
I am having trouble splitting a string. The resulting array has a length of 3, with the second spot (index 1) being completely empty.
I could manipulate the array to work the way I'd like, but I would rather understand why it is acting this way and code it properly from the beginning.
Dim defaultSetting() As String
Dim curSetting as String = "MENU_ITEM_ON_OPEN;;OPTIONAL_LEAVE"
defaultSetting = curSetting.Split(";;")
MsgBox(defaultSetting.Length) 'this is 3
MsgBox(defaultSetting(0)) 'this is as expected "MENU_ITEM_ON_OPEN"
MsgBox(defaultSetting(1)) 'this is empty and I do not know why
MsgBox(defaultSetting(2)) 'this is "OPTIONAL_LEAVE" and should be stored in defaultSetting(1)
Any help would be appreciated, thank you.
The problem here is that Option Strict is set to Off.
The overload of Split which is used expects a ParamArray of Char.
Because of this, the string ";;" is "silently" converted to a single char.
You can check this with following code:
Dim x As Char = ";;"
MsgBox(x)
You want to split by a string, which means you have to use another overload:
defaultSetting = curSetting.Split({";;"}, StringSplitOptions.None)
Thanks to a comment made by dbasnett I was able to find code that worked the way I was expecting, although I am not really sure why if anyone care to explain. But if not, this question has been answered, thanks.
defaultSetting = testString.Split(CType(";;", Char()), StringSplitOptions.RemoveEmptyEntries)

How to extract the characters from a string in Excel

Hi I would like to extract dynamically the numbers from string in Excel.
I have the following strings and I would like to have only the numbers before ". pdf". taken out of the string into the next column.
As you can see the number of characters varies from line to line.
I have invented something like this:
=MID(M20;SEARCH("_";M20);20)
But this takes out only the numbers after "_" and .pdf after this....
How to make it the way I like?
D:\Users\xxxx\Desktop\1610\ts25b_4462.pdf
D:\Users\xxx\Desktop\1610\ts02b_39522.pdf
D:\Users\xxxxx\Desktop\1610\ts02b_except_39511.pdf
D:\Users\xxxx\Desktop\1610\ts02b_except_39555.pdf
D:\Users\xxxx\Desktop\1610\ts22b_6118.pdf
So that I have just :
4462
39522
39511
39555
6118
and so on...
Thank you!!!
With VBA, try to do it like this:
Public Function splitThings(strInput As String) As String
splitThings = Split(Split(strInput, "_")(1), ".")(0)
End Function
Concerning your formula, try to use =LEFT(MID(M20;SEARCH("_";M20);20),K), where K is the difference of the length of ts22b_6118.pdf and 4 (.pdf). 4 is the length of .pdf.
Something like this should do the work:
=LEFT(MID(I3,SEARCH("_",I3)+1,LEN(I3)),LEN(MID(I3,SEARCH("_",I3),LEN(I3)))-5)
You should do it using Excel formula. For example:
=SUBSTITUTE(LEFT(A1,FIND(".pdf",A1)-1),LEFT(A1,FIND("_",A1)),"")
Using the first line as an example, with LEFT(A1,FIND(".pdf",A1)-1) you will have D:\Users\xxxx\Desktop\1610\ts25b_4462 and with the LEFT(A1,FIND("_",A1)) D:\Users\xxxx\Desktop\1610\ts25b_, if you SUBSTITUTE the first part by "" you will have 4462.
Hope this can help.
With this formula, you should be able to get the numbers you want:
=MID(A1,FIND("|",SUBSTITUTE(A1,"_","|",LEN(A1)-LEN(SUBSTITUTE(A1,"_",""))))+1,FIND(".",A1)-FIND("|",SUBSTITUTE(A1,"_","|",LEN(A1)-LEN(SUBSTITUTE(A1,"_",""))))-1)
Basically, this is the initial fomula:
=MID(A1,FIND("_",A1)+1,FIND(".",A1)-FIND("_",A1)-1)
But since there may be two _ in the string so this is the one to find the 2nd _:
=SUBSTITUTE(A1,"_","|",LEN(A1)-LEN(SUBSTITUTE(A1,"_","")))
Now just replace this SUBSTITUTE with A1 above and you get that long formula. Hope this helps.
This will return the number you want regardless of extension (could be .pdf, could be .xlsx, etc) and regardless of the number of underscores present in the filename and/or filepath:
=TRIM(LEFT(RIGHT(SUBSTITUTE(SUBSTITUTE(M20,".",REPT(" ",LEN(M20))),"_",REPT(" ",LEN(M20))),LEN(M20)*2),LEN(M20)))

vb.net Unable to keep decimal places when converting string to single

I have been working on a vb.net project and have run into a problem. I have tried various implementations from Stackoverflow and MSDN but nothing is working. All I am trying to do is convert a string value to a single and keep the precision.
An example of the code would be something like this:
Dim Total As Single = 0
Dim s as String = "427.00"
Total += Single.Parse(s)
// Total = 427
// Expected : 427.00 <-- I need this
I have tried using cultureinfo.invariant,
I have tried using string.format,
I have tried using double instead of single,
I don't know what I am missing.
Any insight would be appreciated, and I can provide more code of what the application is trying to do if necessary.
In addition to #Alex B.'s comment, this is how you would achieve this. Total is a String but the program will bomb if either is not a Single type giving you some type safety.
Dim Total As String = "0"
Dim s as String = "427.00"
Total = (Single.Parse(Total) + Single.Parse(s)).ToString("0.00")

Get handles as a number in order to apply function and operate mathematicaly

The program asks for input in gui editbox as a value, then it takes this value and applies the equation to get the pressure. I haven't been able to do so and I heard from some classmates that matlab takes the input as a string and doesn't operate strings.
get(handles.spl,'String') this is how I get the value, I tried get(handles.spl,'Double') instead but it didn't work, also tried str2double.
I don't know what else to try, I'm also pretty new in programming.
I'd appreciate the help, thanks.
You are correct that the uicontrol String property returns...a string. So you'll need to convert it to a number using str2double.
u = uicontrol('style', 'edit', 'String', '42');
strvalue = get(u, 'String');
numvalue = str2double(strvalue);
% 42

String text to long value

Well, thats it!
I need to convert a string text (like"Hrd$457"), into a long value.
The blackberry IDE has a button that do it, but i need do this by code.
Please note that the string is alpha numeric.
THX!
NOTE:
Sorry if my question was not really clear. The IDE button that im talkin about converts the entire string in a long value that makes that string a unique number. The BlackBerry documentation says:
"To create a unique long key, in the BlackBerry® Integrated Development Environment, type a string value.
com.rim.samples.docs.userinfo
Right-click the string and click Convert ‘com.rim.samples.docs.userinfo’ to long."
So, i need to do exactly the same but by code.
I really appreciate your help buddies, and thanks so much for trying to help.
If you are just looking for a number constant for a string you can do the following.
String str = "asdfasdf345asdfasdf";
int asInt = str.hashCode();
long asLong = (long) asInt;
Returns the first 8 bytes of a SHA1 digest as a long. The same result can be obtained interactively using the BlackBerry JDE by highlighting a string, right-clicking, and choosing "Convert '' to long" from the context menu.
long net.rim.device.api.util.StringUtilities.stringHashToLong(String key)
This is another approach. If there are multiple numbers you can loop through the String using the scanner.
Scanner scanner = new Scanner(str);
scanner.useDelimiter("\\D+");
Long number = scanner.nextLong();
Not sure I fully grasp your example, but how's this?
String match = Pattern.compile("\\d+").matcher("Hrd$457").group();
long longValue = Long.parseLong(match).longValue();

Resources