how to remove non character letter from name field using visual foxpro - visual-foxpro

I have not used Visual FoxPro for a while. Today, my ex-colleague asks me how to remove non character from name field, i.e. only a-z and A-Z are allowed. I remember I used a function called strstran to do it. I needed to define a variable contains a-z and A-Z. But I do not remember now. Does someone knows how to handle this problem. Thanks in advance.

Use the CHRTRAN() function.
FUNCTION GetAlphaCharacters
LPARAMETERS tcExpressionSearched
LOCAL lcAllowedCharacters
m.lcAllowedCharacters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
RETURN CHRTRAN(m.tcExpressionSearched, CHRTRAN(m.tcExpressionSearched, m.lcAllowedCharacters, ""), "")
ENDFUNC

Another option is to use ISALPHA(). This only looks at the left most position in the string but it's not case sensitive.
***This should work, but I haven't tested it.
myresults = ""
myvar = "MyText12"
FOR(i = 1 TO LEN(myvar))
IF ISALPHA( SUBSTR(myvar, i, 1) )
myresults = myresults + SUBSTR(myvar, i, 1)
ENDIF
ENDFOR
RETURN myresults

I know I'm a bit late to the party, but here is a function I wrote to clean out all non-printable ASCII characters from a character string.
CLEAR
* Contains ASCII characters 1 (SOH) and 2 (STX)
cTest = "Garbage Data "
? cTest
cTest = RemoveNonPrintableCharacters(cTest)
? cTest
FUNCTION RemoveNonPrintableCharacters
LPARAMETERS tcExpressionSearched
cCleanExpression = tcExpressionSearched
* Cleans out the first 32 ASCII characters, which are not printable
FOR decCount = 0 TO 31
cCleanExpression = CHRTRAN(m.cCleanExpression, CHR(decCount), "")
ENDFOR
* Also cleans out the non-printable DEL character (ASCII 127)
cCleanExpression = CHRTRAN(m.cCleanExpression, CHR(127), "")
* Return the clean string
RETURN cCleanExpression
ENDFUNC

Related

Check if string "starts with" another string

I want to check if an address starts with http://www.youtube.com.
If I have something like this
if rs("mainVideoName")="http://www.youtube.com*" then
This doesn't work, so how can I do it?
Try this:
Function UrlStartsWith(string1, string2)
UrlStartsWith = InStr(1, string1, string2, 1) = 1
End Function
If UrlStartsWith(rs("mainVideoName"), "http://www.youtube.com") Then
End If
Starts with is tested by using IntStr and ensuring that it returns 1 as the starting position that the search string is found. Since you are testing a URL the code above uses a TextCompare to make insensitive to case.
You can use the InStr() function for this:
Dim positionOfMatchedString
positionOfMatchedString= InStr(rs("mainVideoName"),"http://www.youtube.com")
If positionOfMatchedString > 0 Then
// Do your stuff here
End If
As Anthony points out, this tells you that string2 is contained in string1. You could write it as:
If positionOfMatchedString = 1 Then
to check for it beginning with.
How about...
Dim s: s = "http://www.youtube.com"
Dim l: l = Len(s)
If Left(rs("mainVideoName"), l) = s Then
' String begins with URL part '
End If

VB6 - converting string to double using specific culture?

I am a long time C# developer and have had to look at some VB6 code lately. After some digging, we found out that, somewhere, somehow, we are storing a number as a string. Now we are reading it back and our code is not handling culture differences very well. Here is an example:
Dim text as String
Dim myVal as Double
Set text = "1,123456"
'This sets the value of myVal to 1123456 on our system - incorrect
myVal = text
Set text = "1.123456"
'This sets the value of myVal to 1.123456 on our system - correct
myVal = text
Keeping in mind that this is VB6 and NOT VB.NET, are there any built-in methods, functions, whatever, that can convert a string to a double using a particular culture?
Or at the very least, hinting to the conversion that we may be dealing with a different format?
We are still digging how the value gets written and see if we can reverse engineer the process to give us a consistent result. However, our customer(s) already has(have) data in one format or the other (or both, we are checking...), so a proper conversion algorithm or implementation would be a better answer at this point.
I've used this quick and dirty function in the past to get a double from a text. Here in Argentina, people sometimes use the point and sometimes the comma to separate decimal values, so this function is ready to accept both formats. If only one punctuation is present, it's assumed that it's the decimal separator.
function ConvertDBL( texto )
dim retval, SepDecimal, SepMiles
If texto = "" then texto = "0"
retval = replace(trim(texto), " ", "")
If cdbl("3,24") = 324 then
SepDecimal = "."
SepMiles = ","
else
SepDecimal = ","
SepMiles = "."
end if
If InStr( Retval, SepDecimal ) > 0 then
If InStr( Retval, SepMiles ) > 0 then
If InStr( Retval, SepDecimal ) > InStr( Retval, SepMiles ) then
Retval = replace( Retval, SepMiles, "" )
else
Retval = replace( Retval, SepDecimal, "" )
Retval = replace( Retval, SepMiles, SepDecimal )
end if
end if
else
Retval = replace( Retval, SepMiles, SepDecimal )
end if
ConvertDbl = cdbl( retval )
end function
You cannot choose an arbitrary culture by passing a culture object like you can in .Net. You can choose:
Functions which always use the current regional settings. CStr, Format, CDbl etc. Implicit conversions (like the ones in your question) also use the current regional settings.
Functions which always use USA regional settings (similar to the .Net invariant culture). Str, Val
Usually the current regional settings are taken from Control Panel. There are rumours that you can call SetThreadLocale to change the regional settings from code at runtime - never tried it myself.
In Visual Basic tested works very fast and efficient.
Step 1: Conversion from a string to a value representation.
Step 2: Conversion from a value representation to a double integer value.
Dim Str as String
Dim mValue as double
str = "100,10"
mValue = CDbl(Val(str))

VIM: Why is this function hanging VIM?

I have added the following fine function to my status bar to show which function is currently being edited in C-derived languages:
set statusline+=%{WhatFunctionAreWeIn()}
fun WhatFunctionAreWeIn()
let strList = ["while", "foreach", "ifelse", "if else", "for", "if", "else", "try", "catch", "case"]
let foundcontrol = 1
let pos=getpos(".") " This saves the cursor position
let view=winsaveview() " This saves the window view
while (foundcontrol)
let foundcontrol = 0
" Go to the character before the last open {
normal [{
call search('\S','bW')
" If the character is a ) then go to the character
" preceding the () section
let tempchar = getline(".")[col(".") - 1]
if (match(tempchar, ")") >=0 )
normal %
call search('\S','bW')
endif
let tempstring = getline(".")
for item in strList
if( match(tempstring,item) >= 0 )
let foundcontrol = 1
break
endif
endfor
if(foundcontrol == 0)
call cursor(pos)
call winrestview(view)
return tempstring
endif
endwhile
call cursor(pos)
call winrestview(view)
return tempstring
endfun
However, after a few minutes VIM hangs. Disabling the function prevents the hang, so I feel confident that this function is to blame. Is there anything in there that might hang VIM? Is there a better way to accomplish the task of showing the currently-edited function in the status bar?
Thanks.
The issue is that your strategy for determining whether to keep moving to surrounding braces is too aggressive:
Suppose your cursor is on the f in a preprocessor directive #endif between two functions.
Since you're between two functions, there is no unmatched { for [{ to jump to, so the cursor doesn't move.
Your match() against strList hits the if in #endif, causing the loop to continue.
The loop never exits.
I suspect a ctags-based approach, like #Gowtham suggests, will work better, even if it requires some customization.

Converting Decimal to ASCII Character

I am trying to convert an decimal number to it's character equivalent. For example:
int j = 65 // The character equivalent would be 'A'.
Sorry, forgot to specify the language. I thought I did. I am using the Cocoa/Object-C. It is really frustrating. I have tried the following but it is still not converting correctly.
char_num1 = [working_text characterAtIndex:i]; // value = 65
char_num2 = [working_text characterAtIndex:i+1]; // value = 75
char_num3 = char_num1 + char_num2; // value = 140
char_str1 = [NSString stringWithFormat:#"%c",char_num3]; // mapped value = 229
char_str2 = [char_str2 stringByAppendingString:char_str1];
When char_num1 and char_num2 are added, I get the new ascii decimal value. However, when I try to convert the new decimal value to a character, I do not get the character that is mapped to char_num3.
Convert a character to a number in C:
int j = 'A';
Convert a number to a character in C:
char ch = 65;
Convert a character to a number in python:
j = ord('A')
Convert a number to a character in Python:
ch = chr(65)
Most languages have a 'char' function, so it would be Char(j)
I'm not sure what language you're asking about. In Java, this works:
int a = 'a';
It's quite often done with "chr" or "char", but some indication of the language / platform would be useful :-)
string k = Chr(j);

how do you parse a string in vb6?

Some of us unfortunately are still supporting legacy app like VB6. I have forgotten how to parse a string.
Given a string:
Dim mystring As String = "1234567890"
How do you loop in VB6 through each character and do something like
for each character in mystring
debug.print character
next
In C# i would do something like
char[] myChars = mystring.ToCharArray();
foreach (char c in theChars)
{
//do something with c
}
Any ideas?
Thanks a lot
You can use the 'Mid' function to get at the individual characters:
Dim i As Integer
For i = 1 To Len(mystring)
Print Mid$(mystring, i, 1)
Next
Note this is untested.
There is no possibility to use foreach on strings.
Use
Dim i As Integer
For i = 1 To Len(YourString)
Result = Mid$(YourString, i, 1)
Next
note that the type of Result is a length-1 string, no char or byte type.
If performance is important, you'll have to convert the string to a bytearray fist (using StrConv) and then loop through it like this.
Dim i As Long
For i = 0 To UBound(Data)
Result = Data(i) ' Type is Byte '
Next
This is much more efficient.
The easiest way is to convert the string into an array of bytes and iterate over the byte array (converting each byte to a character).
Dim str As String
Dim bytArray() As Byte
Dim count As Integer
str = "This is a string."
bytArray = str
For count = 0 To UBound(bytArray)
Debug.Print Chr(bytArray(count))
Next
Don't loop; rather, set a reference to Microsoft VBScript Regular Expressions library and use regular expressions to achieve your 'do something' goal.

Resources