Dec 14 2009
string.IndexOf and string.SubString example code
เอาตัวอย่างการใช้งาน IndexOf กับ SubString มาให้ดูกันอีกซักตัวอย่าง โดย code ตัวนี้เอาไว้แยก string ออกเป็น 2 ตัวด้วย “||” วิธีปกติผมจะ Replace || ด้วย | จากนั้นค่อย SubString ด้วยตัว | อีกครั้ง ซึ่งก็ได้ผลลัพธ์ที่ถูกต้องเช่นกัน แต่นี่ก็เป็นอีกวิธีหนึ่งครับ เป็นการดูว่าตัว || อยู่ในตำแหน่งไหนของ string แล้วค่อยแยก string ออกเป็น 2 ตัวด้วยตำแหน่งของ || ครับ ลองดูตัวอย่าง น่าจะเข้าใจนะครับ
1 2 3 4 5 6 7 8 9 10 11 12 | string body = "1234||http://www.aspgod.com"; int indexSub = body.IndexOf("||"); if (indexSub != -1) { string msg = body.Substring(0, indexSub); string url = body.Substring(indexSub + 2, body.Length - indexSub - 2); Response.Write(indexSub + "<br/>"); // 4 Response.Write(msg + "<br/>"); // 1234 Response.Write(url + "<br/>"); // http://www.aspgod.com } |
ผม write ผลลัพธ์ออกมา 3 ตัว
1. ตำแหน่งของ ||
2. string ชุดแรกที่ตัดออกมา
3. string ชุดสองที่ตัดออกมา
No related posts.
Related posts brought to you by Yet Another Related Posts Plugin.
Tweets that mention string.IndexOf and string.SubString example code | oue's profile -- Topsy.com
Dec 14, 2009 @ 20:37:35
[...] This post was mentioned on Twitter by inooomzaa, Ekasit. Ekasit said: ตัวอย่างการใ้งาน IndexOf และ SubString ครับ ขำๆ http://www.aspgod.com/index.php/aspnet/string-indexof-and-string-substring-example-code [...]
Feb 28, 2011 @ 11:11:32
Good article. Thank you.
http://ericclaptonsiteru.blog.com/2011/02/04/full-information-adalat/
Mar 05, 2011 @ 20:05:43
Good article. Thank you.
Mar 26, 2011 @ 12:58:22
Good Article
http://www.nowalways.co.cc