Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How does Java return string fields

2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains how Java returns string fields. Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let Xiaobian take you to learn "Java how to return string fields"!

Title: The return string has several fields.

Idea: Simple, spaces are the flags that distinguish fields.

Write your own trim() function to remove spaces at both ends of a string;

Determine whether the string is null or not. If null, return 0, which means there are 0 fields.

Distinguish fields by using the space flag: the previous character is a space, the current character is not a space, and the segment count is incremented by one.

Language:cpp

class Solution {public://string& trim(string &s) {if (s.empty()) {return s; } s.erase(0, s.find_first_not_of(">)); s.erase(s.find_last_not_of(" ") + 1);return s; }int countSegments(string s) {//String empty, returns 0if (trim(s).empty()) {return 0; }int ans = 1; s = trim(s); for (int i = 0; i < s.length(); i++) {//a field if (s[i-1] == ' ' && s[i] != if the previous character is a space and the current character is not a space ' ') { ans++; } }return ans; }}; At this point, I believe that everyone has a deeper understanding of "how Java returns string fields", so let's actually operate it! Here is the website, more related content can enter the relevant channels for inquiry, pay attention to us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report