将字符串置换为大写和小写 [英] Permutate a String to upper and lower case

查看:149
本文介绍了将字符串置换为大写和小写的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个字符串,abc。程序如何(如果可能,在Java中)如何置换字符串?

I have a string, "abc". How would a program look like (if possible, in Java) who permute the String?

例如:

abc
ABC
Abc
aBc
abC
ABc
abC
AbC


推荐答案

这样的事情可以解决问题:

Something like this should do the trick:

void printPermutations(String text) {
  char[] chars = text.toCharArray();
  for (int i = 0, n = (int) Math.pow(2, chars.length); i < n; i++) {
    char[] permutation = new char[chars.length];
    for (int j =0; j < chars.length; j++) {
      permutation[j] = (isBitSet(i, j)) ? Character.toUpperCase(chars[j]) : chars[j];
    }
    System.out.println(permutation);
  }
}

boolean isBitSet(int n, int offset) {
  return (n >> offset & 1) != 0;
}

这篇关于将字符串置换为大写和小写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆