Android编辑/插入字符串



我想知道如何在android中以编程方式编辑字符串。我正在从我的设备向我的网站显示字符串,撇号破坏了PHP的输出。所以为了解决这个问题,我需要添加字符分隔符,即:反斜杠"\"。

例如,如果我有这样的字符串:我喜欢菲利伯托的
我需要安卓将其编辑为:我喜欢菲利伯托的!

然而,每个字符串都会有所不同,而且还会有其他字符需要我逃离。我该怎么做?

我想知道如何在android中以编程方式编辑字符串。我正在从我的设备向我的网站显示字符串,撇号破坏了PHP的输出。所以为了解决这个问题,我需要添加字符分隔符,即:反斜杠"\"。

这就是我目前所拥有的,感谢ANJ的基本代码…:

if(title.contains("'")) {
    int i;
    int len = title.length();
    char[] temp = new char[len + 1]; //plus one because gotta add new
    int k = title.indexOf("'"); //location of apostrophe 
    for (i = 0; i < k; i++) { //all the letters before the apostrophe
        temp[i] = title.charAt(i); //assign letters to array based on index
    }
    temp[k] = 'L';  // the L is for testing purposes
    for (i = k+1; i == len; i++) { //all the letters after apostrophe, to end
        temp[i] = title.charAt(i); //finish the original string, same array
    }
    title = temp.toString(); //output array to string (?)
    Log.d("this is", title); //outputs gibberish
}

它输出随机字符。。甚至与我的起始字符串都不相似。有人知道是什么原因造成的吗?例如,字符串"Lol'ok"变为>>"%5BC%4042ed0380"

我假设您将字符串存储在某个地方。假设字符串为:str。您可以使用临时数组来添加"/"。对于单个字符串:

    int len = str.length();
    char [] temp = new char[len+1];   //Temporary Array
    int k = str.indexOf("'"), i;      //Finding index of "'"
    for(i=0; i<k-1; i++) 
    {
     temp[i] = str.charAt(i);        //Copying the string before '
    }
    temp[k] = '/';                   //Placing "/" before '
    for(i=k; j<len; j++)
    {
        temp[i+1] = str.charAt(i);     //Copying rest of the string
    }
    String newstr = temp.toString();    //Converting array to string

可以对多个字符串使用相同的字符串。只需将其作为一个函数,并随时调用即可。

字符串API有许多API调用可以提供帮助,例如String.replaceAll。但是。。。

撇号破坏PHP输出

然后修复PHP代码,而不需要"干净"的输入。最好的选择是选择一种支持良好的传输格式(比如JSON或XML),并让每个端的JSON API处理转义代码。

最新更新