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)
Related
I need to get my cell content in excel worksheet to another worksheet by using a user defined function. So I searched stackoverflow and found the following vba code.
Function GetValue(sheetName As String, cellAddress As String) As Variant
GetSheetValue = ThisWorkbook.Sheets(sheetName).Range(cellAddress)
End Function
This is the right thing i expected.But it doesn't work for me. I need to develop a user defined function as like follows.
=GetContent(Sheet1,B6)
I've tried many things but I can't do it. So I'm very appriciate any responses about this matter. (Provide Screenshots about my example)
Thank You
ScreenShot 02
ScreenShot 03
I've found the answer. My function must be correct as following code.
Function GetContent(sheetName As String, cellAddress As String) As String
GetContent = ActiveWorkbook.Sheets(sheetName).Range(cellAddress)
End Function
I didn't concern abot the function name and return as the same. But we have to must do it. Also in string data type we have use quotaions and Case sensitive. So thanks all of you for valuable help. It helps me to fid out the answer. Now wecan use the functions as follows.
=GetContent("Sheet2","b5")
Thank You!
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
I need to validate for valid code name.
So, my string can have values like below:
String test = "C000. ", "C010. ", "C020. ", "C030. ", "CA00. ","C0B0. ","C00C. "
So my function needs to validate below conditions:
It should start with C
After that next 3 characters should be numeric before .
Rest it can be anything.
So in above string values, only ["C000.", "C010.", "C020.", "C030."] are valid ones.
EDIT:
Below is the code I tried:
if (nameObject.Title.StartsWith(String.Format("^[C][0-9]{3}$",nameObject.Title)))
I'd suggest a regex, for example (written off the top of my head, may need work):
string s = "C030.";
Regex reg = new Regex("C[0-9]{3,3}\\.");
bool isMatch = reg.IsMatch(s);
This regex should do the trick:
Regex.IsMatch(input, #"C[0-9]{3}\..*")
Check out http://www.techotopia.com/index.php/Working_with_Strings_in_C_Sharp
for a quick tutorial on (among other things) individual access of string elements, so you can test each element for your criteria.
If you think your criteria may change, using regular expressions gives you maximum flexibility (but is more runtime intensive than regular string-element evaluation). In your case, it may be overkill, IMHO.
In a Visual Basic 6.0 program, I have a string sTemp that I want to ensure does not contain a quotation mark. I have the line:
If sTemp.Contains("""") Then
But when I type the period after sTemp, I don't get anything from intellisense, and when I try to compile I get the following error:
Compile error:
Invalid qualifier
VB6 strings are not objects, so there are no methods on the string variable that you can call.
To test does the string contain quotes you need to use the InStr function i.e.
if InStr(sTemp, """") > 0 then ' string contains at least one double quote
Hope this helps
UPDATE This has nothing to do with the original question
William, I just thought of this, it is unrelated information that you may find useful.
There are many ways to shoot yourself in the foot with VB6.
Among the less obvious is the fact that
Dim myCollection as new Collection
will have side effects you could never imagine.
Never DIM something AS New CSomething
Dim your variable, then on a second line, assign it to a new object. Hope this helps.
Dim myCollection as Collection
Set myCollection = New Collection
Try if instr(sTemp, """") > 0 then
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();