IronPython string methods and depreciated string functions

https://ironpython-test.readthedocs.io/en/latest/library/string.html

I am trying to figure out an alternative way to go about a couple of depreciated python string functions: string.find() and string.replace(). But from reading about string methods in that link, I can’t figure out a way to do that. Any ideas?

What are you trying to achieve? :slight_smile:

They’re not depreciated. They’ve just been moved from the old string module to the built in string class implementation

image

I am trying to do a simple IF statement, where if a word is found it would replace it with another.

Example:

If word “Hello” is found it would be replaced with “Goodbye”
So “Hello World” would become “Goodbye World”

Thanks Dimitar, I thought I tried exactly that but it didn’t work. I didn’t realize you have to format string first (At least according to Visual Codes Intelisense) Now my code does not give me any errors, but still does not work.

inputString = IN[0]
outputString = "{}".format(inputString)
outputString.replace("Hello","Goodbye")
OUT = outputString

OUT is still “Hello World”

@upyoas the following should work

image

import clr

import sys
pyt_path = r'C:\Program Files (x86)\IronPython 2.7\Lib'
sys.path.append(pyt_path)

import string

inputString = IN[0]

newstring=string.replace(inputString, "Hello","Goodbye")

OUT = newstring

or this code:

import clr

import sys
pyt_path = r'C:\Program Files (x86)\IronPython 2.7\Lib'
sys.path.append(pyt_path)

from string import *

inputString = IN[0]

newstring = inputString.replace("Hello","Goodbye")

OUT = newstring
4 Likes

It worked! For some reason the second code did not work for me, and gave no errors. But I can work with the first one for now. Will come back to it later, LOL

Here is the syntax for find too in case some one looks for it.

# Enable Python support and load DesignScript library
import clr
clr.AddReference('ProtoGeometry')
from Autodesk.DesignScript.Geometry import *

clr.AddReference("RevitAPIUI")
from Autodesk.Revit.UI import TaskDialog

# The inputs to this node will be stored as a list in the IN variables.
dataEnteringNode = IN

# Place your code below this line

import sys
pyt_path = r'C:\\Program Files (x86)\\IronPython 2.7\\Lib'
sys.path.append(pyt_path)

import string

inputString = IN[0]

result = string.find( inputString, "World") # -1 = Not Found; # = Location of Character before 'H'
newString = string.replace(inputString, "Hello","Goodbye")

outputString = "Original: {}\nReplace() Result: {}\nFind() Result: {}".format(inputString,newString,result)

# Assign your output to the OUT variable.
OUT = outputString

Capture

1 Like

@upyoas Please mark the post as solved.

1 Like