去除字串中的空格,並將字串翻轉

2021-08-10 01:39:23 字數 672 閱讀 9398

前幾天參加校招,遇到乙個問題,現做分享。

題意大致如下:

將string world = 「hello world」;去除所有的空格,並將字元翻轉輸出

答:

string b = world.

replace

(" ",""

);//去除空格

string c =

newstringbuffer

(b).

reverse()

.tostring()

; 利用stringbuffer進行翻轉

system.out.

println

(c);

//輸出

public

static

void

main

(string[

] args)

for(

int j =

0; j < str.

length()

; j++

) system.out.

println

(new

string

(chars));

}

去除字串中的空格

使用js去除字串內所帶有空格,有以下三種方法 1 replace正則匹配方法 去除字串內所有的空格 str str.replace s g,去除字串內兩頭的空格 str str.replace s s g,去除字串內左側的空格 str str.replace s 去除字串內右側的空格 str str...

去除字串中的空格

利用迭代的思想將字串中的空字元去除 去除字串中的所有空格 deftrim s index 0while index len s if s index if index 0 如果是首字母是空格,直接向後推一位進行迭代 return trim s index 1 len s if index len s...

js去除字串空格

方法一 使用replace正則匹配的方法 去除所有空格 str str.replace s g,去除兩頭空格 str str.replace s s g,去除左空格 str str.replace s 去除右空格 str str.replace s g,str為要去除空格的字串,例項如下 var s...