Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 146
Default Going through a string, a char at a time.

Hello is it possible to go through a String checking for certain characters?

Basically want to somehow convert a String to a character array and go
through it checking for the ' character. Or failing that, is there a function
that disposes of certain characters within a string.

Cheers.

Nate
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6
Default Going through a string, a char at a time.

Hi,

It's not very pretty but it works for me;

Sub removechar()

Dim i, l As Integer
Dim mystr1, mystr2 As String

mystr1 = "how to remove ' from a string"
l = Len(mystr1)

For i = 1 To l
If Mid(mystr1, i, 1) < "'" Then
mystr2 = mystr2 & Mid(mystr1, i, 1)
End If
Next i

End Sub

Ammend to sub or UDf as neccessary.
Hopefully someone else will have a sexier solution!


"NateBuckley" wrote:

Hello is it possible to go through a String checking for certain characters?

Basically want to somehow convert a String to a character array and go
through it checking for the ' character. Or failing that, is there a function
that disposes of certain characters within a string.

Cheers.

Nate

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,489
Default Going through a string, a char at a time.

Hi,

You can remove characters from a string by using the replace function

Msgbox Replace("checking for the ' character","'","")

Cheers
Andy
--

Andy Pope, Microsoft MVP - Excel
http://www.andypope.info
"NateBuckley" wrote in message
...
Hello is it possible to go through a String checking for certain
characters?

Basically want to somehow convert a String to a character array and go
through it checking for the ' character. Or failing that, is there a
function
that disposes of certain characters within a string.

Cheers.

Nate


  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 146
Default Going through a string, a char at a time.

Hehe, well It may not be sexy but it gets the job done.

Thank yee kindly good sir/madam. (Circle which applies).

"Flower" wrote:

Hi,

It's not very pretty but it works for me;

Sub removechar()

Dim i, l As Integer
Dim mystr1, mystr2 As String

mystr1 = "how to remove ' from a string"
l = Len(mystr1)

For i = 1 To l
If Mid(mystr1, i, 1) < "'" Then
mystr2 = mystr2 & Mid(mystr1, i, 1)
End If
Next i

End Sub

Ammend to sub or UDf as neccessary.
Hopefully someone else will have a sexier solution!


"NateBuckley" wrote:

Hello is it possible to go through a String checking for certain characters?

Basically want to somehow convert a String to a character array and go
through it checking for the ' character. Or failing that, is there a function
that disposes of certain characters within a string.

Cheers.

Nate

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,345
Default Going through a string, a char at a time.

This may not be what you are looking for but to remove a letter, (or
character if not a letter), from a cell try:

=SUBSTITUTE(A1,"e","")

To count the number of occurrences of the character use:

=LEN(A1)-LEN(SUBSTITUTE(A1,"e",""))

--
HTH

Sandy
In Perth, the ancient capital of Scotland
and the crowning place of kings


Replace @mailinator.com with @tiscali.co.uk


"NateBuckley" wrote in message
...
Hello is it possible to go through a String checking for certain
characters?

Basically want to somehow convert a String to a character array and go
through it checking for the ' character. Or failing that, is there a
function
that disposes of certain characters within a string.

Cheers.

Nate





  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Going through a string, a char at a time.

A "sexier" subroutine might be...

Sub RemoveApostrophe()
Dim mystr1 As String, mystr2 As String
mystr1 = "how to remove ' from a string"
mystr2 = Replace(mystr1, "'", "")
End Sub

By the way, note the difference between my Dim statement for mystr1 and
mystr2 and yours. Leaving out the As String declaration for mystr1 make it a
Variant, not a String variable. In VB(A), all variables must be declared as
to their type or else they get declared as a default of Variant (this
differs from other languages you may be familiar with).

Rick


"Flower" wrote in message
...
Hi,

It's not very pretty but it works for me;

Sub removechar()

Dim i, l As Integer
Dim mystr1, mystr2 As String

mystr1 = "how to remove ' from a string"
l = Len(mystr1)

For i = 1 To l
If Mid(mystr1, i, 1) < "'" Then
mystr2 = mystr2 & Mid(mystr1, i, 1)
End If
Next i

End Sub

Ammend to sub or UDf as neccessary.
Hopefully someone else will have a sexier solution!


"NateBuckley" wrote:

Hello is it possible to go through a String checking for certain
characters?

Basically want to somehow convert a String to a character array and go
through it checking for the ' character. Or failing that, is there a
function
that disposes of certain characters within a string.

Cheers.

Nate


Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to retrieve certain char within a string? Eric Excel Discussion (Misc queries) 1 July 3rd 07 03:02 AM
How do i get the last position of a char (space) in a string? EsPoNjOsO Excel Worksheet Functions 1 October 12th 06 11:55 AM
sql string, +'/'+convert(char(3)..) doristsr Excel Programming 3 June 29th 06 08:34 AM
string/char CONTAINS function? jim sturtz Excel Worksheet Functions 3 May 31st 06 07:20 PM
last char of a string Buddy Lee Excel Programming 9 November 11th 05 02:02 AM


All times are GMT +1. The time now is 07:03 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"