在Java中的两个字符之间添加空格

时间:2021-01-02 18:58:47

标签: java string

得到包含两个符号的 String

String s = "AB";

如何在两者之间添加 space " "

3 个答案:

答案 0 :(得分:0)

一个字符串是不可变的,这意味着你不能改变它,所以你需要创建一个新的字符串。

一种方法是使用 Bundle completed (36.93s) Cleaning up the bundler cache. Detecting rake tasks ! ! Could not detect rake tasks ! ensure you can run `$ bundle exec rake -P` against your app ! and using the production group of your Gemfile. ! [...] rubygems_integration.rb:460:in `block in replace_bin_path': can't find executable rake for gem rake. rake is not currently included in the bundle, perhaps you meant to add it to your Gemfile? (Gem::Exception) from /[...]ruby/2.6.0/gems/bundler-2.0.2/lib/bundler/rubygems_integration.rb:491:in `block in replace_bin_path' from ./vendor/bundle/bin/rake:29:in `<main>' ![...]helpers/rake_runner.rb:106:in `load_rake_tasks!': Could not detect rake tasks (LanguagePack::Helpers::RakeRunner::CannotLoadRakefileError) ensure you can run `$ bundle exec rake -P` against your app and using the production group of your Gemfile. /tmp/build_dcccbbf6_/vendor/bundle/ruby/2.6.0/gems/bundler- 2.0.2/lib/bundler/rubygems_integration.rb:460:in `block in replace_bin_path': can't find executable rake for gem rake. rake is not currently included in the bundle, perhaps you meant to add it to your Gemfile? (Gem::Exception) from /tmp/build_dcccbbf6_/vendor/bundle/ruby/2.6.0/gems/bundler-2.0.2/lib/bundler/rubygems_integration.rb:491:in `block in replace_bin_path' from ./vendor/bundle/bin/rake:29:in `<main>' from /tmp/codon/tmp/buildpacks/63cff2a36c882a48324b4b7a5466efcada2e529a/lib/language_pack/ruby.rb:1035:in `rake' from /tmp/codon/tmp/buildpacks/63cff2a36c882a48324b4b7a5466efcada2e529a/lib/language_pack/rails4.rb:84:in `block (2 levels) in run_assets_precompile_rake_task' from /tmp/codon/tmp/buildpacks/63cff2a36c882a48324b4b7a5466efcada2e529a/lib/language_pack/base.rb:190:in `log' from /tmp/codon/tmp/buildpacks/63cff2a36c882a48324b4b7a5466efcada2e529a/lib/language_pack/rails4.rb:78:in `block in run_assets_precompile_rake_task' from /tmp/codon/tmp/buildpacks/63cff2a36c882a48324b4b7a5466efcada2e529a/lib/language_pack/instrument.rb:18:in `block (2 levels) in instrument' from /tmp/codon/tmp/buildpacks/63cff2a36c882a48324b4b7a5466efcada2e529a/lib/language_pack/instrument.rb:40:in `yield_with_block_depth' from /tmp/codon/tmp/buildpacks/63cff2a36c882a48324b4b7a5466efcada2e529a/lib/language_pack/instrument.rb:17:in `block in instrument' from /tmp/tmp.9BjaQ6WXw6/lib/ruby/2.6.0/benchmark.rb:308:in `realtime' from /tmp/codon/tmp/buildpacks/63cff2a36c882a48324b4b7a5466efcada2e529a/lib/language_pack/instrument.rb:16:in `instrument' from /tmp/codon/tmp/buildpacks/63cff2a36c882a48324b4b7a5466efcada2e529a/lib/language_pack/base.rb:50:in `instrument' from /tmp/codon/tmp/buildpacks/63cff2a36c882a48324b4b7a5466efcada2e529a/lib/language_pack/base.rb:46:in `instrument' from /tmp/codon/tmp/buildpacks/63cff2a36c882a48324b4b7a5466efcada2e529a/lib/language_pack/rails4.rb:77:in `run_assets_precompile_rake_task' from /tmp/codon/tmp/buildpacks/63cff2a36c882a48324b4b7a5466efcada2e529a/lib/language_pack/ruby.rb:111:in `block (2 levels) in compile' from /tmp/codon/tmp/buildpacks/63cff2a36c882a48324b4b7a5466efcada2e529a/lib/language_pack/ruby.rb:1056:in `allow_git' from /tmp/codon/tmp/buildpacks/63cff2a36c882a48324b4b7a5466efcada2e529a/lib/language_pack/ruby.rb:104:in `block in compile' from /tmp/codon/tmp/buildpacks/63cff2a36c882a48324b4b7a5466efcada2e529a/lib/language_pack/instrument.rb:18:in `block (2 levels) in instrument' from /tmp/codon/tmp/buildpacks/63cff2a36c882a48324b4b7a5466efcada2e529a/lib/language_pack/instrument.rb:40:in `yield_with_block_depth' from [... a lot more lines like this] ! Push rejected, failed to compile Ruby app. ! Push failed

  1. 使用原始字符串创建 StringBuilder
  2. 使用 StringBuilder 方法插入字符
  3. 使用 StringBuilder.insert(...)toString() 方法创建一个新字符串。

答案 1 :(得分:0)

您可以使用以下方法: ''' Lexicographic permutation generation consider example array state of [1,5,6,4,3,2] for sorted [1,2,3,4,5,6] after 56432(treat as number) ->nothing larger than 6432(using 6,4,3,2) beginning with 5 so 6 is next larger and 2345(least using numbers other than 6) so [1, 6,2,3,4,5] ''' def hasNextPermutation(array, len): ' Base Condition ' if(len ==1): return False ''' Set j = last-2 and find first j such that a[j] < a[j+1] If no such j(j==-1) then we have visited all permutations after this step a[j+1]>=..>=a[len-1] and a[j]<a[j+1] a[j]=5 or j=1, 6>5>4>3>2 ''' j = len -2 while (j >= 0 and array[j] >= array[j + 1]): j= j-1 if(j==-1): return False # print(f"After step 2 for j {j} {array}") ''' decrease l (from n-1 to j) repeatedly until a[j]<a[l] Then swap a[j], a[l] a[l] is the smallest element > a[j] that can follow a[l]...a[j-1] in permutation before swap we have a[j+1]>=..>=a[l-1]>=a[l]>a[j]>=a[l+1]>=..>=a[len-1] after swap -> a[j+1]>=..>=a[l-1]>=a[j]>a[l]>=a[l+1]>=..>=a[len-1] a[l]=6 or l=2, j=1 just before swap [1, 5, 6, 4, 3, 2] after swap [1, 6, 5, 4, 3, 2] a[l]=5, a[j]=6 ''' l = len -1 while(array[j] >= array[l]): l = l-1 # print(f"After step 3 for l={l}, j={j} before swap {array}") array[j], array[l] = array[l], array[j] # print(f"After step 3 for l={l} j={j} after swap {array}") ''' Reverse a[j+1...len-1](both inclusive) after reversing [1, 6, 2, 3, 4, 5] ''' array[j+1:len] = reversed(array[j+1:len]) # print(f"After step 4 reversing {array}") return True array = [1,2,4,4,5] array.sort() len = len(array) count =1 print(array) ''' The algorithm visits every permutation in lexicographic order generating one by one ''' while(hasNextPermutation(array, len)): print(array) count = count +1 # The number of permutations will be n! if no duplicates are present, else less than that # [1,4,3,3,2] -> 5!/2!=60 print(f"Number of permutations: {count}")

答案 2 :(得分:0)

一个简单的方法是在每个字符上拆分字符串并使用空格作为分隔符连接部分。

演示:

public class Main {
    public static void main(String[] args) {
        String s = "AB";
        s = String.join(" ", s.split(""));
        System.out.println(s);
    }
}

输出:

A B