String text to long value - string

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();

Related

How do i get an input of time in milliseconds?

private long START_TIME_IN_MILLIS;
private long TimeLeftInMillis = START_TIME_IN_MILLIS;
i have declared these variables, which is meant to store an input from an EditText, but in my OnCreateView i have this line
START_TIME_IN_MILLIS = edtInsertTime.getText();
but it gives me an error, how do I get the input of time in milliseconds and store it in START_TIME_IN_MILLIS?
You didn't post the error you received, and that would be helpful since it can be of hundred reasons..
Just looking at this code, you have variable of type long, ( which is a numeric type ), and what you get from edtInsetTime.getText()? , a type "Editable" according to the documentation, and you can't store Editable object in a long variable.
https://developer.android.com/reference/android/widget/EditText
public Editable getText ()
Returns Editable - The text displayed by the text view.
To get a long value from it, you first need to get a String from this EditText, then convert it to a long format, and then assign it to the variable.
Examplary code would be.
START_TIME_IN_MILLIS = Long.valueOf(edtInsertTime.getText().toString());
Breaking it down.
String text = edtInsertTime.getText().toString()
returns the String value of an edittext
That value then can be converted to a long value by parsing the String to long, with methods like
Long.parseLong(text)
or
Long.valueOf(text)
Asking this question means you don't have basic understanding of how types works in java, so feel free to read about it https://www.baeldung.com/java-primitives

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)

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

IndexOf bug when string contains 'AA'

I've run into a strange problem. I have a string with a value containing 'AA'.
I'm trying to find IndexOf the first accouring A. When I ask if the string Contains("A") it returns true. When using IndexOf("A") I keeps getting the default value -1! (se the picture below)
So far i tested there is only a problem with 'A' and 'a'.
When putting 3 a's in the string I get the index of number 3, as if the first two doesn't exsist.
When adding an extra a to the string, I get the default value -1 again.
I don't know what is causing this, I have a suspision that it's somehow connected to some langauge setting. I'm from denmark, and the use of the letters aa is a synonym for å.
Have anyone else experinced a simular problem or have a suggestion how to avoid it?
System information:
Windows 7 Ultimate (English)
Visual Studio 10 Premium
'aa' is handled as an entity if the culture is da-DK. The question is sort of a duplicate, see String StartsWith() issue with Danish text.
Hmmm I have tried the same now. It works...
static void XYZ()
{
string a = "aaa";
string b = "AAA";
if(a.Contains("a"))
{
Console.WriteLine(a.IndexOf("a"));
}
if(b.Contains("A"))
{
Console.WriteLine(b.IndexOf("A"));
}
}
But wouldn't it be the best to seach for a "aa" and "AA"? I can speak danish and I know that there are single a's too ;-)

Resources