0

I have an array of strings, most of which are string values "true" or "false", there are other strings there but i don't wish to alter them.

So i wish to loop through the strings and where i encounter the word "true" or "false" i want to change the string to "1" or "0"

These strings of 1, 0 and others will end up being used as parameters passed to a SQL stored procedure IE: seCmd.Parameters["@CheckListRef"].Value = data[0];

I am merely struggling with the syntax and therefore seeking guidence

string[] data = args.Trim().Split(',');

// Loop over strings
foreach (string s in data)
{
    if(s == "true")
    {
        convert the string True to the string 1
    }
    else if(s == "false")
    {
      convert the string True to the string 0
    }
}

please can someone offer some guidance

1
  • it may be worth providing an example of the args you are passing as well as what you eventually hope to do with it Commented Apr 16, 2013 at 12:03

8 Answers 8

2

What your code is missing is a reference back to the array location you are checking.

Using a for loop would let you keep that reference:

string[] data = args.Trim().Split(',');

// Loop over strings
for(int i = 0; i < data.Length; i++)
{
    if(data[i] == "true")
    {
        data[i] = "1";
    }
    else if(data[i] == "false")
    {
        data[i] = "0";
    }
}

Note - the above is case sensitive, so you may wan to replace the string tests with data[i].Equals("true", StringComparison.InvariantCultureIgnoreCase).

Sign up to request clarification or add additional context in comments.

Comments

1

This should help:

   string[] data = args.Trim().Split(',');

    for(int i = 0; i < data.Length; i++)
    {
        if(data[i] == "true")
        {
            data[i] = "1";
        }
        else if(data[i] == "false")
        {
            data[i] = "0";
        }
    }

Also, you could shorten your code a bit by doing the following:

   string[] data = args.Trim().Split(',');

    for(int i = 0; i < data.Length; i++)
    {
        data[i].Replace("true", "0").Replace("false", "1");
    }

Comments

1

Here is LINQ version which will return you a new array:

string[] newData = data.Select(r => (r == "true" ? "1" : r == "false" ? "0" : r))
                        .ToArray();

Or if you want to ignore case then:

newData = data.Select(r => 
           (r.Equals("true", StringComparison.CurrentCultureIgnoreCase) ? "1" 
           :r.Equals("false", StringComparison.CurrentCultureIgnoreCase) ? "0" 
           : r))
           .ToArray();

Comments

0

I would use a for loop for this but, in your case would this not work

if(s == "true")
{
    s="1";
}
else if(s == "false")
{
  s="0";
}

Comments

0
var data = args.Trim()
               .Split(',')
               .ToList()
               .ConvertAll(s => s.Replace("true", "1").Replace("false", "0"));

Comments

0

Go for a LINQ expression:

var parsedData = data.Trim()
.Where(d => d == "true" || d == "false")
.Select(d => bool.Parse(d) ? "1" : "0")
.ToList();

Comments

0
string originalString = "true, True, FALSE,  something true,false,something else,false";
string resultString = Regex.Replace(
                originalString, @"((?<=(,\s*))|^)true((?=(\s*,))|$)", "1",RegexOptions.IgnoreCase);
resultString = Regex.Replace(
                resultString, @"((?<=(,\s*))|^)false((?=(\s*,))|$)", "0", RegexOptions.IgnoreCase);
string[] resultArray = Regex.Split(resultString, @"\s*,\s*");

resultArray:

 1
 1
 0
 something true
 0
 something else
 0

Comments

0

You do not have to convert it to 1 or 0. Simply do Convert.ToBoolean() if your datatype is bit then try this:

bool bln;
if(bool.TryParse(data[0], out bln))
 {
   seCmd.Parameters.Add("@CheckListRef",SqlDbType.Bit).Value =bln;
 }

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.