-2

I need to replace a string with a uppercase of the same word.So far i can search for sub-string from a text file which contains my string and replace a word like "apple" or "Apple" or "ApPle" to "APPLE". The problem exist when the string is "Is that pineapple Apple adamsApple applepie smell?", my search keyword exist between the other word so it cannot be found and converted.

MY final working code:

 //Get the txt file from the directory and search for word
  func  fileRead(getPath  string,  searchkey  string){
    var  count  int
    buf1,_:=ioutil.ReadFile(getPath);
    var  buf2=  string(buf1)
    var  b  string
    tokens:=strings.Fields(buf2)
    re := regexp.MustCompile("(?i)"+searchkey)//make searchkey case insensitive
     for  i:=0;i<len(tokens);i++{

      if  (strings.EqualFold(tokens[i],searchkey)){

      tokens[i] = re.ReplaceAllString(tokens[i], strings.ToUpper(searchkey))
      count++

     }else{

      tokens[i]=re.ReplaceAllLiteralString(tokens[i],strings.ToUpper(searchkey))
      count++
         }
        }
        for  j:=0;  j<len(tokens);  j++{
         b+=  tokens[j]
         b+="  "  
          }
        c  :=  []byte(b)
        fmt.Println(count,"                  ",getPath)
        ioutil.WriteFile(getPath,  c,  0644)//rights back to the file
   }
Chetandalal
  • 674
  • 1
  • 7
  • 18
  • 3
    Please use gofmt or use an editor or ide that does it for you :( – OneOfOne Jul 17 '14 at 17:34
  • 1
    I took the liberty to format the code in your question for you. Please try to submit well-indented and readable code next time; poorly indented code is a pain for everybody else to read and significantly worses the propability to receive an answer. – fuz Jul 17 '14 at 17:39
  • use regex `\bapple\b` – chendesheng Jul 17 '14 at 17:40

2 Answers2

3

You should use regexp for that, a simple example:

func KeywordsToUpper(src string, keywords ...string) string {
    var re = regexp.MustCompile(`\b(` + strings.Join(keywords, "|") + `)\b`)
    return re.ReplaceAllStringFunc(src, func(w string) string {
        return strings.ToUpper(w)
    })
}

//edit

I think I misunderstood the question, if all you want is to replace a word even if it's in the middle of other words, like myapplecomputer then using strings.Replace should be enough, for example:

func ReplaceLikeYouDontCare(src string, words ...string) string {
    for _, w := range words {
        src = strings.Replace(src, w, strings.ToUpper(w), -1)
    }
    return src
}
OneOfOne
  • 95,033
  • 20
  • 184
  • 185
  • Thanks, I need to use both regex and replace. The solution requires to have searchkey to be in any case and the string to be edited can also have any case. eg. searchkey:"Apple" string:"BigApples"or"ApPlEs" etc. – Chetandalal Jul 17 '14 at 18:38
  • `regexp.MustCompile(`\b(?i:` + strings.Join(keywords, "|") + `)\b`)` makes it case insensitive. Notice the `?i:` part. – OneOfOne Jul 17 '14 at 18:51
  • sorry, I am getting this error: cannot use keywords (type string) as type []string in argument to strings.Join. I am trying use s=[]string(keyword) but it is unable to change. – Chetandalal Jul 17 '14 at 19:19
  • in my example it can support multiple keywords, if you just want one simply replace `strings.Join(keywords, "|")` with `keywords`. – OneOfOne Jul 17 '14 at 19:37
1

Isn't strings.Replace exactly what you're looking for?

func fileRead(getPath string, searchkey string) {
    buf, _ := ioutil.ReadFile(getPath)
    strings.Replace(string(buf), searchkey, strings.ToUpper(searchkey), -1)
}
Amit Kumar Gupta
  • 17,184
  • 7
  • 46
  • 64