We help IT Professionals succeed at work.

Can i replace string without match case in C#

aimds
aimds asked
on
309 Views
Last Modified: 2013-12-17
Can i replace string without match case in C#?
for Example :
input  = " Test Replace string"
input = input.Replace("test","yyy");
input  should be  "yyy Replace string"
HOw can i do ?

Thanks
Comment
Watch Question

CERTIFIED EXPERT

Commented:
 input.ToLower.Replace("test","yyy");//you will have to account for variants.
CERTIFIED EXPERT

Commented:
if you always want to test for upper and lower variants you can ToLower your test string to if you put it to a variable


String replaceText = "Test";

  str.ToLower().Replace(replaceText.ToLower(),"yyy");

sorry forgot the ToLower() on my first post -- it is a method


Author

Commented:
input = input.Replace("test","yyy");
If i tolower input
it will be "yyy replace string" ==> that mean all my old text becase lower case
can i replace and the old text  still original case ?
Top Expert 2009

Commented:
CERTIFIED EXPERT

Commented:
to answer that i would have to know you criteria for replacing text and capitalization.  but the quick answer is yes.
CERTIFIED EXPERT

Commented:
you will probably have to write your own replace method or possibly just use regex -- depending on what you are trying to do.

Author

Commented:
i did not see any solution in this link http://dotnetperls.com/replace
can u specify?

Thanks
CERTIFIED EXPERT

Commented:
there isn't a solution for your question there, nor at the other link.

Author

Commented:
sorry i did not tell about  input came from
 input = client.DownloadString(URL);
i read source from  URL
then just want to replace some link and then save it back

Author

Commented:
No way to do?
CERTIFIED EXPERT

Commented:
im working on it with what you have given.
Top Expert 2009

Commented:

>> there isn't a solution for your question there, nor at the other link.

I read your question.

Two zones included
I gave examples belonging to both zones.

Later figured out that aimds needed in C#, So I let you do all the talking.
Technical Manager
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
CERTIFIED EXPERT

Commented:
its not that, and i wasn't trying to be rude. -- asker wants to replace the string but then put a qualifier on it saying that they want to keep casing -- i was referring to the casing problem.
CERTIFIED EXPERT

Commented:
without having more info, i can only use what you have given me and this is what i have come up with


it takes the replacement yyy and checks the index of the found work to see if that character is upper case -- if it is then the corresponding index on the replacement is capitalized.
String org = "Test Replace string";
            String word = "test";
            String replacement = "yyy";
            Int32 index = org.ToLower().IndexOf(word);
 
            if (index != -1)
            {
                Int32 i = 0;
                for(i = 0;i < replacement.Length;i++)
                {
                    if (char.IsUpper(org[index + i]))
                    {
                        replacement = replacement.Remove(i, 1).Insert(i, char.ToUpper(replacement[i]).ToString());
                    }
                }
            }

Open in new window

Author

Commented:
Thank you for every answer
i trid regex.replace  
and it already work not

Author

Commented:
it already work now
CERTIFIED EXPERT

Commented:
i thought you said that you weren't able to get regex to work???
CERTIFIED EXPERT

Commented:
nm

Author

Commented:
regex.replace   work
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.