我想在一个故事来代替一个字 [英] I want to replace a word in a story

查看:330
本文介绍了我想在一个故事来代替一个字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个应用程序,这将让我在文本输入框输入名字和一个故事用这些名字。

I want to create an app which will let me input names in text inputs and use those names in a story.

我会命名为 strGuide strHost1 三个输入和 strHost2 。在名称填写后,你会在的onClick 事件去一个新的页面,像这样的例子一个小故事:

I would have three inputs named as strGuide, strHost1 and strHost2. After filling in the names, you would a onClickevent to go to a new page with a short story like this example:

你好,我的 strGuide ,我将在今天由陪同的 srtHost1 strHost2 。当我们通过今天的豪宅走路,感觉免费问 strHost1 strHost2 任何你可能有问题。

"Hello, I am strGuide and I will be accompanied today by srtHost1 and strHost2. As we walk through the mansion today, feel free to ask strHost1 or strHost2 any questions you may have."

我要的 strGuide strHost1 strHost2 可替代的名称相同名称的故事放置。这是一个简短的例子。在现实中,这个故事将是整个游览的脚本。也有三次巡演,所以我想从story1,story2或story3来接。我已经寻找一个答案,但我无法找到我所期待的。

I want the strGuide, strHost1 and strHost2 names which can replace the same names places in the story. This is a short example. In reality, the story would be a script for the entire tour. There are also three tours, so I'd like to pick from story1, story2 or story3. I have searched for an answer, but I can't find what I am looking for.

推荐答案

使用替换() 字符串

例子来演示使用与string.replace(),而这并不能掩盖你的整个故事!

example to demonstrate using String.replace(), and this does not cover your entire Story!

public class Test {

    public static void main(String[] args) {
        String story = "Hello, I am strHost and I will be accompanied today by strHost1 and strHost2. As we walk through the mansion today, feel free to ask strHost1 or strHost2 any questions you may have.";
        String str = story.replaceAll("strHost1", "stringhost1");
        str = str.replaceAll("strHost2", "stringhost2");
        str = str.replaceAll("strHost", "stringhost");
        System.out.println(str);

    }
}

看Java文档的字符串

http://docs.oracle.com/ JavaSE的/ 6 /文档/ API / JAVA / LANG / String.html

更新:
Java文档:

Update: java docs:

replace(char oldChar, char newChar) 
          Returns a new string resulting from replacing all occurrences of oldChar in this string with newChar.

replace(CharSequence target, CharSequence replacement) 
          Replaces each substring of this string that matches the literal target sequence with the specified literal replacement sequence

replaceAll(String regex, String replacement) 
          Replaces each substring of this string that matches the given regular expression with the given replacement

另外一个例子,

public static void main(String[] args) {
        String story = "Hello, I am strHost and I will be accompanied today by someguy1 and anotherguy2. As we walk through the mansion today, feel free to ask someguy1 or anotherguy2 any questions you may have.";
        String str = story.replaceAll("someguy1", "someguy1peter");
        str = str.replaceAll("anotherguy2", "anotherguy2john");
        str = str.replaceAll("strHost", "stringhost");
        System.out.println(str);
    }

输出:

Hello, I am stringhost and I will be accompanied today by someguy1peter and anotherguy2john. As we walk through the mansion today, feel free to ask someguy1peter or anotherguy2john any questions you may have.

这篇关于我想在一个故事来代替一个字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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