Selaa lähdekoodia

Merge branch 'master' of http://git.shengws.com/csx/gdyb

csx 3 vuotta sitten
vanhempi
commit
160e09d153
1 muutettua tiedostoa jossa 17 lisäystä ja 1 poistoa
  1. 17 1
      controllers/js/jsyb_controller.go

+ 17 - 1
controllers/js/jsyb_controller.go Näytä tiedosto

11
 	"github.com/astaxie/beego"
11
 	"github.com/astaxie/beego"
12
 	"github.com/axgle/mahonia"
12
 	"github.com/axgle/mahonia"
13
 	"io/ioutil"
13
 	"io/ioutil"
14
+	"regexp"
14
 	"strings"
15
 	"strings"
15
 	"syscall"
16
 	"syscall"
16
 	"unsafe"
17
 	"unsafe"
555
 	if ret2 != 0 {
556
 	if ret2 != 0 {
556
 		return "", ""
557
 		return "", ""
557
 	}
558
 	}
558
-	return strings.Replace(string(pCardInfo), " ", "", -1), strings.Replace(string(pBusiCardInfo), " ", "", -1)
559
+	return DeleteExtraSpace(string(pCardInfo)), DeleteExtraSpace(string(pBusiCardInfo))
559
 }
560
 }
560
 
561
 
561
 func IntPtr(n int) uintptr {
562
 func IntPtr(n int) uintptr {
581
 	result := string(cdata)
582
 	result := string(cdata)
582
 	return result
583
 	return result
583
 }
584
 }
585
+
586
+func DeleteExtraSpace(s string) string {
587
+	//删除字符串中的多余空格,有多个空格时,仅保留一个空格
588
+	s1 := strings.Replace(s, "  ", " ", -1)      //替换tab为空格
589
+	regstr := "\\s{2,}"                          //两个及两个以上空格的正则表达式
590
+	reg, _ := regexp.Compile(regstr)             //编译正则表达式
591
+	s2 := make([]byte, len(s1))                  //定义字符数组切片
592
+	copy(s2, s1)                                 //将字符串复制到切片
593
+	spc_index := reg.FindStringIndex(string(s2)) //在字符串中搜索
594
+	for len(spc_index) > 0 {                     //找到适配项
595
+		s2 = append(s2[:spc_index[0]+1], s2[spc_index[1]:]...) //删除多余空格
596
+		spc_index = reg.FindStringIndex(string(s2))            //继续在字符串中搜索
597
+	}
598
+	return string(s2)
599
+}