我有一个数组,其中包含一串数字,例如:1011
我想将其分成四个单独的数组,其中包含每个值。我该怎么做呢?
String [] array = {1,0,1,1};
//would I do something like this:
array.substring(0,4)
Run Code Online (Sandbox Code Playgroud) 我喜欢删除以 ; 结尾的 richtextbox 的最后一行。半柱。我喜欢删除这一行,直到;最后一个半列之前的半列。
例子:
hello do not delete this line;
hello this sentence will continue...
untill here;
Run Code Online (Sandbox Code Playgroud)
结果应该是:
hello do not delete this line;
Run Code Online (Sandbox Code Playgroud)
我的代码:
private void button1_Click_1(object sender, EventArgs e) {
List<string> myList = richTextBox1.Lines.ToList();
if (myList.Count > 0) {
myList.RemoveAt(myList.Count - 1);
richTextBox1.Lines = myList.ToArray();
richTextBox1.Refresh();
}
}
Run Code Online (Sandbox Code Playgroud) 伙计们,我有一根绳子Model.Products[i].Description。长度未知。
我只想将它的前 60 个字符放在变量中。
var data = Model.Products[i].Description;
var temp = string.Join(string.Empty,data.Take(60));
Run Code Online (Sandbox Code Playgroud)
给我一个空值异常错误。我做错了什么 ?
有什么帮助吗?
我有一个具有以下格式的字符串:
'User ID: 2894, Task ID: 68, Some other text'
Run Code Online (Sandbox Code Playgroud)
假设我需要将此字符串转换为以下内容:
'User ID: 2684, Task ID: <replaced>, Some other text'
Run Code Online (Sandbox Code Playgroud)
显然,执行此操作的方法是将 string 替换68为 string <replaced>。
'User ID: 2894, Task ID: 68, Some other text'
Run Code Online (Sandbox Code Playgroud)
User ID但如果包含相同的数字序列,这可能会在我脸上爆炸。
'User ID: 2684, Task ID: <replaced>, Some other text'
Run Code Online (Sandbox Code Playgroud)
但是,我还有有关应替换的子字符串的字符索引的信息。所以我有以下代码来完成此任务:
var str = 'User ID: 2894, Task ID: 68, Some other text';
var newStr = str.replace('68', '<replaced>');
alert(newStr);Run Code Online (Sandbox Code Playgroud)
正如你所看到的,现在我需要调用substring两次才能得到结果。但是是否有更简单的代码可以用来替换给定偏移量之后出现的子字符串?
我正在尝试使用正则表达式获取大括号内的子字符串。
假设我有以下字符串:
"This is a {nice} text to search for a {cool} substring".
Run Code Online (Sandbox Code Playgroud)
我希望能够搜索nice并cool
我正在尝试提取最后一个句点(点)之后的子字符串。下面的例子。
echo "filename..txt" 应该返回“txt”echo "filename.txt." 应该返回“”echo "filename" 应该返回“”echo "filename.xml" 应该返回“xml”我在下面尝试过。但仅当字符(点)存在一次时才有效。但我的文件名可能有 0 次或多次 (点)。
echo "filename.txt" | cut -d "." -f2
Run Code Online (Sandbox Code Playgroud) 我四处搜索,找不到在任何地方列出的这个问题,我花了一些时间尝试让它与两个循环一起工作,但没有成功。我会对任何更短、更优雅的东西感兴趣。这是一个练习,但不是家庭作业问题,正如标题所说,我应该保持简单,即没有数组/列表/哈希等。
到目前为止我的工作:
String str = "string";
String subString;
int k = 1;
while (k <= str.length()) {
for (int i = 0; i < str.length(); i++) {
for (int j = i+1; j <= str.length(); j++) {
subString = str.substring(i,j);
if (subString.length()==k) {System.out.println(subString);}
}
}
k++;
}
Run Code Online (Sandbox Code Playgroud) 为什么没有子串函数?
我可以做一些类似 javascript 原型设计的事情,所以我至少可以做一些类似的事情:
string.substring(0,7)
Run Code Online (Sandbox Code Playgroud)
还是我被迫在这里使用我的功能?:
func substring(str string, start int, length int) string {
return string([]rune(str)[start:length+start])
}
Run Code Online (Sandbox Code Playgroud) 在单元格 A1 中,我有以下格式的字符串:
xxxxxx xxxxxx xxxxxx 00 xxxxx xxxxxx xxxxx xxxxxx
如您所见,这些字符串中的一个(并且只有一个)是 2 位字符串(例如02)。这也是单元格中最短的字符串。在00部分之前可以有2-5个单词。
我需要在单元格中找到这两个数字/最短的字符串并将其写在单元格 A2 中。
我有一个这样的字符串:
0000000020100
$string = "0000000020100";
$new_string = '';
for($i=0; $i<strlen($string) ; $i++){
if($string[$i] != '0'){
$new_string .= $string[$i];
}
}
echo $new_string;
Run Code Online (Sandbox Code Playgroud)
但是我得到的输出是 21,我应该得到的输出是 20100。