2014-09-19 24 views
1

我討厭這樣問,但我非常接近於搞清楚它,它真的困擾着我。我需要將我的字符串轉換爲camelcase。我擺脫了空間,我可以大寫正確的字母,我可以刪除元音,但我需要保留代碼的第一個字母,我似乎無法讓它留下來。我試過用六種不同的方式對它進行索引,但無濟於事。保留第一個元音,同時刪除其餘的元素MATLAB

function[cameltoe] = abbreviatingCamelCase(firstWord) 
indexing = find(firstWord(1:end - 1) == ' ');%I want to find all the spaces here 
firstWord(indexing + 1) = upper(firstWord(indexing + 1)); %I want to uppercase all the words  following a space 
firstWord(firstWord == ' ') = []; 
firstWord(ismember(firstWord, ' aeiou')) = []; 
cameltoe = firstWord; 

'一條魚兩條魚紅魚藍魚'應該變成'onFshTwFshRdFshBlFsh'。我一直在爲此工作至少兩個小時。我嘗試在'aeiou'的內部找到第一個詞,但這似乎不起作用。

+0

所以像firstWord(2:結束)? – 2014-09-19 20:31:58

回答

1

希望你不要介意我創建了一個額外的變量。這是你在找什麼?

firstWord = 'one fish two fish red fish blue fish' 
indexing = find(firstWord(1:end - 1) == ' ');%I want to find all the spaces here 
firstWord(indexing + 1) = upper(firstWord(indexing + 1)); %I want to uppercase all the words  following a space 
firstWord(firstWord == ' ') = []; 
Li = ismember(firstWord, 'aeiou'); 
Li(find(Li,1,'first'))=0; 
firstWord(Li) = []; 
cameltoe = firstWord 

編輯:如果你想保留的第一個字母,無論是元音:

indexing = find(firstWord(1:end - 1) == ' '); 
firstWord(indexing + 1) = upper(firstWord(indexing + 1)); 
firstWord(firstWord == ' ') = []; 
firstWord([false ismember(firstWord(2:end), 'aeiou')]) = []; 
cameltoe = firstWord; 
+0

....是的。我喜歡爲此而愛你。我試圖做到這一點,但我想我做了一件奇怪的事。這就像我存在的禍根,所以這就像一個巨大的安慰。我只是很想好好想看第一件作品。我試圖直接索引它。除了現在它與我的第一個測試案例混淆:/'woot我愛matlab'需要'wtILvMtlb',它現在有一個o現在 – 2014-09-19 20:41:20

+0

''first''是'find' funcion的一部分:'find( ,k,'first')'給出邏輯中的'k'個第一個元素。 – 2014-09-19 20:43:51

+0

它說你想要第一個元音嗎?你只想要第一個字母嗎?這使得生活變得更容易 – 2014-09-19 20:44:56

相關問題