locked
Explode or Split Expression RRS feed

  • Question

  • User1771308999 posted

    I have a string that goes something like this: 1,3,4,5

    I'd like to explode/split them and identify each. Is there such option within Razor? I am unable to find it..

    To further advance what I'm doing -- I have a checklist, and each checklist is correspond with #. The first check item is 1, second check item is 2, etc.

    So I use Javascript find who which checklist item is checked and then stored into database. Such as 1,3,4,5.

    Then after pulling from database, I want to be able to know which checklist is checked, but need to take apart 1,3,4,5. Thus, ensure check item 1 is checked, check item 3 is checked, and so on.

    Any assistance would be greatly appreciated! Many thanks in advance!!

    Wednesday, July 16, 2014 8:47 AM

Answers

  • User281315223 posted

    If you have a string in this format, generally the String.Split() method is the easiest way to handle this as seen below :

    // Your string
    string s = "1,3,4,5";
    
    // Split it into an array
    string[] sa = s.Split(','); // yields ["1","3","4","5"]
    

    If you are trying to handle this through Javascript however, one suggestion might be to create an array and iterate through each of your checkboxes and add those to the list. This could be very easily accomplished with either Javascript or jQuery and there is always the Javascript string.split() function which functions identically to the C# version seen above.

    Do you have any examples of the markup or code that you are using? It may make it a bit easier to provide you with a more detailed response.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, July 16, 2014 9:37 AM

All replies

  • User938738290 posted
    string lString = "1,2,3,4,5";
    List<string> lExploded = lString.Split(',');

    Wednesday, July 16, 2014 8:52 AM
  • User281315223 posted

    If you have a string in this format, generally the String.Split() method is the easiest way to handle this as seen below :

    // Your string
    string s = "1,3,4,5";
    
    // Split it into an array
    string[] sa = s.Split(','); // yields ["1","3","4","5"]
    

    If you are trying to handle this through Javascript however, one suggestion might be to create an array and iterate through each of your checkboxes and add those to the list. This could be very easily accomplished with either Javascript or jQuery and there is always the Javascript string.split() function which functions identically to the C# version seen above.

    Do you have any examples of the markup or code that you are using? It may make it a bit easier to provide you with a more detailed response.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, July 16, 2014 9:37 AM
  • User1771308999 posted

    Great! That helps!

    My next challenge is, trying to list all the checklist item #1-5, but noticed that #2 is not part of the string. Meaning it was not checked.

    So I want to create a markup of checklist item showing #1, #3, #4, #5 checked but not #2.

    Each time I try using loop, I ends up repeating the checklist item. Does that make sense??

    Wednesday, July 16, 2014 10:51 AM