0

I have a string with url and I want to remove blockquotes and characters after from the string.

String looks like http://pics.v6.top.rbk.ru/v6_top_pics/resized/250xH/media/img/7/92/754435534528927.jpg\"><div>Какой-то div</div>

So i try to make a for-loop which iterates through every character appending the to new string and stops when it finds blockquotes

I try following code

var value = "http://pics.v6.top.rbk.ru/v6_top_pics/resized/250xH/media/img/7/92/754435534528927.jpg\"><div>Какой-то div</div>"
var result = String()
for char in value.characters {
  var i = false
  if char == "\"" {
    let temp: Character = "\""
    result.append(temp)
    i = true
  } else if i != true {
    result.append(char)
  }
}
print(result)

but it does not work

Also I try while, but it causes infinite loop

for char in value.characters {
  while char != "\"" {
    print("hello")
  }
}

How to do this correctly ?

Thanks in advance!

UPD - i dont need to remove only quotes. I need to remove them AND everything after them

RobG
  • 142,382
  • 31
  • 172
  • 209
Alexey K
  • 6,537
  • 18
  • 60
  • 118
  • possible duplicate of [Swift - Remove " character from string](http://stackoverflow.com/questions/25591241/swift-remove-character-from-string) – Eric Aya Sep 30 '15 at 11:32

1 Answers1

1

If you want to remove all characters from the double quote until the end of the string, it is easier to get the index of the substring with rangeOfString()

let value = "http://pics.v6.top.rbk.ru/v6_top_pics/resized/250xH/media/img/7/92/754435534528927.jpg\"><div>Какой-то div</div>"
if let offsetOfFirstDoubleQuote = value.rangeOfString("\"") {
  let result = value.substringToIndex(offsetOfFirstDoubleQuote.startIndex)
  print(result)
}

or, more specific, if the separator is always ">

if let offsetOfDoubleQuoteGreaterThan = value.rangeOfString("\">") {
  let result = value.substringToIndex(offsetOfDoubleQuoteGreaterThan.startIndex)
  print(result)
}
vadian
  • 274,689
  • 30
  • 353
  • 361