01-0004關於C/C++中的轉義字符(•́へ•́╬)

1.問題描述

1. 轉義字符’‘轉化爲’\‘或者’/’?
2. 如何將反斜槓轉化爲正斜槓或者雙斜槓?
3. 怎麼讓編譯器不解釋轉義字符?
4. 如何檢測字符是否發生轉義?
5. C語言中怎麼檢測字符是否轉義成功?
6. C語言中怎麼判斷一個字符是轉義字符?
7. C語言中有沒有原始字符串的用法?
8. Convert to Raw String Literal?
9. Print raw string from variable? C++?
10. Print raw string from variable? (not getting the answers)
11. Replace backward slashes with forwards slashes or double backward slashes in C++
12. C語言中怎麼把"C:\name\a.txt"轉化爲"C:\name\a.txt"或者"C:/name/a.txt"
13. C print raw bytes?
14. Print raw string from variable?
15. append the raw string modifier to a variable containing a string?
16. Add ‘u’ prefix to string?

上面這些問題,就是我查找的時候用到的可能的,能用的描述方式,但是最終只有一個結果:沒有答案

2.問題分析

  1. 想着不讓編譯器解釋轉義字符,就如同讓編譯器失職,不可能的,思想上有問題,如果這樣想,還不如自己出一個編譯器。
  2. 想着用一種非常偏僻的方式去給字符串變量添加R前綴,後者轉化爲原始字符串,這種想法就好像是某人腦子裏面知道“1+1=2”,卻偏要相同“1>1=2”要怎麼解釋,怎麼寫,怎麼運行纔是對的。【你現在所做的事情,方向是錯的,你想實現一個C/C++不支持的操作;結果是差的,因爲根本不存在這種語法;過程是悲慘的,沒想到吧,其他語言中有的支持這樣操作,而且沒人告訴你C++它不可以這樣操作。】
  3. 再看這個問題,爲什麼會手動定義一個字符串,用它存儲路徑的時候使用反斜槓?這不明顯是錯誤的嗎?剛學的時候知道修改成雙反斜槓?爲什麼現在要死在這個點上?
  4. 再者,從輸入中讀取的反斜槓,在存儲的時候會自動轉化爲雙反斜槓,沒有必要刻意的處理,這些都是編譯器以及編程語言規定好的,運行了幾十年沒有問題,到了自己這卻想着怎麼才能顛覆前人?這是錯的!

3.相關實現

3.1 使用原始字符串

#include <iostream>
#include <string>
#include <algorithm> //replace()
#include <cstring>
#include <atlstr.h>//CString
#include <cmath>
using namespace std;
int main() {
	//1.
	string path = R"(C:\hook\magic.txt)";//似乎只能使用原始字符串
	replace(path.begin(), path.end(), '\\', '/');//這裏的replace是algorithm裏面的
	cout << path << endl;
	//2.
	CString str = R"(C:\hook\magic.txt)";
	str.Replace('\\','/');
	wcout << str.GetString() << endl;
	//3.找到了轉化的方法,似乎是的???怎麼可能
	string str_root = "C:\hook\magic.txt";	
	cout << s + str_root << endl;
	//string str ="C:\hook\magic.txt";
	//string raw_string = R"()".format(str);
	//還是放棄吧!Give up!!!
}

3.2 從控制檯讀入

#include <iostream>
#include <string>
#include <algorithm> 
using namespace std;

int main() {
	string str;
	cout << "讀入字符串:";
	cin >> str;
	cout << "輸出字符串:";
	cout << str << endl;
	replace(str.begin(), str.end(), '\\', '/');
	cout << "轉化後結果:";
	cout << str << endl;
	return 0;	
}

輸出:

讀入字符串:C:\book\a.txt
輸出字符串:C:\book\a.txt
轉化後結果:C:/book/a.txt

3.3 轉化爲雙反斜槓

3.3.1 通過查找&插入實現

#include <iostream> 
#include <string>
using namespace std;
int main()
{
    string::size_type pos = 0;
    string str = "C:\\book\\1.txt";
    cout <<"原始字符串:"<< str << endl;

    while ((pos = str.find_first_of('\\', pos)) != string::npos)
    {
        str.insert(pos, "\\");//插入
        pos = pos + 2;
    }
    cout << "修改字符串:" << str << endl;

    return 0;
}

Output:

原始字符串:C:\book\1.txt
修改字符串:C:\\book\\1.txt

3.3.2 自己寫函數實現

#include <iostream> 
#include <string>
using namespace std;

//backslash to double backslash
//個人覺得這種方法效率最高,最多也就一次循環就結束了
//insert也會申請臨時變量,,可能要使用多次
string bs_to_dbs(string str) {
    string str_temp;
    for (int i = 0; i < str.size(); i++) {
        str_temp.push_back(str[i]);
        if (str[i] == '\\')
            str_temp.push_back(str[i]);
    }
    return str_temp;
}
//backslash to double backslash version-2
string bs_to_dbs_v2(string str) {
    for (int i = 0; i < str.size(); i++) {
        if (str[i] == '\\') {
            str = str.insert(i + 1, 1, '\\');
            i = i + 1;
        }          
    }
    return str;
}
int main()
{
    string str = "C:\\book\\1.txt";
    string str_temp = bs_to_dbs(str);
    string str_temp2 = bs_to_dbs_v2(str);
    cout << "原始字符串:" << str << endl;
    cout << "入棧方法修改:" << str_temp << endl;
    cout << "插入方法修改:" << str_temp << endl;
    return 0;
}

4.相關鏈接

1.c++11 - Can you combine the raw string modifier R"()" with a string variable? - Stack Overflow
2.The Complete Guide to Building Strings In C++: From “Hello World” Up To Boost Karma - Fluent C++
3.python - Add ‘u’ prefix to string - Stack Overflow

BText:這是對於編程語言理解不夠深刻的弊端,分不清它可以做什麼,不可以做什麼,是不知道的,C++的路,還有很遠,加油吧!!!!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章