PDA

View Full Version : Replace a character in a string


drmerman
01-25-2007, 04:23 PM
Hey Guys,

As a result of the getAttr command, I have a string containing the following :

C:/Documents and Settings/User/Desktop/image.jpg

I'm creating a batch file that needs to use this path, but obviously all of the /'s need to be \'s. I know about the fromNativePath command, but that works in a different way.

I was wondering if there is any way to search through the string and when Maya finds a /, replace it with a \ ?

If anybody has any other ideas, they'd be much appreciated! :D

Cheers,
Dr Merman

drmerman
01-25-2007, 04:37 PM
:D Haha. I always post about 5 minutes too soon. I used stringToStringArray, and then stringArrayToString. Worked like a charm.

Cheers,
Dr Merman

StephG
01-25-2007, 05:46 PM
You could also have used substitute:

As in:

string $s = "This/That/The/Other/Thing";
$s = `substituteAllString $s "/" "\\"`;


You need to use the \\ instead of just / because \ would escape the " mark as a special character.

Butt....

For the file path specific thing, you might want to use a command specific to it:


$path = `toNativePath $path`;


Of course, that command is a MEL script in the "others" directory, and it uses the lines:


$strNew = $strFile;
$strFile = substitute ("/", $strNew, "\\");

CGTalk Moderation
01-25-2007, 05:46 PM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.