Dynamically generate collapsible content from a directory with subdirectories containing images.
The problem:
The generated href from the directory names occasionally contain spaces. The same directory name var is used to generate IDs for the matching <div>. the ID name cannot contain spaces. I would like to replace the spaces with a usable character to create a
valid ID name.
The question:
Where and how would I code to replace the spaces with underscores in @dir.Name, without renaming the original directory? I believe the answer lies in the use of
@Regex.Replace but do not know the how/where part.
Can anyone assist me or point me to a good reference for this situation? I'm fairly new to programming.
Member
4 Points
10 Posts
Regex to replace spaces with underscores
May 17, 2013 06:08 PM|John Merchant|LINK
The intention:
Dynamically generate collapsible content from a directory with subdirectories containing images.
The problem:
The generated href from the directory names occasionally contain spaces. The same directory name var is used to generate IDs for the matching <div>. the ID name cannot contain spaces. I would like to replace the spaces with a usable character to create a valid ID name.
The question:
Where and how would I code to replace the spaces with underscores in @dir.Name, without renaming the original directory? I believe the answer lies in the use of
@Regex.Replace
but do not know the how/where part.Can anyone assist me or point me to a good reference for this situation? I'm fairly new to programming.
Thank you.
The code:
@{
}
Regex.Replace
Star
13450 Points
2764 Posts
Re: Regex to replace spaces with underscores
May 17, 2013 06:37 PM|Nasser Malik|LINK
You can use simple replace .. No need of Regex
Regards.
Regex.Replace
Skype: maleknasser1
LinkedIn: https://www.linkedin.com/in/maliknasser
Member
4 Points
10 Posts
Re: Regex to replace spaces with underscores
May 17, 2013 06:51 PM|John Merchant|LINK
The reply:
Incredibly elegant solution. Thank you very much.
Regex.Replace