在字符串中查找重复的字符

时间:2015-07-30 17:03:16

标签: java string character

我知道这个问题被多次询问过,但我没有找到任何有用的答案。我有一个接收String的方法。我想检查字符串中的任何字符是否重复。如果是这样,该方法将返回一个空字符串。如果不是,它将返回String。 该方法正在查找字符串中的任何重复字符。

@font-face {
    font-family: 'UbuntuM'; /*a name to be used later*/
    src: url('../fonts/ubuntu-font-family/0.80/Ubuntu-M.ttf'); /*URL to font*/
}

@font-face {
    font-family: 'UbuntuR'; /*a name to be used later*/
    src: url('../fonts/ubuntu-font-family/0.80/Ubuntu-R.ttf'); /*URL to font*/
}

@font-face {
    font-family: 'ubuntu-light'; /*a name to be used later*/
    src: url('../fonts/ubuntu-font-family/0.80/ubuntu-light.woff'); /*URL to font*/
}



* {
    font-size:1em;
    box-sizing: border-box;
}

*:after, *:before {
    box-sizing: border-box;
}
html,body {
    height:100%;
    width:100%;
    margin:0;
}
body {
    color:#5c5c5c;
    font-family: 'UbuntuR'; 
}

.clear_both {
    clear:both;
}
.mediumfont {
    font-family: 'UbuntuM'; 
}


header {
    box-sizing: border-box;
    height:60px;
    line-height:45px;
    text-align:center;
    position: absolute;
    top:0;
    left:0;
    right:0;
    width:100%;
    background-color:#ecedec;
    color:#5c5c5c;
    padding:5px;
    border-bottom: 10px solid #d7d7d7;
}

#headertitle,
.weather_header {
    font-family: 'ubuntu-light';    
    padding-left: 10px;
    font-size:20px;
    line-height:42px;
}
.header_icon > a{
  font-size: 25px;
  padding: 0 7px;
  color: #5c5c5c;
}

#content {
    box-sizing: border-box;
    position: absolute;
    top:60px;
    bottom:0px;
    left:0;
    right:0;
    width:100%;
    background-color:#ecedec;
    color:#000000;
    overflow-y: auto;
    padding:5px;
}

#content .tab {
    overflow: hidden;
    position:relative;
    margin:5px;
}
#content .tab_list {
    /*
    position: absolute;
    bottom: 0;
    top: 0px;
    left: 0;
    right: 0;   
    */
}

#content .tab,
.weather_data_wrapper {
    /*position: absolute;
    top: 0;
    bottom: 0;
    overflow-y: auto;
    right: 0;
    left: 0;
    */
}

/*
footer {
    height:30px;
    line-height:30px;
    text-align:center;
    position: absolute;
    bottom:0;
    left:0;
    right:0;
    width:100%;
    background-color:red;
    padding:5px;
}
*/


.about_logo_wrapper {
    text-align: center;
    margin-top:10px;
}

.about_logo_wrapper > img {
    width:33%;
    height:auto;
    display:inline-block;
}

.about_text_wrapper {
    text-align:center;
}

.about_title {
  display: block;
  margin: 10px;
  font-size: 1.5em;
  margin-bottom: 2px;
}

.about_template ul {
    list-style: none;
    padding:0;
}
.about_template ul li{
    margin:5px;
}

.about_desc {
    margin-top:35px 0px;
}


#errorOverlay,
#loadingOverlay {
    background-color:rgba(255, 255, 255, 0.5);
    position:absolute;
    top:0;
    bottom:0;
    left:0;
    right:0;
    z-index:100
}

.error_overlay_wrapper{
    position: absolute;
    left: 50%;
    top: 50%;
    text-align: center;        
    width:100%;
    height:10%;
    margin-left: -50%; /*half width*/
    margin-top: -5%; /*half height*/
}

.loading_overlay_wrapper{
    position: absolute;
    left: 50%;
    top: 50%;
    text-align: center;        
    width:100px;
    height:100px;
    margin-left: -50px; /*half width*/
    margin-top: -50px; /*half height*/
}

img {
    max-width:100%;
    max-height:100%;
}
/*
.hourly_weather_date_day,
.daily_weather_date_day,
.hourly_weather_desc,
.daily_weather_desc,
.detail_info_weather
*/
.hourly_weather_desc,
.daily_weather_desc {
    font-size:0.75em;
}
.weather_data,
.detail_info_weather,
.summary_info_weather {
    color: #FFFFFF; 
}

.hourly_weather > .detail_info_weather,
.daily_weather > .detail_info_weather
{
    display:none;
}
.hourly_weather.show_weather_detail > .detail_info_weather,
.daily_weather.show_weather_detail > .detail_info_weather
{
    display:block;
}

.weather_data {
    padding: 5px;
}

.hourly_weather.weather_data,
.daily_weather.weather_data
{
    margin-bottom:5px;
}
.current_weather_desc,
.hourly_weather_desc,
.daily_weather_desc  {
    text-transform: capitalize; 
}

.detail_temp_min_max_group {
    display:inline-block;
}
.detail_icon {
    height:1em; 
}

.detail_icon_bkg {
    background-size: 18px;
    padding-left:20px;
    background-repeat: no-repeat;
    background-position: left center;   
}

.detail_icon_sunrise_bkg {
    background-image:url(/img/details_icon/icon_sunrise.svg);
}
.detail_icon_sunset_bkg {
    background-image:url(/img/details_icon/icon_sunset.svg);
}
.detail_icon_temp_bkg {
    background-image:url(/img/details_icon/icon_temp.svg);
}
.detail_icon_temp_min_max_bkg {
    background-image:url(/img/details_icon/icon_temp_min_max.svg);  
}
.detail_icon_pressure_bkg {
    background-image:url(/img/details_icon/icon_pressure.svg);
}
.detail_icon_humidity_bkg {
    background-image:url(/img/details_icon/icon_humidity.svg);
}
.detail_icon_cloud_bkg {
    background-image:url(/img/details_icon/icon_cloud.svg);
}
.detail_icon_rain_bkg {
    background-image:url(/img/details_icon/icon_rain.svg);
}
.detail_icon_wind_orientation_bkg {
    background-image:url(/img/details_icon/icon_wind_orientation.svg);
}
.detail_icon_wind_speed_bkg {
    background-image:url(/img/details_icon/icon_wind_speed.svg);
}

.summary_info_current_weather {
    text-align:center;
}
.current_weather_logo {
    width: 50%;
    display:inline-block;
}


/*black*/
.weather_data.weather_200,
.weather_data.weather_201,
.weather_data.weather_202,
.weather_data.weather_210,
.weather_data.weather_211,
.weather_data.weather_212,
.weather_data.weather_221,
.weather_data.weather_230,
.weather_data.weather_231,
.weather_data.weather_232,
.weather_data.weather_900,
.weather_data.weather_901,
.weather_data.weather_902,
.weather_data.weather_903,
.weather_data.weather_904,
.weather_data.weather_905,      
.weather_data.weather_906,
.weather_data.weather_950,
.weather_data.weather_951,
.weather_data.weather_952,
.weather_data.weather_953,
.weather_data.weather_954,
.weather_data.weather_955,
.weather_data.weather_956,
.weather_data.weather_957,
.weather_data.weather_958,
.weather_data.weather_959,
.weather_data.weather_960,
.weather_data.weather_961,
.weather_data.weather_962
{
    background-color:#000000;
}

/*blue*/
.weather_data.weather_300,
.weather_data.weather_301,
.weather_data.weather_302,
.weather_data.weather_310,
.weather_data.weather_311,
.weather_data.weather_312,
.weather_data.weather_321,
.weather_data.weather_500,
.weather_data.weather_501,
.weather_data.weather_502,
.weather_data.weather_503,
.weather_data.weather_504,
.weather_data.weather_511,
.weather_data.weather_520,
.weather_data.weather_521,
.weather_data.weather_522,
.weather_data.weather_531,
.weather_data.weather_600,
.weather_data.weather_601,
.weather_data.weather_602,
.weather_data.weather_611,
.weather_data.weather_621
{
    background-color:#55AAFF;
}

/*gray*/
.weather_data.weather_701,
.weather_data.weather_711,
.weather_data.weather_721,
.weather_data.weather_731,
.weather_data.weather_741,
.weather_data.weather_751,
.weather_data.weather_761,
.weather_data.weather_762,
.weather_data.weather_771,
.weather_data.weather_781,
.weather_data.weather_802,
.weather_data.weather_803,
.weather_data.weather_804
{
    background-color:#808080;
}



/*yellow*/
.weather_data.weather_800,
.weather_data.weather_801
{
    background-color:#E0B000;
}
/*night*/
.weather_data.night {
    background-color:#41403b;
}

.daily_weather_logo,
.hourly_weather_logo {
    width: 100px;
    display:inline-block;
}

.weather_header {
    padding:0px;
    position: relative;
}
.weather_header_wrapper {
    margin:5px;
    background-color:#d7d7d7;
}
.weather_header_wrapper > div {
    text-align:center;
}

.row_weather > div {
    text-align:center;
}

.row_weather {
    display: table;
    width:100%;
    table-layout: fixed;
}
.row_weather > div span {
    display: inline-block;
    width:100%;
}

.row_weather [class*="col-"] {
    float: none;
    display: table-cell;
    vertical-align: middle;
}


.align-left {
    text-align:left;
}

.weather_data .separator
{
    margin-top: 10px;
    padding-bottom: 10px;
}

.separator {
    text-align:center;
    line-height: 1px;
    height: 1px;
    position: relative;
    background-color:transparent;
}

.separator > span {
    display: inline-block;
    border-top: 1px solid #FFFFFF;
    position: absolute;
    top: 0;
    bottom: 0;
    right: 10%;
    left: 10%;
}

.weather_type {
    position: absolute;
    left: 0;
    right: 0;
    top: 0;
    bottom: 0;
}

.weather_chooser {
    position:relative;
}


.container-centered {
    height: 100%;
    text-align: center;  /* align the inline(-block) elements horizontally */
    font: 0/0 a;         /* remove the gap between inline(-block) elements */
}

.container-centered:before {    /* create a full-height inline block pseudo=element */
    content: ' ';
    display: inline-block;
    vertical-align: middle;  /* vertical alignment of the inline element */
    height: 100%;
}

.elem-centered {
    display: inline-block;
    vertical-align: middle;  /* vertical alignment of the inline element */
    font: 16px/1 Arial sans-serif;        /* <-- reset the font property */
}

4 个答案:

答案 0 :(得分:3)

好的,我发布了我的解决方案:

{Tue, 28 Jul 2015=>100}

答案 1 :(得分:0)

这可能不是做你想做的最好的方法,但是你可以使用两个for循环来检查每个字符与其他所有字符是否重复。

public static String hasRepeated(String word) {
    if (word.isEmpty()) return "<empty>";
    char[] charArray = word.toCharArray();
    for (int i = 0; i < charArray.length; i++) {
        for (int j = 0; j < charArray.length; j++) {
            if (i == j) {
            } else if (Character.toString(charArray[i]).
                    equalsIgnoreCase(Character.toString(charArray[j]))) {
                return "";
            }
        }
    }
    return word;
}

注意:此代码假定角色的情况并不重要,只是检查它是否重复。

答案 2 :(得分:0)

   /**
    * Returns the index of the first character repeated, or -1 if no repeats
    */
   public static int firstRepeated( String s ) {
      if ( s != null ) {
         int n = s.length();
         for (int i = 0; i < (n - 1); i++) {
            int indx = s.indexOf( s.charAt( i ), i + 1 );
            if ( indx > 0 ) {
               return i;
            }
         }
      }
      return -1;
   }

答案 3 :(得分:-2)

这有效!!

public static String checkDuplicate(String str)
{
    int count = 0;

    char[] charArray = str.toCharArray();
    for(int i=0;i<charArray.length;i++)
    {
        count = 0;
        for(int j=0;j<charArray.length;j++)
        {
            if(charArray[i]==charArray[j])
            {

                count++;
                if(count==2) break;
            }
        }
        if(count==2) break;
    }
    if(count==2)
        return "";
    else
        return str;
}
}