Is there method startswith in xojo like in Java or Vb.Net? - textfield

I try to learn xojo. Is there any method Startswith in xojo just like in Java and Vb.net? If there is, how should I implement it?
Thank you.

In the old framework there is no equivalent so you will have to code it yourself using Left.
Strings in Xojo are, by default, case insensitive and this would work:
Function BeginsWith(extends aString as string, startString as String) As Boolean
Return ( Left( aString, len( startString))=startString )
End Function
If you want case sensitivity you could so this:
Function BeginsWithNoCase(extends aString as string, startString as String) As Boolean
dim s1,s2 as string
s1 = Uppercase( aString)
s2 = Uppercase( startString)
Return ( Left( s1, len( s2))=s2 )
End Function
This and more at https://forum.xojo.com/19302-is-there-method-startswith-in-xojo/0#p162011

Use Text.BeginsWith. It supports both case-insensitive and case-sensitive comparisons.

Related

VB.net regex exclude certain characters from string

I am using regex.ismatch to check a string doesn't contain any one of a list of characters such as £&+(/?!;:* And also a quotation mark " not sure how to place that...
But can't get to to work...
If Regex.ismatch(Line, "^[^##£&+()*']"). Then
Msgbox("error")
End If
But doesn't work for me?
Any suggestions
You could do this pretty easily without Regex by simply doing something like this:
Public Shared Function HasSpecialChars(ByVal str As String) As Boolean
Const specialChars As String = "!##$%^&*()"
Dim indexOf As Integer = str.IndexOfAny(specialChars.ToCharArray())
Return indexOf <> -1
End Function

How can I convert a string into different characters?

I have looked on the web and I cannot find anything that helps me, all I can find is changing the characters into ASCII or Hexadecimal. However I would like to do it a different way. For example, say the string that got passed in was abcdef, I would like to have a key which changes these characters into another string such as qwpolz. Is there an easier way than declaring each character in the alphabet to be another character like:
Dim sText As String = "Hello"
Dim sEncode As String = ""
Dim iLength As Integer
Dim i As Integer
iLength = Len(sText)
For i = 1 To iLength
sEncode = sEncode ????
Next
Return sEncode
And then have a very lengthy loop which checks for these loops? There must be a much simpler way. Can anybody help by pointing me in the right direction?
Edit: Why downvote? Seriously, it's a legitimate question. Instead of downvoting for no reason, just move onto another question.
Well actually, this sounds like a Caesar sipher
Private Overloads Shared Function Encrypt(ByVal ch As Char, ByVal code As Integer) As Char
If Not Char.IsLetter(ch) Then
Return ch
End If
Dim offset As Char = IIf(Char.IsUpper(ch), "A", "a")
Return CType((((ch + (code - offset)) Mod 26) + offset),Char)
End Function
Private Overloads Shared Function Encrypt(ByVal input As String, ByVal code As Integer) As String
Return New String(input.ToCharArray.Select(() => { }, Encrypt(ch, code)).ToArray)
End Function
Private Shared Function Decrypt(ByVal input As String, ByVal code As Integer) As String
Return Encrypt(input, (26 - code))
End Function
Note that this assumes, that you use English alphabet. In general case where for example you have 'ä', 'ö', 'š', 'ž', 'ß', 'õ', 'ü' etc. this would not work. In that case it is simpler to just create a list/dictionary of your ordered alphabet and use it.
Example use:
encrypted = Encrypt(sText, 5)
decypted = Decrypt(encrypted, 5)
Sounds as if you want to modify a string by replacing each character with a different character according to a mapping table. An efficient approach is to use a Dictionary(Of Char, Char). But easier to write and maintain is something like this:
Shared ReadOnly replaceChars As String = "abcdef"
Shared ReadOnly withChars As String = "qwpolz"
Public Shared Function ReplaceAll(input As String) As String
Dim newChars = From c In input
Let index = replaceChars.IndexOf(c)
Select If(index >= 0, withChars(index), c)
Return String.Concat(newChars)
End Function
So the first string contains the chars that you want to replace and the second the replacement characters. Both strings must have the same length.
If you want to support case insensitivity:
Public Shared Function ReplaceAll(input As String, comparison As StringComparison) As String
Dim newChars = From c In input
Let index = replaceChars.IndexOf(c.ToString(), comparison)
Select If(index >= 0, withChars(index), c)
Return String.Concat(newChars)
End Function
Note that this is also a loop. There is no way to avoid some kind of loops if you want to replace multiple characters or strings.

Basic VB troubles

I'm simply wondering what symbol/character I can use to define any character in a string...
Basically I have a number of records with RR 2, RR#2, RR1, RR 1, etc. and I want to use a symbol that will define anything after the RR and replace it with nothing "". I know in SQL it's the "%" symbol, but not sure in VBA.
I am using the Replace function in ArcGIS field calculator.
I tried searching but cannot come up with the right question to find the answer I'm looking for.
Any ideas?
Since it's unclear if you want VBA or VB.Net,
Here's a VBA answer just use the ChopString function using the format shown in the Test sub:
Function ChopString(str As String, after As String, Optional caseInsensitive As Boolean = True) As String
Dim x As Long
If caseInsensitive Then
x = InStr(1, str, after, vbTextCompare)
Else
x = InStr(1, str, after, vbBinaryCompare)
End If
If x Then
str = Left(str, x + Len(after) - 1)
End If
ChopString = str
End Function
Sub Test()
Dim OriginalString As String
Dim choppedString As String
OriginalString = "1234RR this will be chopped"
choppedString = ChopString(OriginalString, "RR")
MsgBox choppedString
End Sub
Sadly the .net REPLACE() function doesn't support wildcard characters, you can use a function as described here but it's a bit long winded.

VB.NET string manipulation -- caps to only one letter caps

So I have a string "NEW".
What is the SIMPLEST way to convert that string to "New".
Basically right now I'm doing this:
Case "NEW"
makes = connector.GetMakesByYear(_AuthorizationKey, "NewCar", CDate(Now), Year)
Case "USED"
makes = connector.GetMakesByYear(_AuthorizationKey, "UsedCar", CDate(Now), Year)
And I would prefer not to use a case statement because it's only one parameter that needs to change, and both are appended with "Car".
Using the “old” string functions, you can use this:
result = StrConv("hello world", VbStrConv.ProperCase)
to convert a string to “proper case”. However, in your case this would probably result in (if I read this right) “Usercar”, not “UserCar”.
You may use:
String.Format("{0}{1}", carType.Substring(0, 1).ToUpper(), carType.Substring(1).ToLower())
Regards
If this is something you plan on using often, you might consider creating an extension function for it:
Public Module ObjectExtensions
<System.Runtime.CompilerServices.Extension()>
Public Function firstLetterToUpper(ByVal s As String) As String
Return Char.ToUpper(s.First()) + New String(s.Skip(1).Select(Function(x) Char.ToLower(x)).ToArray())
End Function
End Module
Then you can do something like this:
"USED".firstLetterToUpper()
Which returns "Used"
Obviously you can change the function body with something more efficient like Guilherme's or Konrad's answer, but making an extension function for this can be quite useful if you do plan on doing something like this often, or if you are just a fan of readability.
Here what I have done!
Function InitUpperCase(ByVal str As String) As String
If String.IsNullOrEmpty(str) Then
Return str
End If
Dim charlist() As Char = str.ToCharArray
charlist(0) = Char.ToUpper(charlist(0))
Return New String(charlist)
End Function
to see Output
MessageBox.Show(InitUpperCase("my first letter"))

Last but one Char in vb.net String

How do I find last but one character in a vbstring
for e.g. In the string V1245-12V0 I want to return V
Don't use substring to get just one character
Dim MyString As String = "V1245-12V0"
Dim MyChar As Char = MyString(MyString.Length - 2)
Sorry it's been a while since I did VB so this may not be perfect (and is probably a mixture of C# and VB) but you get the idea:
Dim s = "V1245-12V0"
Dim lastButOneLetter = String.Empty
If s.Length > 1 Then
'Can only get the last-but-one letter from a string that is minimum 2 characters
lastButOneLetter = s.Substring(s.Length - 2, 1)
Else
'do something if string is less than 2 characters
End If
EDIT: fixed to be compilable VB.NET code.
Dim secondToLastChar As Char
secondToLastChar = Microsoft.VisualBasic.Strings.GetChar(mystring, mystring.Length - 2)
http://msdn.microsoft.com/en-us/library/4dhfexk4(VS.80).aspx
Or just remember that any string is an array of chars;
secondToLastChar = mystring(mystring.Length - 2)
If you want to get the last alpha-character in a string you could use a LINQ query such as (C#):
var d = from c in myString.ToCharArray().Reverse()
where Char.IsLetter(c)
select c;
return d.First();
string.Substring(string.Length - 2, 1);
Was it difficult?
dim mychar as string
dim yourstring as string
yourstring="V1245-12V0"
mychar=yourstring.Substring(yourstring.Length - 2, 1)
Use the Substring on the string s which contains 'V1245-12V0'
s.Substring(s.Length - 2, 1);
Here's a VB solution:
Dim text = "V1245-12V0"
Dim v = Left(Right(text, 2), 1)
You do not need to check the length of text, except for your semantics as to what you want to happen for empty (and Nothing) and single character strings.
You can have your own functions like
Function Left(ByVal str as string, byval index as integer) As String
Left=str.Substring(0,index);
End Function
Function Right(ByVal str as string, byval index as integer) As String
Right=str.Substring(str.Length-index)
End Function
And use them to get what you need.