转换字符串标记器

时间:2019-04-15 13:16:55

标签: c# c++

有人可以帮助我将StrTok代码从C ++转换为C#吗?

strFileName = StringFunctions.StrTok(strFileToCopy, "\\");
strFileName = StringFunctions.StrTok(null, "\0");

尝试代码:

string strFileName = ""; 

string[] FileNames = Regex.Split(strFileToCopy, "\\"); 

2 个答案:

答案 0 :(得分:0)

基于此处fun isAppInForeground(): Boolean { val activityManager = getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager ?: return false val appProcesses = activityManager.runningAppProcesses ?: return false val packageName = packageName for (appProcess in appProcesses) { if (appProcess.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND && appProcess.processName == packageName) { return true } } return false } 的描述和示例:http://www.cplusplus.com/reference/cstring/strtok/,看来*大致等效于## sample code 1 import numpy as np 2 x = np.array([[-1,-1],[-2,-1],[-3,-2],[1,1],[2,1],[3,2]]) 3 y = np.array([1,1,1,2,2,2]) 4 from sklearn.naive_bayes import GaussianNB 5 clf = GaussianNB(x, y) 6 clf = clf.fit(x,y) ###showing error on compiling 7 print(clf.predict([[-2,1]])) ## output shown Traceback (most recent call last): File "naive.py", line 7, in <module> clf = clf.fit(x,y) File "/home/abhihsek/.local/lib/python3.6/site- packages/sklearn/naive_bayes.py", line 192, in fit sample_weight=sample_weight) File "/home/abhihsek/.local/lib/python3.6/site- packages/sklearn/naive_bayes.py", line 371, in _partial_fit raise ValueError('Number of priors must match number of' ValueError: Number of priors must match number of classes. ## code of library function line 192 190 X, y = check_X_y(X, y) 191 return self._partial_fit(X, y, np.unique(y), _refit=True, 192 sample_weight=sample_weight) ## code of library function line 371 369 # Check that the provide prior match the number of classes 370 if len(priors) != n_classes: 371 raise ValueError('Number of priors must match number of' 372 ' classes.') 373 # Check that the sum is 1 的{​​{3}}方法:

StrTok

这将输出:

  

     

a

     

样本

     

字符串

与链接的strtok页面中的示例匹配。

*我认为区别在于要使用strtok获取所有条目,您必须重复调用它,并传递string进行后续调用,而C#的var str = "- This, a sample string."; var pch = str.Split(" ,.-".ToCharArray(), StringSplitOptions.RemoveEmptyEntries); foreach (var result in pch) { Console.WriteLine(result); } 仅返回令牌数组。


正如其他人在评论中所建议的那样,如果您只想获取文件名,则应改用NULL(您需要添加Split):

Path.GetFileName

输出:

  

test.txt

答案 1 :(得分:-1)

您可以使用std中的字符串流

std::istringstream ss("This is the line to tokenize");
std::string token;

while(std::getline(ss, token, ' '))
    std::cout << "line: " << token << "\n";

// Out:
/*
This
is
the
line
to
tokenize
*/