473,385 Members | 1,356 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

best way for Replace insensitive in strings

I need to replace all the occurences of a string within another string
(or stringbuilder):

Function ReplaceInsensitive(ByVal InputString As String, _
ByVal SubstringReplaced As
String, _
ByVal Replacement As
String) As String
'...
End Function

I wish the search for "SubstringReplaced" to be case insensitive.

I can think of various way to do it, but all I can think is quite
awkward. Can anybody suggest a good and fast method? There must be
something built in the language, but I am missing it

-Pam

Jun 13 '06 #1
10 9562
pa***********@libero.it wrote:
I wish the search for "SubstringReplaced" to be case insensitive.


\\\
Function ReplaceInsensitive(ByVal InputString As String, _
ByVal SubstringReplaced As String,
_
ByVal Replacement As String) As
String

Return Replace(InputString, SubStringReplaced, _
Replacement, Compare:=CompareMethod.Text)

End Function
///

--

(O)enone
Jun 13 '06 #2
NOT TESTED

Function ReplaceInsensitive(ByVal InputString As String, _
ByVal
SubstringReplaced As String, _
ByVal Replacement As
String) As String

Return InputString.Replace(SubstringReplaced, Replacement)
End Function
pa***********@libero.it wrote:
I need to replace all the occurences of a string within another string
(or stringbuilder):

Function ReplaceInsensitive(ByVal InputString As String, _
ByVal SubstringReplaced As
String, _
ByVal Replacement As
String) As String
'...
End Function

I wish the search for "SubstringReplaced" to be case insensitive.

I can think of various way to do it, but all I can think is quite
awkward. Can anybody suggest a good and fast method? There must be
something built in the language, but I am missing it

-Pam


Jun 13 '06 #3

The version provided by (O)enone works fine. Thanks for the help!

(Ahmed suggestion is on the right track, omitting the CompareMethod is
like: Compare:=CompareMethod.Binary)
Any hint, instead, for the case when a StringBuilder is used?

Sub ReplaceInsensitive(ByVal Sb As System.Text.StringBuilder, _
ByVal SubstringReplaced As String, _
ByVal Replacement As String)
'...
End Sub
Thank you very much.

-Pam

Jun 13 '06 #4
Samething,
sb.Replace(substringreplaced, replacement)
pa***********@libero.it wrote:
The version provided by (O)enone works fine. Thanks for the help!

(Ahmed suggestion is on the right track, omitting the CompareMethod is
like: Compare:=CompareMethod.Binary)
Any hint, instead, for the case when a StringBuilder is used?

Sub ReplaceInsensitive(ByVal Sb As System.Text.StringBuilder, _
ByVal SubstringReplaced As String, _
ByVal Replacement As String)
'...
End Sub
Thank you very much.

-Pam


Jun 13 '06 #5
mmm ... no, remember the condition that it must be "case insensitive".
I don't see an option to specify it with the StringBuilder Replace
method (but I may be missing it (?) ).

-P

Ahmed ha scritto:
Samething,
sb.Replace(substringreplaced, replacement)
pa***********@libero.it wrote:
The version provided by (O)enone works fine. Thanks for the help!

(Ahmed suggestion is on the right track, omitting the CompareMethod is
like: Compare:=CompareMethod.Binary)
Any hint, instead, for the case when a StringBuilder is used?

Sub ReplaceInsensitive(ByVal Sb As System.Text.StringBuilder, _
ByVal SubstringReplaced As String, _
ByVal Replacement As String)
'...
End Sub
Thank you very much.

-Pam


Jun 13 '06 #6
Return Replace(sb.tostring, SubStringReplaced, _
Replacement, Compare:=CompareMethod.Text)

pa***********@libero.it wrote:
mmm ... no, remember the condition that it must be "case insensitive".
I don't see an option to specify it with the StringBuilder Replace
method (but I may be missing it (?) ).

-P

Ahmed ha scritto:
Samething,
sb.Replace(substringreplaced, replacement)
pa***********@libero.it wrote:
The version provided by (O)enone works fine. Thanks for the help!

(Ahmed suggestion is on the right track, omitting the CompareMethod is
like: Compare:=CompareMethod.Binary)
Any hint, instead, for the case when a StringBuilder is used?

Sub ReplaceInsensitive(ByVal Sb As System.Text.StringBuilder, _
ByVal SubstringReplaced As String, _
ByVal Replacement As String)
'...
End Sub
Thank you very much.

-Pam


Jun 14 '06 #7
mmm ... Nice try Ahmed, but note that we are working with a
StringBuilder (infact, I proposed a *Sub* and *not* a Function). The
usefulness of a stringbuilder is that we have an immutable string, if
we convert it to a string, there is no point in using a StringBuilder.

-Pam

Ahmed ha scritto:
Return Replace(sb.tostring, SubStringReplaced, _
Replacement, Compare:=CompareMethod.Text)


Jun 14 '06 #8
Hello Pam-o,

Yer changing schtuff in the string by the very definition of "replace", so
your precious immutable string.. aint so immuted anymore.

-Boo
mmm ... Nice try Ahmed, but note that we are working with a
StringBuilder (infact, I proposed a *Sub* and *not* a Function). The
usefulness of a stringbuilder is that we have an immutable string, if
we convert it to a string, there is no point in using a StringBuilder.

-Pam

Ahmed ha scritto:
Return Replace(sb.tostring, SubStringReplaced, _
Replacement, Compare:=CompareMethod.Text)

Jun 14 '06 #9
Pamela,

If it are only words with a first upercase, I would just do it twice.

Otherwise Regex will be your needed tool.

RegexLib
http://www.regexlib.com/Default.aspx

Expresso
http://www.ultrapico.com/Expresso.htm

It needs a real hobbyist to help you then with your problem.

I hope this helps a little bit?

Cor
<pa***********@libero.it> schreef in bericht
news:11**********************@f6g2000cwb.googlegro ups.com...
mmm ... no, remember the condition that it must be "case insensitive".
I don't see an option to specify it with the StringBuilder Replace
method (but I may be missing it (?) ).

-P

Ahmed ha scritto:
Samething,
sb.Replace(substringreplaced, replacement)
pa***********@libero.it wrote:
> The version provided by (O)enone works fine. Thanks for the help!
>
> (Ahmed suggestion is on the right track, omitting the CompareMethod is
> like: Compare:=CompareMethod.Binary)
>
>
> Any hint, instead, for the case when a StringBuilder is used?
>
> Sub ReplaceInsensitive(ByVal Sb As System.Text.StringBuilder, _
> ByVal SubstringReplaced As String, _
> ByVal Replacement As String)
> '...
> End Sub
>
>
> Thank you very much.
>
> -Pam

Jun 14 '06 #10

A special award to who finds the most efficient method :)

(grabbed from the web, easily adaptable to stringbuilder logic)

-tom

'--- Custom Replace Function CReplace
'--- VB.NET Loop Version
'--- intMode = 0 = Case-Sensitive
'--- intMode = 1 = Case-Insensitive
Function CReplace(ByVal strExpression As String, _
ByVal strSearch as String, _
strReplace As String, _
intMode as Integer _
) As String
Dim strReturn as String
Dim lngPosition As Long
Dim strTemp As String

If intMode = 1 Then '--- vbTextCompare
strReturn = ""
strSearch = strSearch.ToUpper()
strTemp = strExpression.ToUpper()
lngPosition = strTemp.IndexOf(strSearch)
Do While lngPosition >= 0
strReturn = strReturn + strExpression.SubString(0,lngPosition) +
strReplace
strExpression =
strExpression.SubString(lngPosition+strSearch.Leng th)
strTemp = strTemp.SubString(lngPosition+strSearch.Length)
lngPosition = strTemp.IndexOf(strSearch)
Loop
strReturn = strReturn + strExpression
Else
'--- vbBinaryCompare
strReturn = strExpression.Replace(strSearch,strReplace)
End If

CReplace = strReturn
End Function


'--- Custom Replace Function CReplace
'--- VB.NET Recursive Version
'--- intMode = 0 = Case-Sensitive
'--- intMode = 1 = Case-Insensitive
Function CReplace( strExpression As String, _
strSearch as String, _
strReplace As String, _
intMode as Integer _
) As String
Dim strReturn as String
Dim lngPosition As Long
Dim strTemp As String

If intMode = 1 Then '--- vbTextCompare
strTemp = strExpression.ToUpper()
lngPosition = strTemp.IndexOf(strSearch.ToUpper())
If lngPosition >= 0 Then
strTemp = strExpression.Remove(lngPosition,strSearch.Length)
strTemp = strTemp.Insert(lngPosition,strReplace)
strReturn = CReplace(strTemp,strSearch,strReplace,intMode)
Else
strReturn = strExpression
End If
Else
'--- vbBinaryCompare
strReturn = strExpression.Replace(strSearch,strReplace)
End If

CReplace = strReturn
End Function

'--- Custom Replace Function CReplace
'--- VB.NET RegExp Version
'--- intMode = 0 = Case-Sensitive
'--- intMode = 1 = Case-Insensitive
Public Function CReplace(strExpression As String, _
ByVal strSearch As String, _
strReplace As String, _
intMode as Integer _
) As String

Dim strReturn As String
Dim lngPosition As Long
Dim strTemp as String

If intMode=1 Then strSearch=GetCaseInsensitiveSearch(strSearch)

strReturn = Regex.Replace(strExpression,strSearch,strReplace)

CReplace = strReturn

End Function

' Creates a case-insensitive regular expression search string
' For Example:
' "[fF][oO][oO][bB][aA][rR]"= GetCaseInsensitiveSearch("FooBar")
Public Function GetCaseInsensitiveSearch(strSearch As String) As String
Dim strReturn As New String(")
Dim chrCurrent As char
Dim chrLower As char
Dim chrUpper As char
Dim intCounter As Integer

For intCounter = 0 To strSearch.Length-1

chrCurrent=strSearch.Chars(intCounter)
chrLower = char.ToLower(chrCurrent)
chrUpper = char.ToUpper(chrCurrent)
If chrUpper = chrLower Then
strReturn = strReturn + chrCurrent
Else
strReturn = strReturn + "[" + chrLower + chrUpper + "]"
End If
Next
GetCaseInsensitiveSearch = strReturn
End Function
for instance:

http://authors.aspalliance.com/bbilb...=7&ArticleID=4


Cor Ligthert [MVP] ha scritto:
Pamela,

If it are only words with a first upercase, I would just do it twice.

Otherwise Regex will be your needed tool.

RegexLib
http://www.regexlib.com/Default.aspx

Expresso
http://www.ultrapico.com/Expresso.htm

It needs a real hobbyist to help you then with your problem.

I hope this helps a little bit?

Cor
<pa***********@libero.it> schreef in bericht
news:11**********************@f6g2000cwb.googlegro ups.com...
mmm ... no, remember the condition that it must be "case insensitive".
I don't see an option to specify it with the StringBuilder Replace
method (but I may be missing it (?) ).

-P

Ahmed ha scritto:
Samething,
sb.Replace(substringreplaced, replacement)
pa***********@libero.it wrote:
> The version provided by (O)enone works fine. Thanks for the help!
>
> (Ahmed suggestion is on the right track, omitting the CompareMethod is
> like: Compare:=CompareMethod.Binary)
>
>
> Any hint, instead, for the case when a StringBuilder is used?
>
> Sub ReplaceInsensitive(ByVal Sb As System.Text.StringBuilder, _
> ByVal SubstringReplaced As String, _
> ByVal Replacement As String)
> '...
> End Sub
>
>
> Thank you very much.
>
> -Pam


Jun 14 '06 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

20
by: hagai26 | last post by:
I am looking for the best and efficient way to replace the first word in a str, like this: "aa to become" -> "/aa/ to become" I know I can use spilt and than join them but I can also use regular...
1
by: ajay.sonawane | last post by:
How can I find the wheather the occurance substring is main string in wide char provided that the compasion should be case insensitive.
1
by: Ron James | last post by:
I have a Listbox containing strings. When calling Contains (), I would like a case insensitive comparison. (I don't want to add newfile.txt to a Listbox containing NewFile.txt). I'm...
1
by: Rasika Wijayaratne | last post by:
Hello, Can I get feedback on these .NET coding best practices please. Thanks to Konrad Rudolph and Jon Skeet for the replies to my previous post on 'Writing Properties.' Thanks in advance to all...
5
by: Gerrit Beuze | last post by:
Hi all, Using C# 1.1: I need a fast way of determining whether a string is in a list of approx 150 keyword like strings. in two versions: one case-sensitive, the other one case-insenstive. I...
1
by: Raterus | last post by:
Howdy, I'm making a search engine for an application I'm working on. I want to "bold" keywords they have searched for. I have a long string of words that represents a particular document, and I...
3
by: kd | last post by:
Hi All, How to perform case-insensitive comparision of strings? Would there be some kind of an indicator, which when set to true, would allow case-insenitive comparision of strings using...
7
by: Adrian | last post by:
Hi, I want a const static std::set of strings which is case insensitive for the values. So I have the following which seems to work but something doesnt seem right about it. Is there a better...
4
hsriat
by: hsriat | last post by:
function repAll(t) { var finds = new Array(......); //array of to-be-replaced strings var repls = new Array(......); //array of replace-with strings t = t.replace(finds, repls, 'gi'); } ...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.