Eliminate a hyphen/underscore for a subscript

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rshin2020
    New Member
    • Dec 2009
    • 26

    Eliminate a hyphen/underscore for a subscript

    Hi. I need to eliminate the underscore for X_2 where the 2 is a subscript. What is the code like? I have a sample here but can't figure out which is what and how to change it, as I did try to change but no changes took place at all. Please help. Thanks in advance.

    Code:
    string Anthem = "Star Spangled Banner";
    Anthem = Anthem.Replace(" ", "");
    p/s:I have a column called Symbol and there are values in it such as X_1, X_2.
  • tlhintoq
    Recognized Expert Specialist
    • Mar 2008
    • 3532

    #2
    Code:
    string myValue = "X_2";
    string newValue = myValue.Replace("_","");
    The MSDN for string.replace explains this in detail.

    Comment

    • rshin2020
      New Member
      • Dec 2009
      • 26

      #3
      I am still getting an error. I have replaced "X_2" with "B3,B4" as i have in the excel column. It says identifer expected at the "B4". Do I need to have a method written for it?
      Here's what I have done:
      Code:
      string myValue = ("B3","B4");
      string newValue = myValue.Replace("_", "");

      Comment

      • tlhintoq
        Recognized Expert Specialist
        • Mar 2008
        • 3532

        #4
        Well... yeah... You are trying to assign two different values to one string.
        string myValue = ("B3","B4");
        You can't assign both "B3" and "B4" to a single string.

        Also, neither of these string contain an underscore - so how are you expecting to replace an underscore that doesn't exist?

        Comment

        • rshin2020
          New Member
          • Dec 2009
          • 26

          #5
          So how else should I put it? I need to get the rows first right? The rows are B3 and B4, they already have values of X_2 and X_3 respectively. I need to eliminate the underscore. Please help. Thanks.

          Comment

          • tlhintoq
            Recognized Expert Specialist
            • Mar 2008
            • 3532

            #6
            What rows? What are you talking about? There is no "rows" property of a string object or inside the .Replace() method.

            Comment

            • rshin2020
              New Member
              • Dec 2009
              • 26

              #7
              In these rows, there are values as I mentioned earlier, the X_2 and X_3. How do I assign the code to eliminate the underscores? That's all. The code you gave me didn't work. Because, why did you put 'string myValue = ("B3","B4"); '? The underscores contains in the rows of B3 and B4.

              Comment

              • tlhintoq
                Recognized Expert Specialist
                • Mar 2008
                • 3532

                #8
                OH..... You are working on an Excel spreadsheet. It helps if you make that clear. How would I know you are working on an Excel spreadsheet from the title:
                Eliminate a hyphen/underscore for a subscript
                Or anywhere in the original question
                Hi. I need to eliminate the underscore for X_2 where the 2 is a subscript. What is the code like? I have a sample here but can't figure out which is what and how to change it, as I did try to change but no changes took place at all. Please help. Thanks in advance.

                Code:
                string Anthem = "Star Spangled Banner";
                Anthem = Anthem.Replace(" ", "");

                Comment

                • rshin2020
                  New Member
                  • Dec 2009
                  • 26

                  #9
                  I'm sorry. I should have made that clear. My mistake. So any solution?

                  Comment

                  • tlhintoq
                    Recognized Expert Specialist
                    • Mar 2008
                    • 3532

                    #10
                    Are you able to read the Excel spreadsheet programmaticall y? Are you able to obtain a value from a given cell using the Microsoft.Offic e.Excel.Interop framework?

                    I might suggest you work on just being able to read a spreadsheet before trying to figure out how to change one.

                    Comment

                    • rshin2020
                      New Member
                      • Dec 2009
                      • 26

                      #11
                      Yes, I am able to read the spreadsheet. All values can be displayed. Right now, i just need a way to replace the _ with a space. From X_2 to X2. Thats all. Any idea?. Thanks in advance.
                      Here's the full coding:

                      Code:
                      using System;
                      using System.Collections.Generic;
                      using System.Text;
                      using Excel = Microsoft.Office.Interop.Excel;
                      
                      namespace UncFunc
                      {
                          class Report
                          {
                      
                              public void Create(Quantity q)
                              {
                                  Excel.Application objApp;
                                  Excel._Workbook objBook;
                                  Excel.Workbooks objBooks;
                                  Excel.Sheets objSheets;
                                  Excel._Worksheet objSheet;
                                  Excel.Range range;
                                  Dictionary<int, Quantity> rootQty = q.RootQuantities;
                                           
                                  try
                                  {
                                      // Instantiate Excel and start a new workbook.
                                      objApp = new Excel.Application();
                                      objBooks = objApp.Workbooks;
                                      objBook = objBooks.Add(Type.Missing);
                                      objSheets = objBook.Worksheets;
                                      objSheet = (Excel._Worksheet)objSheets.get_Item(1);
                      
                                      //Get the range where the starting cell has the address
                                      //m_sStartingCell and its dimensions are m_iNumRows x m_iNumCols.
                                      range = objSheet.get_Range("A1", Type.Missing);
                                      range = range.get_Resize(6,9);
                      
                                      //Create an array.
                                      string[,] saRet = new string[rootQty.Count+2, 9];
                      
                      
                                      saRet[0, 4] = "Root Quantities";
                                      saRet[1, 0] = "String";
                                      saRet[1, 1] = "Symbol";
                                      saRet[1, 2] = "Limit Description";
                                      saRet[1, 3] = "Value";
                                      saRet[1, 4] = "Limit";
                                      saRet[1, 5] = "DF";
                                      saRet[1, 6] = "Divisor";
                                      saRet[1, 7] = "Std Unc";
                                      saRet[1, 8] = "Unit";
                      
                      
                                      int[] aryKeys= new int[rootQty.Keys.Count];
                                      Dictionary<int,Quantity>.KeyCollection oKeys = rootQty.Keys;
                                      oKeys.CopyTo(aryKeys, 0);
                      
                                      //Fill the array.
                                      for (long iRow = 0; iRow < rootQty.Count; iRow++)
                                      {
                                         
                                          //for (long iCol = 0; iCol < 4; iCol++)
                                          //{
                                              int iKey = aryKeys[iRow];
                      
                                              Quantity qty = rootQty[iKey];
                      
                                              String sSymbol = qty.Symbol;//column B
                                              String sLimitDescription = qty.LimitDescription;// column C
                                              String sValue = qty.Value.ToString();//column D
                                              String sLimit = qty.Limit.ToString();//column E
                                              String sDF = qty.df.ToString();//column F
                                              String sDivisor = qty.Divisor.ToString();//column G
                                              String sStdUnc = qty.StdUnc.ToString();//column H
                                              String sUnit = qty.Unit;
                      
                                              saRet[iRow+2, 1] = sSymbol;
                                              saRet[iRow + 2, 2] = sLimitDescription;
                                              saRet[iRow + 2, 3] = sValue.ToString();
                                              saRet[iRow + 2, 4] = sLimit.ToString();
                                              saRet[iRow + 2, 5] = sDF.ToString();
                                              saRet[iRow + 2, 6] = sDivisor.ToString();
                                              saRet[iRow + 2, 7] = sStdUnc.ToString();
                                              saRet[iRow + 2, 8] = sUnit;
                      
                                      
                                          //Previous statements                
                                              //saRet[iRow+2,0] = sSymbol;
                                              //saRet[iRow+2,1] = sValue;
                                              //saRet[iRow+2,2] = sStdUnc;
                                              //saRet[iRow+2,3] = qty.Unit; //sUnit
                                              
                                              //Put the row and column address in the cell.
                                              //saRet[iRow, iCol] = iRow.ToString() + "|" + iCol.ToString();
                      
                                              
                                        // }
                                      }
                      
                                      
                                      //Set the range value to the array.
                                      range.set_Value(Type.Missing, saRet);
                      
                      
                                      //Set Subscript
                                      range = range.get_Resize(6, 9);
                                      objSheet.get_Range("A1", Type.Missing);
                                      //range = range.get_Resize(0, 2);
                                      //range.get_Range(2, 0);
                      
                                      objSheet.get_Range("A1", "A6").EntireColumn.AutoFit();
                                      objSheet.get_Range("B3", "B6").get_Characters(2, 2).Font.Subscript = true;  //column and character chosen respectively
                      
                                      //objSheet.get_Range("B4","B6").Font.Subscript = true;
                                      //objSheet.get_Range(1, 1).Font.Subscript = true;
                      
                                                     
                                      //Set Bold(Added)
                                      objSheet.get_Range("A1", "I1").Font.Bold = true;
                                      objSheet.get_Range("A2", "I2").Font.Bold = true;
                                      //range = range.get_Resize(1, 1);
                                      //range.get_Characters(1,1).Font.Bold = true;
                                      //range.Font.Bold = true;
                      
                      
                                      [B]string myString;
                                      myString = ;
                                      myString = Regex.Replace(myString, "_", "");[/B]
                      //the one above needs the modification
                                      //string myValue = ("X_2");
                                      //string newValue = myValue.Remove(1,1);
                      
                                      
                      
                          
                      
                                      //Return control of Excel to the user.
                                      objApp.Visible = true;
                                      objApp.UserControl = true;
                      
                                      objSheet.get_Range("A1", "I6").BorderAround(Excel.XlLineStyle.xlContinuous, Excel.XlBorderWeight.xlMedium,
                                                  Excel.XlColorIndex.xlColorIndexAutomatic, Excel.XlColorIndex.xlColorIndexAutomatic);// added
                      
                      
                                      //Save
                                      objBook.SaveAs("Report Uncertainty.xls", Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, 
                                          Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlShared,
                                          Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
                                  }
                                  catch (Exception theException)
                                  {
                                      String errorMessage;
                                      errorMessage = "Error: ";
                                      errorMessage = String.Concat(errorMessage, theException.Message);
                                      errorMessage = String.Concat(errorMessage, " Line: ");
                                      errorMessage = String.Concat(errorMessage, theException.Source);
                      
                                      Console.WriteLine(errorMessage, "Error");
                                  }
                              }
                      
                          }
                      }
                      p/s I have also stated the one which needed to be modified, in Bold.

                      Comment

                      • rshin2020
                        New Member
                        • Dec 2009
                        • 26

                        #12
                        I have typed the 2 lines in immediate window, and it seemed to work but it wont display as such in the excel file. Thats what i don't understand. Please help. Thanks.

                        Comment

                        • tlhintoq
                          Recognized Expert Specialist
                          • Mar 2008
                          • 3532

                          #13
                          Right now, i just need a way to replace the _ with a space. From X_2 to X2.
                          There is no space in your example. You have gone from X_2 to X2 (no space)

                          I have shown you how to do the replace.
                          Code:
                          string myValue = "X_2";
                          string newValue = myValue.Replace("_","");
                          The replace method is very simple. The first parameter is the character you want to look for (the one to be replaced), the second parameter is the one to insert (the doing the replacing)
                          In the above example all underscores are replaced with "" (no space)

                          Code:
                          string myValue = "X-2";
                          string newValue = myValue.Replace("-","4");
                          In this example all hyphens are replaced with the number 4

                          Code:
                          string myValue = "X&2";
                          string newValue = myValue.Replace("&","B");
                          In this example all & symbols are replaced with the letter B

                          Comment

                          • tlhintoq
                            Recognized Expert Specialist
                            • Mar 2008
                            • 3532

                            #14
                            p/s I have also stated the one which needed to be modified, in Bold.
                            Code:
                                            string myString;
                                            myString = ;
                                            myString = Regex.Replace(myString, "_", "");
                            //the one above needs the modification
                                            //string myValue = ("X_2");
                                            //string newValue = myValue.Remove(1,1);
                            Here is what you have done.
                            You created a new string named myString.
                            It has no value.
                            Then you try to replace all the underscores with nulls.
                            Guess what... This code works perfectly. It just doesn't produce the results you want. But it does exactly what you told it to do: It went through the string 'myString' and replaced all the underscores with an empty string.

                            Since you know how to get the values from the spreadsheet... Those are the values you need to evaluate. Don't make a new string. Evaluate the values from the cells of your spreadsheet.

                            Comment

                            • rshin2020
                              New Member
                              • Dec 2009
                              • 26

                              #15
                              Thank you so much!! Really appreciate your time explaining it. The problem is that it doesnt display the results in excel but in the immediate window it does. Why is that so?

                              Comment

                              Working...