string -- changing/reading 'x' char within a string
How do I edit/read 'x' character within a string variable?
I am using the following Session variable as follows:
Session.Add("PageGlobals", string.Empty);
sample usage is that there are number of 'flags' that need to be keep trak off within the page, so when page refresh, these 'flags' values are not reset etc.
Session("PageGlobals", ..... character 1 change to "1" for example)
then change character 5 to "0" etc...
.NET Programming
Last Comment
avnish_tanna
8/22/2022 - Mon
Solar_Flare
you should be able to set values in the session by
((string)Session["PageGlobals"])[5] = 'x';
and you can read them with
char c = ((string)Session["PageGlobals"])[5];
amillyard
ASKER
Solar_Flare: I am getting a compiler as follows:
((string)Session["PageGlobals"])[1] = '1';
Property or indexer 'string.this[int]' cannot be assigned to -- it is read only
amillyard
ASKER
the '1' does not need to be an int (as have a row of 1's and 0's in the same string -- representing various flag states, i.e. yes or no)
Try using this;
Session["PageGlobals"])[1] = '1';
If you have all the flag in Session["PageGlobals"]) variable, example "10015110", and you want to change all 0 to 2, then try below:
Session["PageGlobals"]) = Session["PageGlobals"]).ToString().Replace("0","2");
amillyard
ASKER
what I am looking to achieve is change a specific value within the string length...
i.e. [1], or [4] or [7] etc..
amillyard
ASKER
Session["PageGlobals"][1] = "1";
or Session["PageGlobals"][1] = '1';
Cannot apply indexing with [] to an expression of type 'object'
Check below example:
string str = "0112013434";
str = str.Substring(0, 4).Replace("1", "5") + str.Substring(5, str.Length);
Response.Write(str);
I have not tested above - check, and let me know.
amillyard
ASKER
need example in context of a session variable - has this been converted correctly below?
if so, -- I am getting the following error message:
'object' does not contain a definition for 'Substring' and no extension method 'Substring' accepting a first argument of type 'object' could be found (are you missing a using directive or an assembly reference?
You required to replace the numbers within the variable, right? I think this was your requirement.
0,4 means It will search in first 5 characters.
"1","5" means it will replace 1 with 5 within the first 5 characters, as mentioned above.
and then we concatenate the rest of the string, which we wanted unchanged.
amillyard
ASKER
but there could be multiples of '1' for example... -- only need the specific character to change, which is determined by its position (as each position represents a differnet page flag I keeping track off -- button/drop-down selection states etc)
1101000110100010101 for example -- each one of these '0' '1' are to be treated as a seperate variable -- basically, I did not want to have to setup x20 session variables -- instead 1 session variable which includes all the other '1's and '0' flag states required....to save memory etc.
searching a string is not what is required as such -- as I know the specific location of each char that needs to change -- hence why want to be able to reference [1], [5] etc.
(do not want to change multile characters at the same time -- just 1 char value at a time -- i.e. not all '1's to be replaced by another value -- only change value at [x] position within string.)
avnish_tanna
If position=0 then
Session["PageGlobals"] = "1" + Session["PageGlobals"].ToString().Substring(1, Session["PageGlobals"].ToString().Length);
else if position > 0 and <length-2 then
Session["PageGlobals"] = Session["PageGlobals"].ToString().Substring(0, position) + "1" + Session["PageGlobals"].ToString().Substring(position+1, Session["PageGlobals"].ToString().Length);
else if position=Length-1 then
Session["PageGlobals"] = Session["PageGlobals"].ToString().Substring(0, Session["PageGlobals"].ToString().Length-1) + "1"
NileshVJoshi
Hi amillyard,
I found a simple solution to your problem:
First assume that, your session PageGlobals has a value "0128456". And you need to replace the 3rd index value '8' to '7' so that your PageGloabls session will have a value = "0127456"
//get a session value in string variable:-
string s = Session["PageGlobals"].ToString();
//Create a string builder object
StringBuilder sb = new StringBuilder(s);
//replace the value '8' located at position 3 to "7"
sb = sb.Replace(s.Substring(3 ,1), "7", 1, 1);
//Add modified result to Session:
Session.Add("PageGlobals", sb.ToString());
Hope this will help you.
-Nilesh
((string)Session["PageGlob
and you can read them with
char c = ((string)Session["PageGlob