Avatar of amillyard
amillyard
Flag for United Kingdom of Great Britain and Northern Ireland asked on

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

Avatar of undefined
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)
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
avnish_tanna

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'
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
avnish_tanna

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?

Session["PageGlobals"] = (string)Session["PageGlobals"].Substring(0, 4).Replace("1", "5") + (string)Session["PageGlobals"].Substring(5, (string)Session["PageGlobals"].Length);

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?
avnish_tanna

Session["PageGlobals"] = Session["PageGlobals"].ToString().Substring(0, 4).Replace("1", "5") + Session["PageGlobals"].ToString().Substring(5, Session["PageGlobals"].ToString().Length);
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
amillyard

ASKER
avnish_tanna:  could you explain a little what this routine does exactly?

it appears to replace a character(s) with another.

what do these numbers represent?

(0, 4)
("1", "5")
5, Session["PageGlobals"].ToString().Length);

avnish_tanna

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.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
amillyard

ASKER
(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
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
amillyard

ASKER
NileshVJoshi:  ok, many thanks -- then the other way -- how do I read 'x' position within the session string?
ASKER CERTIFIED SOLUTION
avnish_tanna

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.