我需要匹配所有这些开始标记:
<p>
<a href="foo">
Run Code Online (Sandbox Code Playgroud)
但不是这些:
<br />
<hr class="foo" />
Run Code Online (Sandbox Code Playgroud)
我想出了这个,并希望确保我做对了.我只抓住了a-z.
<([a-z]+) *[^/]*?>
Run Code Online (Sandbox Code Playgroud)
我相信它说:
/,然后我有这个权利吗?更重要的是,你怎么看?
将额外的空白区域替换为一个空白区域的最快方法是什么?
例如
从
foo bar
Run Code Online (Sandbox Code Playgroud)
至
foo bar
Run Code Online (Sandbox Code Playgroud) 亲爱的程序员,
我正在使用C#Visual Studio 2013编写代码,我刚刚意识到我可能不需要使用Trim()它Replace(" ", string.Empty).
一个例子如下:
SanitizedString = RawString
.Replace("/", string.Empty)
.Replace("\\", string.Empty)
.Replace(" ", string.Empty)
.Trim();
Run Code Online (Sandbox Code Playgroud)
因为我以前的代码结构不同,我没有注意到它:
SanitizedString = RawString.Trim()
.Replace("/", string.Empty)
.Replace("\\", string.Empty)
.Replace(" ", string.Empty);
Run Code Online (Sandbox Code Playgroud)
我知道这些方法的工作方式不同,因为Trim()删除了所有空白字符,而Replace(" ", string.Empty)只删除了空格字符.
这就是为什么我有一个不同的问题.
我没有看到任何明显的方法来实现与替换.我的问题是,当我希望从字符串中删除所有空白字符时,我将如何处理它?
我找到了以下内容:
但由于我从未使用正则表达式,我对如何将其应用于字符串犹豫不决?
由于.NET不使用C样式null来结束字符串,我如何保留分配的字符串,但使用不安全的代码来改变它的长度?
据我所知,.NET为每个字符串使用一个20字节的标头,大概这是存储字符串长度的地方,无论如何直接修改这个长度?因此,.NET会将字符串保留在内存中,但是当我调用.Length它时,它将返回.Length我想要的内容.
如果这是可能的,那么听到所有可能的副作用也会很有趣
UPDATE
我正在努力完成这个而不使用反射.
我有这个字符串
"go for goa"
Run Code Online (Sandbox Code Playgroud)
并且输出应该是
"go for goa"
Run Code Online (Sandbox Code Playgroud)
我想删除多余的空格。这意味着两个或多个连续空格应替换为一个空格。我想使用就地算法来做到这一点。
以下是我尝试过但不起作用的代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* Function to remove spaces in an string array */
char *removeSpaces(char *str) {
int ip_ind = 1;
/* In place removal of duplicate spaces*/
while(*(str + ip_ind)) {
if ((*(str + ip_ind) == *(str + ip_ind - 1)) && (*(str + ip_ind)==' ')) {
*(str_ip_ind-1)= *(str + ip_ind);
}
ip_ind++;
}
/* After above step add end of string*/
*(str + ip_ind) = …Run Code Online (Sandbox Code Playgroud) 使用trim()消除达特白色空间,这是行不通的。我做错了什么或有其他选择吗?
String product = "COCA COLA";
print('Product id is: ${product.trim()}');
Run Code Online (Sandbox Code Playgroud)
控制台打印: Product id is: COCA COLA
如何从字符串中删除所有空格?我可以想到一些显而易见的方法,例如遍历字符串并删除每个空白字符,或使用正则表达式,但是这些解决方案并不那么具有表现力或效率。什么是从字符串中删除所有空格的简单有效的方法?
我正在读取文本文件中的所有行到名为"masterToken"的列表,我正在尝试删除列表中的所有空格(文本文件中的所有空格).masterToken = masterToken.Where(s => !string.IsNullOrWhiteSpace(s)).ToList();是我目前正在尝试使用的,它不起作用.
有什么问题masterToken = masterToken.Where(s => !string.IsNullOrWhiteSpace(s)).ToList();和/或有没有更好的方法来实现我想要的东西?
谢谢
public static void Lex()
{
List<string> masterToken = File.ReadAllLines("C:\\Users\\Theo\\Documents\\Visual Studio 2017\\Projects\\Interpreter\\test.txt").ToList();
foreach (var item in masterToken)
{
Console.Write(item.ToString());
}
masterToken = masterToken.Where(s => !string.IsNullOrWhiteSpace(s)).ToList();
Console.WriteLine("");
foreach (var item in masterToken)
{
Console.Write(item.ToString());
}
Console.ReadLine();
}
Run Code Online (Sandbox Code Playgroud) 在 C# 中,我有一个包含空格、回车符和/或换行符的字符串。是否有一种简单的方法可以尽可能高效地规范化从文本文件导入的大字符串(100.000 到 1.000.000 个字符) ?
为了澄清我的意思:假设我的字符串看起来像 string1 但我希望它像 string2
string1 = " ab c\r\n de.\nf";
string2 = "abcde.f";
Run Code Online (Sandbox Code Playgroud) 我正在尝试进行 while 循环以确保员工的格式正确 - 但是只有当我第一次正确输入员工 ID 时它才有效。
When I put first a wrong format to the ID and then put a correct one it does not revaluate and identify it as true
Here is the part of the code in question:
Console.Write("Please enter your employee ID:");
empID = Console.ReadLine();
string pattern = @"^\d{9}[A-Z]{1}$";
Match match = Regex.Match(empID, pattern);
while (match.Success != true)
{
if (match.Success == true)
{
Console.WriteLine(empID);
}
else
{
Console.WriteLine("Incorrect employee ID - please try again");
empID …Run Code Online (Sandbox Code Playgroud) 嗨,我是C#的初学者,我试图删除字符串中的空格.我使用以下代码:
public String RemoveSpace(string str1)
{
char[] source = str1.ToCharArray();
int oldIndex = 0;
int newIndex = 0;
while (oldIndex < source.Length)
{
if (source[oldIndex] != ' ' && source[oldIndex] != '\t')
{
source[newIndex] = source[oldIndex];
newIndex++;
}
oldIndex++;
}
source[oldIndex] = '\0';
return new String(source);
}
Run Code Online (Sandbox Code Playgroud)
但我现在面临的问题是,当我给
输入字符串"H E的L-"
的输出显示"赫尔L"
这是因为在最后一次迭代oldIndex是arr[2]通过替换arr[4]最后一个字符"L"被冷落.有人可以指出正在做的错误吗?注意:不应使用正则表达式,修剪或替换功能.谢谢.
我正在构建一个允许用户更改其电子邮件地址的功能,如果他们在之前或之后意外地放入了空格,我需要修剪它以便正则表达式不会抱怨.它似乎没有在我拥有它的地方工作(字符串保持完全相同)但我不知道为什么.这是在我的控制器中:
public ActionResult ChangeEmail(ChangeEmailVM vm)
{
try
{
if (UserManager.EmailAvail(new EmailRequest
{
Email = vm.EmailAddress.Trim()
}))
else//...etc etc
Run Code Online (Sandbox Code Playgroud)
我想我可能需要将.trim()放在电子邮件地址的getter或setter中,但我不确定正确的语法.
我有这个代码:
if (TextParsingConfiguration.join == true)
{
images = images.Trim(' ');
}
if (TextParsingConfiguration.removecommas == true)
{
images = ReplacingChars(images, new[] { ',', '"' }, " ");
}
Run Code Online (Sandbox Code Playgroud)
join和removecommas是bool变量我也在checkBoxes中的form1中使用它们.和ReplacingChars方法:
public static string ReplacingChars(string source, char[] toReplace, string withThis)
{
return string.Join(withThis, source.Split(toReplace, StringSplitOptions.None));
}
Run Code Online (Sandbox Code Playgroud)
也许我错了逻辑,但我想给用户两个选项.
如果2号是逻辑的问题呢?如果不是我可以删除(清洁)的其他选项?removecommas正在运行.它删除逗号和引号并保留空格.