Java中去除字符串中所有空格的几种方法

2016-02-19 09:08 13 1 收藏

想要天天向上,就要懂得享受学习。图老师为大家推荐Java中去除字符串中所有空格的几种方法,精彩的内容需要你们用心的阅读。还在等什么快点来看看吧!

【 tulaoshi.com - 编程语言 】

JAVA中去掉空格  

1. String.trim() 

trim()是去掉首尾空格 

2.str.replace(" ", ""); 去掉所有空格,包括首尾、中间 
代码如下:

String str = " hell o "; 
String str2 = str.replaceAll(" ", ""); 
System.out.println(str2); 
  
3.或者replaceAll(" +",""); 去掉所有空格 

4.str = .replaceAll("s*", ""); 
可以替换大部分空白字符, 不限于空格  
s 可以匹配空格、制表符、换页符等空白字符的其中任意一个 

来源:https://www.tulaoshi.com/n/20160219/1589689.html

延伸阅读
标签: Web开发
function cutstr(thestr,strlen) dim l,t,c l=len(thestr) t=0 for dxy=1 to l c=Abs(asc(Mid(thestr,dxy,1))) if c255 then t=t+2 else t=t+1 end if if t=strlen then thev=left(thestr,dxy) exit for else bu=strlen-t for bui=1 to bu strbu=" " strbuall=strbuall&strbu next thev=thestr&strbuall strbu="" strbuall="" end if next...
1.创建对象   对于java程序中的 字符串直接常量 ,JVM会使用一个字符串池来保存它们。当第一次使用某个字符串直接常量时,JVM会将它放入字符串池中进行缓存。在一般情况下,字符串池中的字符串对象不会被垃圾回收。当程序再次需要使用该字符串时,无需重新创建一个新的字符串就可以直接让引用变量直接指向字符串中已有的字符串。而 使用n...
代码如下: /*     String name = "adsbsadgsadgtewterfsdf";     eg a--6,b--1 d--3 ...     将字符串以a(字母)=2(个数)存入Map集合框架中    思路:1.将字符串转换成字符数组.           2.定义一个Map集合,然后对字符数组进行遍...
标签: Web开发
% '************************************************** '函数名:gotTopic '作 用:截字符串,汉字一个算两个字符,英文算一个字符 '参 数:str ----原字符串 ' strlen ----截取长度 '返回值:截取后的字符串 '************************************************** function gotTopic(str,strlen) if str="" then gotTopic="" exit function e...
use tempdb go --测试数据 declare @s varchar(1000) set @s='ak47,mp5,1,23' /*要求输出结果 S ---- ak47 mp5 1 23 */ --3种方法对比: --1.[朴实]动态Exec方法: declare @s1 varchar(1000) set @s1=right(replace(','+@s,',',''' as S union select '''),le...

经验教程

406

收藏

53
微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部