删除字符串中的字符,直到/ r / n

时间:2013-06-13 08:51:11

标签: android string

我有很长的字符串,在某些部分有

一些文字+“PHOTO; ENCODING = BASE64; TYPE = JPEG:”+一些随机生成的字符+ / r / n ......

我想知道如何从

中删除部分

“PHOTO; ENCODING = BASE64; TYPE = JPEG:”untill / r / n

所以我只会留下

一些文字+ / r / n?

到目前为止我的代码:

if (string.contains("PHOTO;ENCODING=BASE64;TYPE=JPEG:") {
   string = string.replace("PHOTO;ENCODING=BASE64;TYPE=JPEG:", "");
}

但这显然不会取代我随机生成的字符,只有“PHOTO; ENCODING = BASE64; TYPE = JPEG:”。

如何从“PHOTO; ENCODING = BASE64; TYPE = JPEG:”中直接“循环”字符串,直到/ r / n?

2 个答案:

答案 0 :(得分:1)

为什么不试试

1) Get the index of "PHOTO;ENCODING=BASE64;TYPE=JPEG:". and call it idx
2)If idx != -1 then take substring of original string using str.subString(0,idx) and call it newStr
3)return newStr+(str.endsWith("\r\n")?"\r\n":"")

答案 1 :(得分:1)

final String input = "some text + PHOTO;ENCODING=BASE64;TYPE=JPEG: + some characters that generate randomly + /r/n"
final int index = input.indexOf("PHOTO;ENCODING=BASE64;TYPE=JPEG:");
if (index != -1)
{ 
    final String result = input.subString(0, index) + System.getProperty("line.separator")
}