Welcome to Keen Software House Forums! Log in or Sign up to interact with the KSH community.
  1. You are currently browsing our forum as a guest. Create your own forum account to access all forum functionality.

A few questions: String methods. Replace. Block Groups.

Discussion in 'Programming Questions and Suggestions' started by Kuu Lightwing, Jan 3, 2015.

Thread Status:
This last post in this thread was made more than 31 days old.
  1. Kuu Lightwing Senior Engineer

    Messages:
    1,503
    I decided to toy with programming a bit and check it out by writing a simple script that allows me to rename the components of a drone.

    Code:
    void Main()
    {
    List<IMyTerminalBlock> antennas = new List<IMyTerminalBlock>();
    GridTerminalSystem.GetBlocksOfType<IMyRadioAntenna>(antennas);
    string droneName = antennas[0].CustomName;
    
    List<IMyTerminalBlock> terminalBlocks  = new List<IMyTerminalBlock>();  
    GridTerminalSystem.SearchBlocksOfName("RC", terminalBlocks);
    
    for (int i = 0; i < terminalBlocks.Count; ++i) {
        string name = terminalBlocks[i].CustomName;
        if (terminalBlocks[i] != antennas[0])
            terminalBlocks[i].SetCustomName (name.Replace ("RC", droneName));
        }
    }
    
    It checks the name of the first antenna found at the grid and replaces an "RC" substring of each other block with the name of the antenna.

    1) How to replace not just "RC" substring, but "RC??" substring (RC + 2 more characters)?
    2) How to do the same thing with the block groups.
     
  2. Cuber Apprentice Engineer

    Messages:
    262
    1) Use Regex and Regex.Replace.
    This one will match "RC" and 0-2 characters a-z, A-Z, 0-9: [FONT= &#39]"RC\w{0,2}"[/FONT] - test it here.
    If you want it to match any two characters (except for newlines), use this: [FONT= &#39]"RC.{0,2}"[/FONT] - test it here.
    And finally, if you want to match "RC" and 0-2 of any numerical digits, this one will do the trick: [FONT= &#39]"RC\d{0,2}"[/FONT] - test it here
    Code:
    System.Text.RegularExpressions.Regex rcNameRegex = new System.Text.RegularExpressions.Regex(@"RC\w{0,2}");
    void Main()
    {
        string testString = "RC12 Online";
        string modifiedString = rcNameRegex.Replace(testString, "toastbox");
        // modifiedString = "toastbox Online"
    }
    
    2) Do you mean you want to rename block groups? Or do you mean that instead of going through the list provided by [FONT= &#39]"GridTerminalSystem.SearchBlocksOfName("RC", terminalBlocks);"[/FONT], you just want to iterate through the block group named whatever?
     
    Last edited by a moderator: Jan 3, 2015
  3. Kuu Lightwing Senior Engineer

    Messages:
    1,503
    1) Aha, Regex. Thank you
    2) Yeah, renaming groups. Iterating through a block group would be a good thing to know, too.
    I actually have a feeling that it is not possible to rename a group - only deleting the old one and making a new one.
     
Thread Status:
This last post in this thread was made more than 31 days old.