Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

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:

"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."

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
537 views
Welcome To Ask or Share your Answers For Others

1 Answer

Use replace() of String

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);

    }
}

see java docs for String

http://docs.oracle.com/javase/6/docs/api/java/lang/String.html

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

Another example,

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);
    }

Output:

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.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...