1

I am trying to insert a string at a position for C# string, its failing

here is the snippet.

  if(strCellContent.Contains("<"))
   {
         int pos = strCellContent.IndexOf("<");
         strCellContent.Insert(pos,"&lt;");
   }

please tell me the solution

2
  • 2
    And would you be as kind to explain how it's failing? Commented Mar 23, 2011 at 7:06
  • First thing, you should clear that you want to insert new string by replacing "<" or concatenate new string without replacing < Commented Mar 23, 2011 at 7:19

6 Answers 6

7

The return value contains the new string that you desire.

strCellContent = strCellContent.Insert(pos,"&lt;");
Sign up to request clarification or add additional context in comments.

Comments

7

Gunner and Rhapsody have given correct changes, but it's worth knowing why your original attempt failed. The String type is immutable - once you've got a string, you can't change its contents. All the methods which look like they're changing it actually just return a new value. So for example, if you have:

string x = "foo";
string y = x.Replace("o", "e");

the string x refers to will still contain the characters "foo"... but the string y refers to will contain the characters "fee".

This affects all uses of strings, not just the particular situation you're looking at now (which would definitely be better handled using Replace, or even better still a library call which knows how to do all the escaping you need).

Comments

2

I think you might be better of with a Replace instead of an Insert:

strCellContent = strCellContent.Replace("<", "&lt;");

Maybe doing Server.HtmlEncode() is even better:

strCellContent = Server.HtmlEncode(strCellContent);

1 Comment

+1. Manual search/replace is a terrible idea in face of something like HtmlEncode (assuming he has the ability to call that). The Xml library also has similar auto-escaping features.
1

When I look at your code I think you want to do a replace, but try this:

 if(strCellContent.Contains("<"))    
 {
      int pos = strCellContent.IndexOf("<");
      strCellContent = strCellContent.Insert(pos,"&lt;");
} 

Comments

0

.Contains is not a good idea here, because you need to know the position. This solution will be more efficient.

int pos = strCellContent.IndexOf("<");
if (pos >= 0)   //that means the string Contains("<")
{
   strCellContent = strCellContent.Insert(pos,"&lt;");  //string is immutable
}

Comments

0

As others have explained with the code, I will add that

The value of the String object is the content of the sequential collection, and that value is immutable (that is, it is read-only). For more information about the immutability of strings, see the Immutability and the StringBuilder Class section.

from: http://msdn.microsoft.com/en-us/library/system.string.aspx

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.