ToUpper() method not working

前端 未结 6 856
星月不相逢
星月不相逢 2021-01-20 09:52

I am passing a message from a server that gets stored into a string variable called strObject. I wish to convert the string inside strObject to upper case. So, I use ToUpper

相关标签:
6条回答
  • 2021-01-20 10:00

    strObject.ToUpper() returns a string in upper case

    Use the following ...

    strObject = strObject.ToUpper().Trim();
    
    0 讨论(0)
  • 2021-01-20 10:02

    Strings are immutable, you need to remember that when you work with them.

    Your method inserts values to your strObject and strValue variables to which you call various worker methods but those worker methods do not change the immutable string variables that have already been created.

    Although the original variables are immutable you can still set new variables (Even with the same name) equal to the original plus the result of the worker method.

    So all you really need to do is to change

    strObject.ToUpper().Trim();

    and

    strValue.ToUpper().Trim();

    to

    strObject = strObject.ToUpper().Trim()

    and

    strValue = strvalue.ToUpper().Trim()

    This answer is pretty much a duplicate of my answer here but I think both are correct.

    0 讨论(0)
  • 2021-01-20 10:06

    string.ToUpper() returns a value, use it.

    0 讨论(0)
  • 2021-01-20 10:14

    Strings are immutable, so calling the functions on the string doesn't actually change the strings. Instead they return a modified copy of the strings.

    This means that you have to store the returned value of the functions, fore example in the original variable.

    0 讨论(0)
  • 2021-01-20 10:15

    A System.String is immutable, so you must re-assign, like:

    strObject = strObject.ToUpper().Trim(); 
    

    All methods that manipulate strings, leave the original string unchanged and returns a new string with the desired content. You must pick up that return value and assign it to something.

    0 讨论(0)
  • 2021-01-20 10:26

    This won't work since strings are immutable, you have to assign the value back to strObject

    strObject = strObject.ToUpper().Trim();
    

    Also there is nothing much done by the switch as shown in your code, you can remove it unless this is not the entire code.

    public void VerifyValue(String strObject, String strValue, int row)
    {               
         //strObject.ToUpper().Trim();
         //strValue.ToUpper().Trim();
         if(strObject.ToUpper() ==  "TASK_STATUS")
         {
              if (m_taskStatus.taskStatus.ToString() == strValue.ToUpper())
              {
                  ExcelRecorder(null, row);
              }
              else
              {
                   ExcelRecorder("The value [" + m_taskStatus.taskStatus.ToString() + "] does not match with the given value.", row);
               }
           }
    }
    
    0 讨论(0)
提交回复
热议问题