Re: [問題] 如何把Strings裡面分成很多不同的東西
※ 引述《BlackMatrix (BlackMatrix)》之銘言:
: 問題是, 我想把一個一串Strings加數字讓他變得不一樣
: 有四種條件:
: The return value will be the ROT13 encoding of the input file. For our
: purposes the ROT13 algorithm is defined as follows:
: A) If the input is a lower case letter, the output is the lower case
: letter plus 13 (with wrap-around, so 'n' becomes 'a')
: B) If the input is a upper case letter, the output is the upper case
: letter plus 13 (with wrap-around, so 'n' becomes 'a')
: C) If the input is a number the output is the number plus 5 (with
: wrap-around so '6' becomes '1')
: D) If the input is neither letter nor number, the output is the same
: as the input ('!' becomes '!' and ' ' becomes ' ')
: 也就是說:
: 假如我的文件, test.txt裡面的一串字是1234那就會變成5678, 然後
: 1a2a3a4a 就會變成 5n6n7n8n 因為a+13會變成n, 大寫跟小寫都會
: 我現在可以用Scanner跟Reader來讀取我的strings, 可是重點是我不會改變他們的數字或
: 是字母
: 問題1:
: a+13會變成a13, 那要怎麼才能讓他跳13個字母
: 111我+5會變成545454...非常詭異
: 問題2:
: 因為我之前有學過C++, 我就想說把Strings當成Array of Chars可是似乎沒辦法
: 所以我就把他全部都寫到 char[] charArray = new char [encodedFile.length()]
: 然後利用.charAt(i) <== i 是用for loop
: 我現在所有的字母跟數字都在Array of Char裡面, 可是我要怎麼用if statement來表示
: A. 如果碰到字母要跳13位
: B. 如果碰到數字要+5
: 目前我A完全不會做, B我這樣寫, charArray[5] += 5是沒辦法這樣做的.
: 因為我似乎把Char當成Int來用, 所以...到底該怎麼辦
: 我在網上查了很多資料, 我有用Replace可是完全沒辦法用
: 我的Code在底下:
: http://nopaste.csie.org/a4a9b
: 第一次寫Java, 買了Just Java2 可是沒有幫助我太多
: 因為實在寫不出來, 請大家麻煩指點指點
給你一點提示
我下面寫的東西.
是把一條 String, 裡面的大草變小草, 小草變大草.
把裡面用到的東西搞懂, 你的問題也就不難做了
public static String changeCase(String input) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < input.length(); i++) {
char c = input.charAt(i);
if (c >= 'a' && c <= 'z') {
c = (char)(c - 32);
} else if (c >= 'A' && c <= 'Z') {
c = (char)(c + 32);
}
sb.append(c);
}
return sb.toString();
}
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 61.238.156.189
推
02/05 17:00, , 1F
02/05 17:00, 1F
推
02/05 17:46, , 2F
02/05 17:46, 2F
→
02/06 00:19, , 3F
02/06 00:19, 3F
討論串 (同標題文章)
本文引述了以下文章的的內容:
完整討論串 (本文為第 2 之 2 篇):