PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, May 18, 2022

[FIXED] How to substring a text easily

 May 18, 2022     c#, html, partial, substring, text     No comments   

Issue

Alright here the question. This is an example text

Nokia 700<span>Stok Kodu: 389952</span>
<br><span style="background-image:none; margin:0; padding:0; font:14px/18px Arial,Helvetica,sans-serif">Nokia 700 Cep Telefonu PDA, Dokunmatik, Bluetooth, Radyo</span>

I want to get "Nokia 700" which starts with index 0 and ends with <span> and i am able to do it with the way below

var singleNode = myDoc.DocumentNode.SelectSingleNode(srxProductName);
string srProductName = singleNode.InnerHtml.ToString()
    .Substring(0, singleNode.InnerHtml.ToString().IndexOf("<span>"));

The question is i wonder are there any easier or more practical way of doing it.


Solution

If you already have the node containing your text in singleNode, you can just extract the first child node, which should be a text node, and get its value:

var productName = singleNode.ChildNodes[0].Value;


Answered By - Jacob
Answer Checked By - Robin (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

1,214,515

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © 2025 PHPFixing