PDA

View Full Version : access a specific char in a string


zachgrachan
04-26-2003, 11:06 PM
is there a way to access say the third letter in a string, or any needed character? in c++ i would just put the string into a char array but MEL doesn't seem to have anything like char.

bigfatMELon
04-26-2003, 11:49 PM
Check out this function available in my stringTools library on highend3d.

string $char = `stringNthChar ($anyString)`;

-jl

zachgrachan
04-27-2003, 01:12 AM
you are the greatest being, ever to wal....uhummm....i mean thanks, dude! thats more than i asked for and exactly what i was looking for.

Luddy
04-27-2003, 05:11 PM
Cool, I was looking for this info as well. :thumbsup:

I'm trying to store the last character in a variable but am unsure
how to do it inside procedures. Any help?


sphere -n mesh07;
select mesh07;

string $curSe[] = `ls -sl`;
print $curSe;

global proc string stringLastChar(string $item)
{
string $numMesh = `match ".$" $item`;
return $numMesh;
//print $numMesh; // 7 ?????????????
}
stringLastChar $curSe[0];


//print $numMesh; // 7 ?????????????

bigfatMELon
04-27-2003, 08:03 PM
That is essentially what I do in my stringLastChar, though I have condensed it down to a single line:

global proc string stringLastChar (string $item){
return `match ".$" $item`;
}

Even though it's just one line, I like having all of my string functions named in such a way that you can instantly tell what they do. It's a lot easier to trouble shoot or follow old code. For instance:

string $sel[] = `ls -sl`, $last;

for ($x in $sel){
$last = `stringLastWord $x "_"`;
if (`stringFirstChar $last` == "x")
doSomething;
}

-jl

Luddy
04-27-2003, 11:15 PM
I got it- instead of...

stringLastChar $curSe[0];

put...

string $lastCharacter = stringLastChar($curSe[0]);
print $lastCharacter;

Also....

//==================================
// $numEnd = 564 when "nameObject_564" selected
//==================================

string $curSe[] = `ls -sl`;

global proc string stringLastWord (string $item, string $wordChar)
{
string $wordList[];

tokenize $item $wordChar $wordList;

return $wordList[(size($wordList) -1)];
}

string $numEnd = stringLastWord ($curSe[0], "_");
print $numEnd;

michaelcomet
04-28-2003, 12:21 PM
FWIW the same can be done using built in Maya MEL commands like: size and substring.

ie: string $char3 = `substring $theString 3 3`;

int $numChars = size($theString);

Sanctuary
04-30-2003, 09:39 PM
i`ll go for substring as well, it`s much faster because it`s hardcoded

bigfatMELon
05-01-2003, 06:15 AM
Since I'm using substring in my functions I doubt that there would be a dicernable difference between using substring directly vs stringNthChar, stringFirstChar or stringLastChar.

But there will definitely be a difference when it comes time to read the code later on. :)

-jl

Sanctuary
05-01-2003, 10:07 PM
Originally posted by bigfatMELon
... I doubt that there would be a dicernable difference between using substring directly vs stringNthChar, stringFirstChar or stringLastChar.

depend on how many calls you make to that function ... if you have to cycle though a array with 100 item or more ... then :)

bigfatMELon
05-02-2003, 06:22 AM
I doubt it. I've built massive scripts using those functions exclusively while doing very complicated string work (see UltraPose) and speed just isn't an issue. I'd say that you'd have to iterate over a few thousand times before you could tell the difference and it would still be small.

-jl

Sanctuary
05-02-2003, 08:36 AM
anyway ... let`s cut the race, zachgrachan got the solution so let`s don`t spend server space with this topic, ok?

:)

CGTalk Moderation
01-15-2006, 12:00 AM
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.