Is there a way to detect a Chinese character in a string which is build like this:
dim test as string = "letters 中國的"
Now I want to substring only the Chinese characters. But my code is database driven, so I can't substring it, because the length is always different. So is there a way I can split the string, from the moment I detect a Chinese character?
I think you can use regexp like in the following example, didn't test and I haven't code using VB.net for years So syntax may be not correct.
Dim m As Match = Regex.Match(value, "[\u4e00-\u9fa5]+",
RegexOptions.IgnoreCase)
' If successful, write the group.
If (m.Success) Then
Dim key As String = m.Groups(1).Value
End If
http://msdn.microsoft.com/en-us/library/twcw2f1c.aspx
Related
In vb.net, I am attempting to format a string into a formatted Phone Number where the formatting is read in from a database variable.
A phone number like "5555555555" will get nicely formated into "(555) 555-5555" if the string is formatted in this fashion:
String.Format("{0:(###) ###-####}", Long.Parse(PhoneNum))
However, the "(###) ###-####" string is stored in a database, to maintain a central formatting choice for phone numbers in a theoretical system.
Does anyone know how I can substitute this hardcoded formatting for one with a variable? I am trying something like:
Dim phoneFormat as String = <~read in the String format from DB~>
String.Format("{0:" & phoneFormat.ToString & "}", Long.Parse(PhoneNum)))
Unfortunately however, this only returns the string itself. I am presented with "(###) ###-####".
Object.ToString is probably simpler, but you can still use String.Format. Here are these two methods
Dim phoneNum = "1234567890"
Dim phoneFormat = "(###) ###-####"
' simpler version using Long.ToString
Dim formattedPhoneNumber1 = Long.Parse(phoneNum).ToString(phoneFormat)
' your original attempt using String.Format
Dim formattedPhoneNumber2 = String.Format("{0:" & phoneFormat & "}", Long.Parse(phoneNum))
' cleaner version using String.Format with traditional interpolation and $ interpolation
Dim formattedPhoneNumber3 = String.Format($"{{0:{phoneFormat}}}", CLng(phoneNum))
Console.WriteLine(formattedPhoneNumber1)
Console.WriteLine(formattedPhoneNumber2)
Console.WriteLine(formattedPhoneNumber3)
Console.ReadLine()
(123) 456-7890
(123) 456-7890
(123) 456-7890
I think what you were probably not sending the Long into the function, rather the string (by the way why is the number 5555555555 being stored as a string in the first place?). You need to include Long.Parse() otherwise the #s in your format won't know what to operate on.
I am successfully replacing a text in a file.
Dim fileReader As String = My.Computer.FileSystem.ReadAllText("C:\test.txt").Replace("foo", "bar")
My.Computer.FileSystem.WriteAllText("C:\test.txt", fileReader, False)
But how can I replace if I don't know the middle of the text ? For example
example_my("browser.taskbar.lastgroupid", "E7CF176E110C211B");
How to replace the E7CF176E110C211B ?
One way is to use RegEx. You can specify the first and last parts and then use \d+ or better [0-9A-F]+ to match any hex number. So given that the part that you don't know is a hexadecimal number, you can use the following code:
Dim fileReader As String = My.Computer.FileSystem.ReadAllText("C:\test.txt")
fileReader = System.Text.RegularExpressions.RegEx.Replace(fileReader, "[0-9A-F]+", "bar")
My.Computer.FileSystem.WriteAllText("C:\test.txt", fileReader, False)
This will replace all hexadecimal numbers in the file with the word "bar".
Note that for a text file this kind of global Replace can have undesirable results. For example the above regex will replace letters A to F even within normal text. So you'll probably want to put some kind of limitations such as min. number of digits to make sure it replaces only valid hex numbers. For example you may want to use [0-9A-F]{4,} which will replace only 4 or more consecutive hex digits. But even that is not 100% safe; e.g. it will replace DEAD, FACE, FADE, FACED etc. with the word "bar". To figure out exactly what is 100% safe for your file, you'll need to examine its contents carefully.
Edit
Reading you comments, you can use the following code instead:
Dim fileReader As String = My.Computer.FileSystem.ReadAllText("C:\test.txt")
fileReader = System.Text.RegularExpressions.RegEx.Replace(fileReader , "lastgroupid"", ""(.+)""\)", "lastgroupid"", ""ANYTHING_YOU_WANT""\)")
My.Computer.FileSystem.WriteAllText("C:\test.txt", fileReader, False)
This will replace all occurrances of hex numbers between lastgroupid", " AND ") with the string ANYTHING_YOU_WANT.
I want to get specific characters from listbox, but I don't know how to do it properly. I already used search (tried because I don't know how properly to name) but get nothing.
So i have this line in my listbox:
1,2014-01-01,Text,Text,XYZ123,Text,Text
How do i need to get only XYZ123? Its always same format, 3 letters and 3 numbers.
Thank you.
I would use a Regular Expression
The Regex of XYZ123 = \w{3}\d{3}
First solution:
Based on a small console application:
Dim i As String = "1,2014-01-01,Text,Text,**XYZ123**,Text,Text"
For Each Str As String In i.Split(",")
Dim match As Match = Regex.Match(Str, "\w{3}\d{3}")
If match.Success Then
Console.WriteLine(Str)
End If
Next
Console.ReadLine()
Second (better) solution:
Based on the comment of Chinz (all credits belong to him)
Dim i As String = "1,2014-01-01,Text,Text,**XYZ123**,Text,Text"
Console.WriteLine(Regex.Match(i, "\w{3}\d{3}").Value)
Console.ReadLine()
if all the strings have the same overall format you could split on "**" and get the [1] from the plitted
To start here is an example of a line I am trying to manipulate:
trait slot QName(PrivateNamespace("*", "com.company.assembleegameclient.ui:StatusBar"), "_-0IA") type QName(PackageNamespace(""), "Boolean") value False() end
I wrote a code that will go through and read through each line and stop at the appropriate line. What I am trying to achieve now is to read through the characters and save just the
_-0IA
to a new string. I tried using Trim(), Replace(), and indexof so far but I am having a ton of difficulties because of the quotation marks. Has anyone deal with this issue before?
Assuming your source string will always follow a strict format with only some data changes, something like this might work:
'Split the string by "," and extract the 3rd element. Trim the space and _
quotation mark from the front and extract the first 5 characters.
Dim targetstr As String = sourcestr.Split(","c)(2).TrimStart(" """.ToCharArray).Substring(0, 5)
If the length of the target string is variable it can be done like this:
Dim temp As String = teststr.Split(","c)(2).TrimStart(" """.ToCharArray)
'Use the index of the next quotation mark instead of a fixed length
Dim targetstr As String = temp.Substring(0, temp.IndexOf(""""c))
Hey guys I'm stuck with this question. Please help.
I want to write a program that can extract alphabetical characters and special characters from an input string. An alphabetical character is any character from "a" to "z"(capital letters and numbers not included") a special character is any other character that is not alphanumerical.
Example:
string = hello//this-is-my-string#capetown
alphanumerical characters = hellothisismystringcapetown
special characters = //---#
Now my question is this:
How do I loop through all the characters?
(the for loop I'm using reads like this for x = 0 to strname.length)...is this correct?
How do I extract characters to a string?
How do I determine special characters?
any input is greatly appreciated.
Thank you very much for your time.
You could loop through each character as follows:
For Each _char As Char In strname
'Code here
Next
or
For x as integer = 0 to strname.length - 1
'Code here
Next
or you can use Regex to replace the values you do not need in your string (I think this may be faster but I am no expert) Take a look at: http://msdn.microsoft.com/en-us/library/xwewhkd1.aspx
Edit
The replacement code will look something as follows although I am not so sure what the regular expression (variable called pattern currently only replacing digits) would be:
Dim pattern As String = "(\d+)?" 'You need to update the regular expression here
Dim input As String = "123//hello//this-is-my-string#capetown"
Dim rgx As New Regex(pattern)
Dim result As String = rgx.Replace(input, "")
Since you need to keep the values, you'll want to loop through your string. Keeping a list of characters as a result will come in handy since you can build a fresh string later. Then take advantage of a simple Regex test to determine where to place things. The psuedo code looks something like this.
Dim alphaChars As New List(Of String)
Dim specialChars As New List(Of String)
For Each _char As Char in testString
If Regex.IsMatch(_char, "[a-z]")) Then
alphaChars.Add(_char)
Else
specialChars.Add(_char)
End If
Next
Then If you need to dump your results into a full string, you can simply use
String.Join(String.Empty, alphaChars.ToArray())
Note that this code makes the assumption that ANYTHING else than a-z is considered a special character, so if needs be you can do a second regular expression in your else clause to test for you special characters in a similar manner. It really depends on how much control you have over the input.