我想知道如何在Python中检查字符串是否以"hello"开头.
在Bash我经常做:
if [[ "$string" =~ ^hello ]]; then
do something here
fi
Run Code Online (Sandbox Code Playgroud)
我如何在Python中实现相同的目标?
我需要循环一些值,
for i in $(seq $first $last)
do
does something here
done
Run Code Online (Sandbox Code Playgroud)
对于$first
和$last
,我需要它是固定长度5.所以如果输入是1
,我需要在前面添加零,使它成为00001
.它循环直到99999
例如,但长度必须是5.
如:00002
,00042
,00212
,012312
等等.
关于我如何做到这一点的任何想法?
c ++中是否有特定的函数可以返回我想要查找的特定字符串的行号?
ifstream fileInput;
int offset;
string line;
char* search = "a"; // test variable to search in file
// open file to search
fileInput.open(cfilename.c_str());
if(fileInput.is_open()) {
while(!fileInput.eof()) {
getline(fileInput, line);
if ((offset = line.find(search, 0)) != string::npos) {
cout << "found: " << search << endl;
}
}
fileInput.close();
}
else cout << "Unable to open file.";
Run Code Online (Sandbox Code Playgroud)
我想在以下位置添加一些代码:
cout << "found: " << search << endl;
Run Code Online (Sandbox Code Playgroud)
这将返回行号,后跟搜索到的字符串.
我正在尝试读取一个文本文件,例如 file.txt,它包含多行。
说输出file.txt
是
$ cat file.txt
this is line 1
this is line 2
this is line 3
Run Code Online (Sandbox Code Playgroud)
我想将整个输出存储为变量,例如$text
.
当变量$text
被回显时,预期输出为:
this is line 1 this is line 2 this is line 3
Run Code Online (Sandbox Code Playgroud)
我的代码如下
while read line
do
test="${LINE}"
done < file.txt
echo $test
Run Code Online (Sandbox Code Playgroud)
我得到的输出始终只是最后一行。有没有一种方法可以将 file.txt 中的多行连接为一个长字符串?
嗨,我有以下代码:
char msg[10000];
string mystr = "hello";
Run Code Online (Sandbox Code Playgroud)
我想将 mystr 放入 msg 中。有没有办法做到这一点?我尝试了各种方法,但不断得到:
incompatible types in assignment of 'const char*' to char [10000]'
Run Code Online (Sandbox Code Playgroud)
我试过:
msg = mystr.c_str();
Run Code Online (Sandbox Code Playgroud)
和
msg = (char[10000])mystr;
Run Code Online (Sandbox Code Playgroud)
无济于事。
嗨,我有以下代码:
char msg[10000];
string mystr = "hello";
// stores mystr into msg
copy(mystr.begin(), mystr.end(), msg);
Run Code Online (Sandbox Code Playgroud)
代码复制后,我想清除char msg [10000]的内容.我怎么做?
if ((clearance.equals("")) || (!clearance.equals("0")) || (!clearance.equals("1"))){
System.out.println("Invalid entry, exiting.\n");
System.exit(0);
}
Run Code Online (Sandbox Code Playgroud)
但是,当我键入0或1时,它仍会打印"无效条目,退出".
知道出了什么问题吗?
在bash中,我需要运行一个从i = 1循环到i = 99999999的脚本,但它总是耗尽内存.有没有解决方法?或者我有最大值吗?
first=1
last=99999999
randomString="CXCXQOOPSOIS"
for val in $( seq $first $last )
do
padVal=$( printf "%010d\n" $val )
hash=$( echo -n $randomString$padVal | md5sum )
if [[ "$hash" =~ ^000000) ]]; then
echo "Number: $val" >> log_000000
echo "$val added to log - please check."
fi
done
Run Code Online (Sandbox Code Playgroud)