473,703 Members | 2,656 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to extract just the letters from a string

6 New Member
Im trying to take only alphabetical characters out of what the users input to the text field (txtinput.text) ..
So if i put in "The yellow man had 32 teeth!"
then im trying to get results like" Theyellowmanhad teeth"
anything besides characters should be stripped out of the inputed field.

this is in visual basic
Apr 26 '07 #1
14 21949
boyntonboy242000
6 New Member
Hi im replying to this because im trying to take only alphabetical characters out of what the users input to the text field (txtinput.text) .. So if i put in "The yellow man had 32 teeth!" then im trying to get results like" Theyellowmanhad teeth" anything besides characters should be stripped out of the inputed field.
Apr 26 '07 #2
Killer42
8,435 Recognized Expert Expert
Hi im replying to this because im trying to take only alphabetical characters out of what the users input to the text field (txtinput.text) .. So if i put in "The yellow man had 32 teeth!" then im trying to get results like" Theyellowmanhad teeth" anything besides characters should be stripped out of the inputed field.
The simple way would be to loop (probably using a For loop) through each character in the string (most likely using Mid() function) and if it is a letter, append it to another, output string.

Psedudo-code...
Expand|Select|Wrap|Line Numbers
  1. For Each Character In MyInput
  2.   If Ucase(Character) is between "A" and "Z"
  3.     MyOutput = MyOutput & Character
  4.   End If
  5. Next
Oh, and by the way - you can't really copy "anything besides characters" because they're all characters.
Apr 26 '07 #3
SammyB
807 Recognized Expert Contributor
Hi im replying to this because im trying to take only alphabetical characters out of what the users input to the text field (txtinput.text) .. So if i put in "The yellow man had 32 teeth!" then im trying to get results like" Theyellowmanhad teeth" anything besides characters should be stripped out of the inputed field.
I'm speechless: Killer didn't use Replace! Just brute-force it:
Expand|Select|Wrap|Line Numbers
  1.     s = Replace(s, 1, "")
  2.     s = Replace(s, 2, "")
  3.     s = Replace(s, 3, "")
  4. ...
  5.  
and, you can put it in a loop so that it is only three lines.

But, also do it Killer's way so that you can learn how to use the Mid function.

BTW, if you're using VB.NET, these functions are part of the Strings module, so you say Strings.Mid(... ) and Strings.Replace (...)
Apr 27 '07 #4
Killer42
8,435 Recognized Expert Expert
I think it would be a bit more than three lines, Sammy. Or are you assuming that letters and numbers are the only possible characters?
Apr 27 '07 #5
dip_developer
648 Recognized Expert Contributor
Im trying to take only alphabetical characters out of what the users input to the text field (txtinput.text) ..
So if i put in "The yellow man had 32 teeth!"
then im trying to get results like" Theyellowmanhad teeth"
anything besides characters should be stripped out of the inputed field.

this is in visual basic
Take two strings say strChar and strNumber

Read the length of your string....Loop through your string.....With the Substring() function Read characters one by one........Chec k characters with IsNumeric() Function whether its a number or not.....if number then strip off the character and store it to strNumber Otherwise store it to strChar.......

after the loop completes strChar will be your string without number.....
Implement it in code.......

it will be Something like....

Expand|Select|Wrap|Line Numbers
  1. Dim strChar,strNumber As String
  2. Dim n As Integer=myString.Length
  3. For i as integer=0 to n-1
  4. dim str as string=myString.Substring(i,i+1)
  5. If IsNumeric(str) then
  6. strNumber+=str
  7. else
  8. strChar+=str
  9. End if
  10. Next
  11. MsgBox(strChar)
Apr 27 '07 #6
Killer42
8,435 Recognized Expert Expert
...Check characters with IsNumeric() Function whether its a number or not.....if number then strip off the character and store it to strNumber Otherwise store it to strChar.......
What happens to spaces, and punctuation?
Apr 28 '07 #7
Geoff
17 New Member
I'd do this with ascii characters for each letter, similar to what was said earlier with between "a" and "z".

This is proabably over complicating the problem quite a bit, but it works, it keeps the letters in the case they were found in as well ^^

Expand|Select|Wrap|Line Numbers
  1.  
  2.     Dim strText As String, strLeft As String, strRight As String
  3.     Dim intA As Integer
  4.     strText = Text1.Text
  5.     For intA = 1 To Len(strText)
  6.         strLeft = Left(strText, intA)
  7.         strRight = Right(strLeft, 1)
  8.  
  9.         If Asc(strRight) > 97 And Asc(strRight) < 122 Then
  10.             Label1.Caption = Label1.Caption & strRight
  11.         End If
  12.         If Asc(strRight) > 65 And Asc(strRight) < 90 Then
  13.             Label1.Caption = Label1.Caption & strRight
  14.         End If
  15.     Next intA
  16.  
  17.  
Hope this helps.
Apr 28 '07 #8
Killer42
8,435 Recognized Expert Expert
I have a similar problem. can you guys help?
...
If I'm not mistaken, this sounds like a question for the Access forum. Would you like me to move it over there? You're likely to get better information there, if this is SQL-related.
Apr 29 '07 #9
Killer42
8,435 Recognized Expert Expert
I'd do this with ascii characters for each letter, similar to what was said earlier with between "a" and "z".
...
Looks good.

I do have a couple of issues with it, just on performance grounds. There seems to be a lot of duplicated effort.

For example, why use Left() then Right() function? Is this faster than the Mid() function? (this is a serious question - sometimes the longer coding can be more efficient). Or perhaps it's just a personal preference?

And your second If test doesn't need to be executed if the first was satisfied. Personally, I'd short-cut the process by using ElseIf or similar. Plus I would place the Asc() value into a variable so the function is only called once.

In fact, one of the main reasons I like to use Select Case for these kind of situations is that it allows you much more flexibility in your conditions (if checking one value). For example, where you are doing two IF tests, I would have used something like...
Expand|Select|Wrap|Line Numbers
  1. Select Case Asc(strRight)
  2.   Case 97 To 122, 65 To 90
  3.     ...
  4.  
However, while this seems (to me) "nicer" to code, I don't know how it compares on performance. Must check it out one of these days...

Oh, and one more point - I would always use a Long in favour of an Integer, unless space was very tight. Believe it or not, Long data type is (very slightly) faster to use on a 32 bit processor.
Apr 29 '07 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

3
6555
by: Bernard Drolet | last post by:
Hi, I have a column containing a string; the string always starts with a letter (a-z), followed by an undefined number of letters, then one number or more. The REGEXP would look like *+ I need to extract the letters at the beginning in an SQL query or in PL/SQL
5
102526
by: rs | last post by:
I have a table with a timestamp field which contains the date and time. ie. 9/13/2004 9:10:00 AM. I would like to split this field into 2 fields, one with just the DATE portion ie 9/13/2004 and the other with just the TIME portion. ie 9:10:00 AM. I can make the table view display what I want by placing the same data in 3 fields and setting the display property to 'General Date',' Medium Time' and 'Short Date' but the underlying data...
3
3518
by: zek2005 | last post by:
Hi friends! I have a varchar field in my DB with numeric values separates by spaces. I need to extract the numbers to create an array. Example 1: 1820 1823 1825 --> need to be transform into 1820 1823 1825
6
1838
by: giloosh | last post by:
Hello, how can i extract a websites html into a string. also, is there a limit to how many chars a string can hold? something like: $string = extracted_html($url); thanks for any help!
3
18373
by: deko | last post by:
I'm sure someone has passed this way before... I want to check to see is a domain name is contained in a string, and if one is, I want to extract it. In these strings, domains are always preceded by "http://" or "http : //www" (without the spaces). in pseudo code, I thought it might look like this: if (eregi("http: //", $mystring)) {
4
1462
by: pigeonrandle | last post by:
Quickie time! Is indexing using String to get individual characters 'as quick' as converting the String to a Char and then using Char? Is the conversion to Char worth the processing? In advance, i thank you, James Randle.
5
17947
markmcgookin
by: markmcgookin | last post by:
Hi Folks, I am writing a program to analyse an html page in java, I am connecting to a website, then going to extract ALL the links from it. I think the best way to do this is using the <a href... /a> tags as a guideline. I have the code.... String data1; DataInputStream webadd = null;
7
1723
by: blitzztriger | last post by:
hello all , i need help at this: <? //connection stuff $conexao = mysql_connect("localhost", "root", "12345") or die ("error to db."); $db = mysql_select_db("mal") or die ("error selecting db."); // some regex $subject = $_POST ;
1
1620
by: GS | last post by:
I need to extract sections out of a long string of about 5 to 10 KB, change any date format of dd Mmm yyyy to yyyy-mm-dd, then further from each section extract columns of tables. what is the best approach in using regex for this? I can see match and replace the dates, extract section with regex, and then for each section extract again with the right regex the tables....
2
1633
by: rajesh0303 | last post by:
I want to extract substring from string and replace them by character. .ex:In "lphrd" , I want to extract and ,and want them to replace be replaced by Ä and É. so, that the result string is ÄlprÉd. Need help...
0
8739
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8654
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9089
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8983
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6575
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5910
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4668
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2406
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2037
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.