为removeLetter()方法传递字符串参数后,如何删除删除了单个字母的相同字符串? [英] After passing a string parameter for removeLetter() method, how can I return the same string with a single letter removed?

查看:297
本文介绍了为removeLetter()方法传递字符串参数后,如何删除删除了单个字母的相同字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为removeLetter()方法传递字符串,并返回删除了一个字母的字符串。在我最后创建了一个随机字符后,如何从's1'和's2'中传递的字符串中删除单个随机字符?



也许是这样的:

I'm trying to pass strings for removeLetter() method, and return the the string with a single letter removed. After I have created a single random character in the end, how can I remove the single random character from the strings I have passed in 's1' and 's2'?

Maybe something like this:

String newWord = letters.charAt() - letters.charAt(charPos);





完整代码:





Full code:

public class MyProgram
{
    public void start()
    {
        String s1 = removeLetter("MARVELLOUS");
        String s2 = removeLetter("TANGO");

        System.out.println("Letters removed: " + s1 + ", " + s2);
    }

    private String removeLetter(String letters)
    {

        int li = az.length();
        int ranChar = (int)(Math.random()*li);
        char charPos = letters.charAt(ranChar);


    }
}

推荐答案

您可以将字符串转换为 ArrayList of char 然后使用删除( charPos

然后再次使用 ToArray()转换回字符串 / code>。
You could convert the string to an ArrayList of char and then use Remove(charPos)
Then convert back to a string again with ToArray().


您可以将字符串拆分为要删除的字符两侧的两个子字符串。然后连接这两个并返回新创建的字符串。
You could split the string into two substrings either side of the character you want removed. Then concatenate those two and return the newly created string.


这篇关于为removeLetter()方法传递字符串参数后,如何删除删除了单个字母的相同字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆