2014-04-13 2 views
-2

그래서 여기까지입니다. 나는 3 개의 변수를 인수 (a, b, c)로 받아들이고 어떤 식으로 문자열 c를 문자열 a에있는 문자열 b와 바꿔주는 메소드를 생성해야한다.방법을 바꾸지 않고 다른 문자열로 문자열 내에서 문자열을 교환하는 방법

이 방법을 바꾸는 방법없이이 작업을 수행해야하므로이 방법을 만드는 것이 중요합니다.

그래서 예는 것

"Hello my friend, how are you?", "h", "y" 
Result: "Yello my friend, yow are you?" 

또는

"Computer programming is great!", "great", "awesome" 
Result: "Computer programming is awesome!" 

우리는 charAt()는, string.substring, string.length()string.IndexOf()는이 문제를 해결하기 위해 제공됩니다. 어떤 아이디어?

+1

가 나는 또한 답을 알고 싶어 언급했다. 네가 하나를 얻을 때 알려줘! –

+1

무언가를 시험해 보시고 그 4 문장의 결과로 뭔가를 시도해보십시오. – Matthew

+0

[예 아이디어 있음] (http://docs.oracle.com/javase/7/docs/api/java/lang/String.html) – CodeCamper

답변

0

regex으로 시도하십시오.

StringBuffer myStringBuffer = new StringBuffer(); 

    // find all h or H 
    Pattern myPattern = Pattern.compile("h|H"); 
    Matcher myMatcher = myPattern.matcher("Hello my friend, how are you?"); 
    while (myMatcher.find()) { 
     // replace all the occurrence with the actual replacement 
     if (myMatcher.group().equals("h")) { 
      myMatcher.appendReplacement(myStringBuffer, "y"); 
     } else { 
      myMatcher.appendReplacement(myStringBuffer, "Y"); 
     } 
    } 
    myMatcher.appendTail(myStringBuffer); 
    System.out.println(myStringBuffer); 

출력 :

Yello my friend, yow are you? 
0

나는 정확한 코드를 작성하는 것은이 명확 학교 과제이다 우측 때문에 아니라고 생각합니다. 이것은 매우 쉬운 작업이며 documentation을 읽은 후에 그것을 구현하는 방법을 알고 있어야합니다. 이 모든이 방법을 사용하여 수행 할 수 없습니다 heres는 약간의 힌트

You have to chceck wheter string b is substring of a if yes split a into two strings without b and put c between them. 

경우 당신은

관련 문제