1

I have a chunk of code that looks like this: HiClass *myNewClass;

Now, what I'm doing is writing a method to roll through and delete the Hi, as well as everything after it, including the *, so that only myNewClass; is left. Now, I take out the "Hi" like so:

 textToConvert = inputField.stringValue.stringByReplacingOccurrencesOfString("Hi", withString: "", options: NSStringCompareOptions.LiteralSearch, range: nil) 

But within this same method, if possible, I would like to somehow delete everything leaving only myNewClass;, as stated above.

My first though on how to approach this is to use a range. Though still being new to Swift and trying to avoid Objective-C, I'm unsure of how to remove all of the characters between the Hi and the *, leaving onlymyClass.

2 Answers 2

1

you can use this method

var testStr = "HiClass *myNewClass" let array = testStr.componentsSeparatedByString("*") testStr = String(array.last) 
Sign up to request clarification or add additional context in comments.

2 Comments

That didn't work unfortunately, it only gave me the end values of the string.
don't you want to remove all of the characters between the Hi and the * ? there other method can do this: var str = "HiClass myNewClass" let range = str.rangeOfString("") str.removeRange(Range(start: str.startIndex, end: (range?.endIndex)!))
0

You can use indexOf with Swift 2.0 to find the index, and just take a substring to the end of it like this:

var str = "HiClass *myNewClass" if let idx = str.characters.indexOf("*") { var s = str.substringFromIndex(advance(idx, 1)) } 

1 Comment

I don't believe .characters is a property of String, I'm getting an error there

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.