问题描述
大家好,
我不知道如何从字符串中获取子字符串。实际上我正在开发一个项目,它向用户发送一个自动生成的电子邮件,在那个电子邮件中,他们是用户可以通过其访问网站的链接,现在我需要做的是从链接中提取数据并将其读入例如,这是我的链接(localhost:57684 / Booking_Site_TimeSlots?code = 58_148),最后一个数字58_148是两个不同的ID,例如58是用户ID,148是系列ID,我需要使用字符串获取这些ID然后我需要创建一个子字符串来分隔这些ID,这是我为了从链接中获取ID而创建的字符串(字符串代码=!string.IsNullOrEmpty(Request.QueryString [code])?Request.QueryString [ code]:Guid.Empty.ToString();)
请帮帮我个人,
谢谢。
我尝试了什么:
我无法解决这个问题我尝试过但是他们没有用。
Hi Everyone,
I do not know how to get a sub-string form a string. Actually i am working on a project which sends an automatically generated email to the user and in that email their is a link through which the user can access the website, now what i need to do is to extract data from the link and read it into the website for example this is my link (localhost:57684/Booking_Site_TimeSlots?code=58_148) and the last numbers 58_148 are two different IDs like 58 is the user ID and 148 is the Series ID, i need to get these IDs using a string and then i need to create a substring to separate these IDs and this is the string i have created to get the IDs from the link (string code = !string.IsNullOrEmpty(Request.QueryString["code"]) ? Request.QueryString["code"] : Guid.Empty.ToString();)
Please Help Me out Guys,
Thank You.
What I have tried:
Im not able to work out this i tried somethings but they did not worked.
推荐答案
var s = "localhost:57684/Booking_Site_TimeSlots?code=58_148";
var l = s.Split('=');
var id = l[l.GetLength(0) - 1];
var x = id.Split('_');
Console.WriteLine("Separated parts are: {0},{1}", x[0], x[1]);
给出结果
Separated parts are: 58,148
你应该在这里添加一些检查以确保你确实在数组元素中有数据
You should add some checks in here to make sure you actually have data in the array elements
private static string[] GetIds(string inputStr)
{
string[] returnStrings = {"Null","Null"};
// Yes I intended these to be the word Null not null value for the purpose of this demo
if (inputStr == null) return returnStrings;
if (!inputStr.Contains("=")) return returnStrings;
var l = inputStr.Split('=');
if (l.Length > l.GetLength(0) - 1)
{
var id = l[l.GetLength(0) - 1];
returnStrings = id.Split('_');
}
return returnStrings;
}
以下是一些测试运行
And here are some test runs
string[] tests =
{
"localhost:57684/Booking_Site_TimeSlots?code=58_148",
"localhost:57684/Booking_Site_TimeSlots?code=58",
"localhost:57684/Booking_Site_TimeSlots?code",
"localhost:57684/Booking_Site_TimeSlots?code=58_148_88",
"localhost:57684/Booking_Site_TimeSlots?code=",
""
};
for (var i = 0; i < tests.GetLength(0); i++)
{
var r = GetIds(tests[i]);
Console.WriteLine("INPUT: {0}. Separated parts are {1}, {2}", tests[i],
(r.GetLength(0) >= 1) ? r[0] : "Not Found",
(r.GetLength(0) >= 2) ? r[1] : "Not Found");
}
结果:
Results:
INPUT: localhost:57684/Booking_Site_TimeSlots?code=58_148. Separated parts are 58, 148
INPUT: localhost:57684/Booking_Site_TimeSlots?code=58. Separated parts are 58, Not Found
INPUT: localhost:57684/Booking_Site_TimeSlots?code. Separated parts are Null, Null
INPUT: localhost:57684/Booking_Site_TimeSlots?code=58_148_88. Separated parts are 58, 148
INPUT: localhost:57684/Booking_Site_TimeSlots?code=. Separated parts are , Not Found
INPUT: . Separated parts are Null, Null
我故意使用Null和Not Found来证明值的设置位置。
I deliberately used "Null" and "Not Found" to demonstrate where the values were being set.
这篇关于如何从字符串中获取子字符串。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!